Completed
Pull Request — master (#3)
by Josh
13:39
created

Bucket::getFromFactory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 2
cts 2
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace CodeJet\Bucket;
4
5
use CodeJet\Bucket\Exception\NotFoundException;
6
use Interop\Container\ContainerInterface;
7
8
class Bucket implements ContainerInterface
9
{
10
    /** @var \Closure[] */
11
    private $factories = [];
12
13
    /** @var array */
14
    private $values = [];
15
16
    /** @var ContainerInterface */
17
    private $delegateContainer;
18
19
    public function __construct(array $values = [])
20
    {
21
        foreach ($values as $id => $value) {
22
            $this->add($id, $value);
23
        }
24 12
    }
25
26 12
    /**
27 9
     * @param string $id
28
     * @param mixed $value
29
     *
30 3
     * @return self
31
     */
32 3
    public function add(string $id, $value)
33
    {
34
        if ($value instanceof \Closure) {
35
            return $this->addFactory($id, $value);
36
        }
37
38
        $this->values[$id] = $value;
39
40
        return $this;
41 9
    }
42
43 9
    /**
44
     * @param string $id
45 9
     * @param \Closure $value
46
     *
47
     * @return self
48
     */
49
    public function addFactory(string $id, \Closure $value)
50
    {
51 15
        $this->factories[$id] = $value;
52
53 15
        return $this;
54 3
    }
55
56
    /**
57 12
     * @inheritdoc
58 9
     */
59
    public function get($id)
60
    {
61 12
        if (!$this->has($id)) {
62
            throw new NotFoundException('Item with id "' . $id . '" was not found.');
63
        }
64
65
        if (!isset($this->values[$id])) {
66
            $this->values[$id] = $this->getFromFactory($id);
67
        }
68 9
69
        return $this->values[$id];
70 9
    }
71 9
72
    /**
73 9
     * @param string $id
74
     * @return mixed
75
     */
76
    private function getFromFactory(string $id)
77
    {
78
        $factory = $this->factories[$id];
79 15
        $containerToUseForDependencies = $this->delegateContainer ?: $this;
80
81 15
        return $factory($containerToUseForDependencies);
82
    }
83
84
    /**
85
     * @inheritdoc
86
     */
87
    public function has($id): bool
88 15
    {
89
        return $this->hasFactory($id) || $this->hasValue($id);
90 15
    }
91
92
    /**
93
     * @param $id
94
     * @return bool
95
     */
96
    protected function hasFactory(string $id): bool
97 6
    {
98
        return isset($this->factories[$id]);
99 6
    }
100
101
    /**
102
     * @param $id
103
     * @return bool
104
     */
105 3
    protected function hasValue(string $id): bool
106
    {
107 3
        return isset($this->values[$id]);
108 3
    }
109
110
    /**
111
     * @param ContainerInterface $delegateContainer
112
     */
113
    public function setDelegateContainer(ContainerInterface $delegateContainer)
114
    {
115
        $this->delegateContainer = $delegateContainer;
116
    }
117
}
118