ErrorHandlerContainer::getFactory()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 5
c 2
b 0
f 0
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 10
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
/**
4
 * This file is part of coisa/error-handler.
5
 *
6
 * (c) Felipe Sayão Lobato Abreu <[email protected]>
7
 *
8
 * This source file is subject to the license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
declare(strict_types=1);
13
14
namespace CoiSA\ErrorHandler\Container;
15
16
use Psr\Container\ContainerInterface;
17
18
/**
19
 * Class ErrorHandlerContainer
20
 *
21
 * @package CoiSA\ErrorHandler\Container
22
 */
23
final class ErrorHandlerContainer implements ContainerInterface
24
{
25
    /**
26
     * @var null|ContainerInterface
27
     */
28
    private $container;
29
30
    /**
31
     * @var string[]
32
     */
33
    private $factories;
34
35
    /**
36
     * @var string[]
37
     */
38
    private $aliases;
39
40
    /**
41
     * @var object[]
42
     */
43
    private $instances;
44
45
    /**
46
     * ErrorHandlerContainer constructor.
47
     *
48
     * @param null|ContainerInterface $container
49
     */
50 85
    public function __construct(ContainerInterface $container = null)
51
    {
52 85
        $configProvider = new ConfigProvider();
53
54 85
        $this->factories = $configProvider->getFactories();
55 85
        $this->aliases   = $configProvider->getAliases();
56
57 85
        $this->container = $container;
58 85
    }
59
60
    /**
61
     * @param string $id
62
     *
63
     * @return bool
64
     */
65 45
    public function has($id)
66
    {
67 45
        return ($this->container && $this->container->has($id))
68 35
            || \array_key_exists($id, $this->aliases)
69 45
            || \array_key_exists($id, $this->factories);
70
    }
71
72
    /**
73
     * @param string $id
74
     *
75
     * @throws Exception\ContainerException
76
     * @throws Exception\NotFoundException
77
     *
78
     * @return mixed
79
     */
80 38
    public function get($id)
81
    {
82 38
        if ($this->container && $this->container->has($id)) {
83 35
            return $this->container->get($id);
84
        }
85
86 18
        if (!isset($this->instances[$id])) {
87
            try {
88 18
                $this->instances[$id] = ($this->getFactory($id))($this);
89 2
            } catch (Exception\NotFoundException $notFoundException) {
90 1
                throw $notFoundException;
91 1
            } catch (\Throwable $throwable) {
92 1
                throw Exception\ContainerException::createFromThrowable($throwable);
93
            }
94
        }
95
96 16
        return $this->instances[$id];
97
    }
98
99
    /**
100
     * @param string $id
101
     *
102
     * @throws Exception\NotFoundException
103
     *
104
     * @return callable
105
     */
106 18
    private function getFactory(string $id): callable
107
    {
108 18
        if (false === $this->has($id)) {
109 1
            throw new Exception\NotFoundException(\sprintf('Factory for class %s was not found', $id));
110
        }
111
112 17
        if (isset($this->aliases[$id])) {
113 9
            return new Factory\AliasFactory($this->aliases[$id]);
114
        }
115
116 17
        return new $this->factories[$id]();
117
    }
118
}
119