Passed
Push — master ( 3fe46b...97da99 )
by Andy
01:40
created

ContainerFactory   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 106
ccs 38
cts 38
cp 1
rs 10
c 0
b 0
f 0
wmc 16

6 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 5 1
A requirePhpFile() 0 3 1
A __construct() 0 19 4
A parseYamlFile() 0 9 2
A parseImports() 0 17 5
A getImportResource() 0 10 3
1
<?php
2
3
namespace Palmtree\Container;
4
5
use Symfony\Component\Yaml\Yaml;
6
7
class ContainerFactory
8
{
9
    /** @var Container */
10
    private $container;
11
    /** @var array */
12
    private $phpImports = [];
13
14 24
    private function __construct($configFile)
15
    {
16 24
        $yaml = $this->parseYamlFile($configFile);
17
18 24
        if (!isset($yaml['services'])) {
19 2
            $yaml['services'] = [];
20
        }
21
22 24
        if (!isset($yaml['parameters'])) {
23 2
            $yaml['parameters'] = [];
24
        }
25
26 24
        $this->container = new Container($yaml['services'], $yaml['parameters']);
27
28 24
        foreach ($this->phpImports as $file) {
29 21
            self::requirePhpFile($file, $this->container);
30
        }
31
32 24
        $this->container->instantiateServices();
33 24
    }
34
35
    /**
36
     * @param string $configFile
37
     *
38
     * @return Container
39
     */
40 24
    public static function create($configFile)
41
    {
42 24
        $factory = new self($configFile);
43
44 24
        return $factory->container;
45
    }
46
47
    /**
48
     * @param string $file
49
     *
50
     * @return array
51
     */
52 24
    private function parseYamlFile($file)
53
    {
54 24
        $data = Yaml::parseFile($file);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $data is correct as Symfony\Component\Yaml\Yaml::parseFile($file) targeting Symfony\Component\Yaml\Yaml::parseFile() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
55
56 24
        if (isset($data['imports'])) {
57 21
            $data = $this->parseImports($data, dirname($file));
58
        }
59
60 24
        return $data;
61
    }
62
63
    /**
64
     * @param array  $data
65
     * @param string $dir
66
     *
67
     * @return array
68
     */
69 21
    private function parseImports($data, $dir)
70
    {
71 21
        foreach ($data['imports'] as $key => $import) {
72 21
            $resource = self::getImportResource($dir, $import);
73
74 21
            $extension = pathinfo($resource, PATHINFO_EXTENSION);
75
76 21
            if ($extension === 'yml' || $extension === 'yaml') {
77 21
                $data = array_replace_recursive($data, $this->parseYamlFile($resource));
78 21
                unset($data['imports'][$key]);
79 21
            } elseif ($extension === 'php') {
80 21
                $this->phpImports[] = $resource;
81 21
                unset($data['imports'][$key]);
82
            }
83
        }
84
85 21
        return $data;
86
    }
87
88
    /**
89
     * @param string    $file
90
     * @param Container $container
91
     */
92 21
    private static function requirePhpFile($file, $container)
93
    {
94 21
        require $file;
95 21
    }
96
97
    /**
98
     * @param string $dir
99
     * @param array  $import
100
     *
101
     * @return string
102
     */
103 21
    private static function getImportResource($dir, $import)
104
    {
105 21
        $resource = $import['resource'];
106
107
        // Prefix the directory if resource is not an absolute path
108 21
        if ($resource[0] !== DIRECTORY_SEPARATOR && !preg_match('~\A[A-Z]:(?![^/\\\\])~i', $resource)) {
109 21
            $resource = "$dir/$resource";
110
        }
111
112 21
        return $resource;
113
    }
114
}
115