Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractYamlReader
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
4 / 4
9
100.00% covered (success)
100.00%
1 / 1
 __construct
n/a
0 / 0
n/a
0 / 0
0
 getDefaultConfigurationPath
n/a
0 / 0
n/a
0 / 0
0
 instance
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
5
 resetInstances
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 loadYaml
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 parseSimpleArray
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace WsdlToPhp\PackageGenerator\ConfigurationReader;
6
7use Symfony\Component\Yaml\Parser;
8
9abstract class AbstractYamlReader
10{
11    protected static array $instances;
12
13    protected string $filename;
14
15    abstract protected function __construct(string $filename);
16
17    abstract public static function getDefaultConfigurationPath(): string;
18
19    /**
20     * @throws \InvalidArgumentException
21     */
22    public static function instance(?string $filename = null): self
23    {
24        $loadFilename = empty($filename) ? static::getDefaultConfigurationPath() : $filename;
25        if (empty($loadFilename) || !is_file($loadFilename)) {
26            throw new \InvalidArgumentException(sprintf('Unable to locate file "%s"', $loadFilename), __LINE__);
27        }
28
29        $key = sprintf('%s_%s', static::class, $loadFilename);
30        if (!isset(self::$instances[$key])) {
31            self::$instances[$key] = new static($loadFilename);
32        }
33
34        return self::$instances[$key];
35    }
36
37    /**
38     * For tests purpose only!
39     */
40    public static function resetInstances(): void
41    {
42        self::$instances = [];
43    }
44
45    protected function loadYaml(string $filename)
46    {
47        $ymlParser = new Parser();
48
49        return $ymlParser->parse(file_get_contents($filename));
50    }
51
52    /**
53     * @throws \InvalidArgumentException
54     */
55    protected function parseSimpleArray(string $filename, string $mainKey): array
56    {
57        $values = $this->loadYaml($filename);
58        if (!array_key_exists($mainKey, $values)) {
59            throw new \InvalidArgumentException(sprintf('Unable to find section "%s" in "%s"', $mainKey, $filename), __LINE__);
60        }
61
62        return $values[$mainKey];
63    }
64}