Completed
Pull Request — master (#4)
by Woody
01:55
created

InjectorContainer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 48
ccs 14
cts 14
cp 1
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 12 3
A hasReference() 0 6 1
A has() 0 4 2
1
<?php
2
3
namespace Northwoods\Container;
4
5
use Auryn\Injector;
6
use Auryn\InjectorException;
7
use Psr\Container\ContainerInterface;
8
9
class InjectorContainer implements ContainerInterface
10
{
11
    const I_ALL = 31;
12
13
    /**
14
     * @var Injector
15
     */
16
    private $injector;
17
18 7
    public function __construct(Injector $injector)
19
    {
20 7
        $this->injector = $injector;
21 7
    }
22
23
    // ContainerInterface
24 4
    public function get($id)
25
    {
26 4
        if (false === $this->has($id)) {
27 1
            throw NotFoundException::classDoesNotExist($id);
28
        }
29
30
        try {
31 3
            return $this->injector->make($id);
32 1
        } catch (InjectorException $e) {
33 1
            throw ContainerException::couldNotMake($id, $e);
34
        }
35
    }
36
37
    // ContainerInterface
38 5
    public function has($id)
39
    {
40 5
        return class_exists($id) || $this->hasReference($id);
41
    }
42
43
    /**
44
     * Check the injector has a reference
45
     *
46
     * @param string $id
47
     *
48
     * @return bool
49
     */
50 3
    private function hasReference($id)
51
    {
52
        // https://github.com/rdlowrey/auryn/issues/157
53 3
        $details = $this->injector->inspect($id, self::I_ALL);
54 3
        return (bool) array_filter($details);
55
    }
56
}
57