Passed
Pull Request — master (#6)
by Korotkov
01:52
created

Container::iOc()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 2
nop 3
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author    : Korotkov Danila <[email protected]>
7
 * @copyright Copyright (c) 2018, Korotkov Danila
8
 * @license   http://www.gnu.org/licenses/gpl.html GNU GPLv3.0
9
 */
10
11
namespace Rudra;
12
13
14
use \ReflectionClass;
15
16
17
/**
18
 * Class ContainerInterface
19
 *
20
 * @package Rudra
21
 */
22
class Container implements ContainerInterface, ContainerGlobalScopeInterface
23
{
24
25
    use ContainerReflectionTrait;
0 ignored issues
show
Bug introduced by
The trait Rudra\ContainerReflectionTrait requires the property $name which is not provided by Rudra\Container.
Loading history...
26
    use ContainerCookieTrait;
27
    use ContainerGlobalsTrait;
28
    use ContainerSessionTrait;
29
    use ContainerConfigTrait;
30
    use ContainerResponseTrait;
31
32
    /**
33
     * @var ContainerInterface
34
     */
35
    public static $app;
36
    /**
37
     * @var array
38
     */
39
    protected $objects = [];
40
    /**
41
     * @var array
42
     */
43
    protected $bind = [];
44
45
    /**
46
     * @return ContainerInterface
47
     */
48
    public static function app(): ContainerInterface
49
    {
50
        if (!static::$app instanceof static) {
51
            static::$app = new static();
52
        }
53
54
        return static::$app;
55
    }
56
57
    /**
58
     * @param $app
59
     */
60
    public function setServices(array $app): void
61
    {
62
        foreach ($app['contracts'] as $interface => $contract) {
63
            $this->setBinding($interface, $contract);
64
        }
65
66
        foreach ($app['services'] as $name => $service) {
67
            if (array_key_exists(1, $service)) {
68
                $this->set($name, $service[0], $service[1]);
69
                continue;
70
            }
71
72
            $this->set($name, $service[0]);
73
        }
74
    }
75
76
    /**
77
     * @param $key
78
     *
79
     * @return mixed
80
     */
81
    public function get(string $key = null)
82
    {
83
        return (empty($key)) ? $this->objects : $this->objects[$key];
84
    }
85
86
    /**
87
     * @param string $key
88
     * @param        $object
89
     * @param array  $params
90
     *
91
     * @return object|void
92
     */
93
    public function set(string $key, $object, $params = null)
94
    {
95
        ('raw' == $params) ? $this->rawSet($key, $object) : $this->iOc($key, $object, $params);
0 ignored issues
show
Bug introduced by
It seems like $params can also be of type array; however, parameter $params of Rudra\Container::iOc() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

95
        ('raw' == $params) ? $this->rawSet($key, $object) : $this->iOc($key, $object, /** @scrutinizer ignore-type */ $params);
Loading history...
96
    }
97
98
    /**
99
     * @param      $object
100
     * @param null $params
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $params is correct as it would always require null to be passed?
Loading history...
101
     *
102
     * @return object
103
     */
104
    public function new($object, $params = null)
105
    {
106
        $reflection  = new ReflectionClass($object);
107
        $constructor = $reflection->getConstructor();
108
109
        if ($constructor && $constructor->getNumberOfParameters()) {
110
            $paramsIoC = $this->getParamsIoC($constructor, $params);
111
112
            return $reflection->newInstanceArgs($paramsIoC);
113
        }
114
115
        return new $object;
116
    }
117
118
    /**
119
     * @param $key
120
     *
121
     * @return bool
122
     */
123
    public function has(string $key): bool
124
    {
125
        return isset($this->objects[$key]);
126
    }
127
128
    /**
129
     * @param string $key
130
     * @param string $param
131
     *
132
     * @return mixed
133
     */
134
    public function getParam(string $key, string $param)
135
    {
136
        if ($this->has($key) && isset($this->get($key)->$param)) {
137
            return $this->get($key)->$param;
138
        }
139
    }
140
141
    /**
142
     * @param string $key
143
     * @param string $param
144
     * @param        $value
145
     */
146
    public function setParam(string $key, string $param, $value): void
147
    {
148
        if (isset($this->objects[$key])) {
149
            $this->get($key)->$param = $value;
150
        }
151
    }
152
153
    /**
154
     * @param string $key
155
     * @param string $param
156
     *
157
     * @return bool
158
     */
159
    public function hasParam(string $key, string $param)
160
    {
161
        if ($this->has($key)) {
162
            return isset($this->get($key)->$param);
163
        }
164
    }
165
166
    /**
167
     * @param string $key
168
     *
169
     * @return mixed|string
170
     */
171
    public function getBinding(string $key)
172
    {
173
        return $this->bind[$key] ?? $key;
174
    }
175
176
    /**
177
     * @param string $key
178
     * @param        $value
179
     */
180
    public function setBinding(string $key, $value): void
181
    {
182
        $this->bind[$key] = $value;
183
    }
184
}
185