PimpleAdapter::hasKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Hgraca\MicroDI\Adapter\Pimple;
4
5
use Hgraca\MicroDI\Adapter\Exception\ArgumentNotFoundException;
6
use Hgraca\MicroDI\Adapter\Exception\ClassOrInterfaceNotFoundException;
7
use Hgraca\MicroDI\Adapter\Exception\FactoryContextNotFoundException;
8
use Hgraca\MicroDI\Adapter\Exception\InstanceNotFoundException;
9
use Hgraca\MicroDI\Adapter\Exception\NotAnObjectException;
10
use Hgraca\MicroDI\BuilderInterface;
11
use Hgraca\MicroDI\Port\ContainerInterface;
12
use Pimple\Container;
13
14
final class PimpleAdapter implements ContainerInterface
15
{
16
    const FACTORY_CONTEXT_POSTFIX = '.context';
17
18
    /** @var Container */
19
    private $container;
20
21 26
    public function __construct(Container $container)
22
    {
23 26
        $this->container = $container;
24 26
    }
25
26 6
    public function addArgument(string $parameter, $argument)
27
    {
28 6
        $this->addKey($parameter, $argument);
29 6
    }
30
31 8
    public function hasArgument(string $parameter): bool
32
    {
33 8
        return $this->hasKey($parameter);
34
    }
35
36
    /**
37
     * @throws ArgumentNotFoundException
38
     *
39
     * @return mixed
40
     */
41 5
    public function getArgument(string $parameter)
42
    {
43 5
        if (! $this->hasArgument($parameter)) {
44 1
            throw new ArgumentNotFoundException();
45
        }
46
47 4
        return $this->getKey($parameter);
48
    }
49
50 3
    public function addFactoryContext(string $factoryClass, array $context)
51
    {
52 3
        return $this->addKey($this->generateFactoryContextKey($factoryClass), $context);
53
    }
54
55 4
    public function hasFactoryContext(string $factoryClass): bool
56
    {
57 4
        return $this->hasKey($this->generateFactoryContextKey($factoryClass));
58
    }
59
60 2
    public function getFactoryContext(string $factoryClass): array
61
    {
62 2
        if (! $this->hasFactoryContext($factoryClass)) {
63 1
            throw new FactoryContextNotFoundException();
64
        }
65
66 1
        return $this->getKey($this->generateFactoryContextKey($factoryClass));
67
    }
68
69 8
    public function addInstance($instance)
70
    {
71 8
        if (! is_object($instance)) {
72 1
            throw new NotAnObjectException();
73
        }
74
75 7
        $this->container[get_class($instance)] = $instance;
76 7
    }
77
78 2
    public function addInstanceLazyLoad(BuilderInterface $builder, string $class, array $arguments = [])
79
    {
80 2
        $this->assertClassOrInterfaceExists($class);
81
82 1
        $this->container[$class] = function () use ($builder, $class, $arguments) {
83 1
            return $builder->buildInstance($class, $arguments);
84
        };
85 2
    }
86
87
    /**
88
     * @throws ClassOrInterfaceNotFoundException
89
     */
90 11
    public function hasInstance(string $class): bool
91
    {
92 11
        $this->assertClassOrInterfaceExists($class);
93
94 9
        return $this->hasKey($class);
95
    }
96
97
    /**
98
     * @throws InstanceNotFoundException
99
     * @throws ClassOrInterfaceNotFoundException
100
     *
101
     * @return mixed
102
     */
103 8
    public function getInstance(string $class)
104
    {
105 8
        if (! $this->hasInstance($class)) {
106 1
            throw new InstanceNotFoundException();
107
        }
108
109 6
        return $this->getKey($class);
110
    }
111
112
    /**
113
     * @return mixed
114
     */
115 9
    private function addKey(string $key, $content)
116
    {
117 9
        return $this->container[$key] = $content;
118
    }
119
120 18
    private function hasKey(string $key): bool
121
    {
122 18
        return isset($this->container[$key]);
123
    }
124
125
    /**
126
     * @return mixed
127
     */
128 9
    private function getKey(string $key)
129
    {
130 9
        return $this->container[$key];
131
    }
132
133 5
    private function generateFactoryContextKey(string $factoryClass): string
134
    {
135 5
        return $factoryClass . self::FACTORY_CONTEXT_POSTFIX;
136
    }
137
138
    /**
139
     * @throws ClassOrInterfaceNotFoundException
140
     */
141 12
    private function assertClassOrInterfaceExists(string $class)
142
    {
143 12
        if (! class_exists($class) && ! interface_exists($class)) {
144 2
            throw new ClassOrInterfaceNotFoundException();
145
        }
146 10
    }
147
}
148