Test Failed
Push — master ( 431fcb...762008 )
by Mikael
01:27
created

DIFactoryConfig::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
ccs 3
cts 5
cp 0.6
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 2.2559
1
<?php
2
3
namespace Anax\DI;
4
5
use \Anax\Configure\ConfigureInterface;
6
use \Anax\Configure\Configure2Trait;
7
8
/**
9
 * DI factory class creating a set of default services by loading
10
 * them from a configuration file.
11
 */
12
class DIFactoryConfig extends DI implements ConfigureInterface
13
{
14
    use Configure2Trait {
15
        configure as protected configure2;
16
    }
17
18
19
20
    /**
21 6
     * Constructor optionally creating a set of services from a configuration
22
     * file.
23 6
     *
24 6
     * @todo Remove $configFile as argument and force using method configure()
25 6
     *
26 6
     * @param array|string|null $what is an array with key/value
27 6
     *                                config options or a file to
28
     *                                be included which returns such
29
     *                                an array.
30
     */
31 6
    public function __construct($what = null)
32
    {
33
        if (!is_null($what)) {
34 6
            $this->configure($what);
35 6
        }
36
    }
37
38
39
40
    /**
41
     * Set a configuration object to use, this object have the ability to
42
     * load configuration from an array or files.
43
     *
44
     * @param array|string $what is an array with key/value config options
45
     *                           or a file to be included which returns such
46
     *                           an array.
47
     *
48
     * @return self
49
     */
50
    public function configure($what)
51
    {
52
        $this->configure2($what);
53
54
        if (isset($this->config["services"])) {
55
            foreach ($this->config["services"] as $name => $service) {
56
                $this->createService($name, $service);
57
            }
58
        }
59
60
        if (isset($this->config["items"])) {
61
            foreach ($this->config["items"] as $config) {
62
                if (isset($config["services"])) {
63
                    foreach ($config["services"] as $name => $service) {
64
                        $this->createService($name, $service);
65
                    }
66
                }
67
            }
68
        }
69
70
        return $this;
71
    }
72
73
74
75
    /**
76
     * Create a service from a name and an array containing details on how to
77
     * create it.
78
     *
79
     * @param string $name    of service.
80
     * @param array  $service details to use when creating the service.
81
     *
82
     * @return self
83
     */
84
    protected function createService($name, $service)
85
    {
86
        if (isset($service["shared"]) && $service["shared"]) {
87
            $this->setShared($name, $service["callback"]);
88
        } else {
89
            $this->set($name, $service["callback"]);
90
        }
91
92
        if (isset($service["active"]) && $service["active"]) {
93
            $this->get($name);
94
        }
95
96
        return $this;
97
    }
98
}
99