Completed
Pull Request — master (#44)
by Korotkov
01:42
created

Objects::set()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
/**
4
 * @author    : Jagepard <[email protected]">
5
 * @copyright Copyright (c) 2019, Jagepard
6
 * @license   https://mit-license.org/ MIT
7
 */
8
9
namespace Rudra\Container;
10
11
use Rudra\Container\Interfaces\ContainerInterface;
12
13
class Objects extends Container
14
{
15
    /**
16
     * @var ContainerInterface
17
     */
18
    private $binding;
19
20
    /**
21
     * Objects constructor.
22
     * @param $binding
23
     */
24
    public function __construct(ContainerInterface $binding)
25
    {
26
        $this->binding = $binding;
27
    }
28
29
    /**
30
     * @param  array  $data
31
     * @throws \ReflectionException
32
     */
33
    public function set(array $data): void
34
    {
35
        list($key, $object) = $data;
36
37
        if (array_key_exists(1, $object)) {
38
            ('raw' === $object[1]) ? $this->mergeData($key, $object[0]) : $this->iOc($key, $object[0], $object[1]);
39
            return;
40
        }
41
42
        $this->iOc($key, $object[0]);
43
    }
44
45
    /**
46
     * @param  string  $key
47
     * @param $object
48
     * @throws \ReflectionException
49
     */
50
    private function mergeData(string $key, $object)
51
    {
52
        $this->data = array_merge([$key => $object], $this->data);
53
    }
54
55
    /**
56
     * @param  string  $key
57
     * @param        $object
58
     * @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...
59
     * @throws \ReflectionException
60
     */
61
    private function iOc(string $key, $object, $params = null): void
62
    {
63
        $reflection = new \ReflectionClass($object);
64
        $constructor = $reflection->getConstructor();
65
66
        if ($constructor && $constructor->getNumberOfParameters()) {
67
            $paramsIoC = $this->getParamsIoC($constructor, $params);
68
            $this->mergeData($key, $reflection->newInstanceArgs($paramsIoC));
69
            return;
70
        }
71
72
        $this->mergeData($key, new $object());
73
    }
74
75
76
    /**
77
     * @param  \ReflectionMethod  $constructor
78
     * @param                  $params
79
     * @return array
80
     * @throws \ReflectionException
81
     */
82
    private function getParamsIoC(\ReflectionMethod $constructor, $params): array
83
    {
84
        $i = 0;
85
        $paramsIoC = [];
86
87
        foreach ($constructor->getParameters() as $value) {
88
            if (isset($value->getClass()->name) && $this->binding->has($value->getClass()->name)) {
89
                $className = $this->binding->get($value->getClass()->name);
90
                $paramsIoC[] = (is_object($className)) ? $className : new $className;
91
                continue;
92
            }
93
94
            if ($value->isDefaultValueAvailable() && !isset($params[$i])) {
95
                $paramsIoC[] = $value->getDefaultValue();
96
                continue;
97
            }
98
99
            $paramsIoC[] = $params[$i++];
100
        }
101
102
        return $paramsIoC;
103
    }
104
105
    /**
106
     * @param      $object
107
     * @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...
108
     * @return object
109
     * @throws \ReflectionException
110
     */
111
    public function new($object, $params = null)
112
    {
113
        $reflection = new \ReflectionClass($object);
114
        $constructor = $reflection->getConstructor();
115
116
        if ($constructor && $constructor->getNumberOfParameters()) {
117
            $paramsIoC = $this->getParamsIoC($constructor, $params);
118
119
            return $reflection->newInstanceArgs($paramsIoC);
120
        }
121
122
        return new $object();
123
    }
124
}
125