Passed
Push — bugfix/support-windows ( 517fb4...0e99e7 )
by Chema
04:47
created

AbstractFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 59
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getServicesToExtend() 0 5 1
A resolveDependencyProvider() 0 3 1
A getProvidedDependency() 0 3 1
A getContainer() 0 9 2
A createContainerWithProvidedDependencies() 0 7 1
A resetCache() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework;
6
7
use Closure;
8
use Gacela\Framework\ClassResolver\DependencyProvider\DependencyProviderResolver;
9
use Gacela\Framework\Config\Config;
10
use Gacela\Framework\Container\Container;
11
use Gacela\Framework\Container\Exception\ContainerKeyNotFoundException;
12
13
abstract class AbstractFactory
14
{
15
    use ConfigResolverAwareTrait;
16
17
    /** @var array<string,Container> */
18
    private static array $containers = [];
19
20
    /**
21
     * @internal
22
     */
23 45
    public static function resetCache(): void
24
    {
25 45
        self::$containers = [];
26
    }
27
28
    /**
29
     * @throws ContainerKeyNotFoundException
30
     */
31 20
    protected function getProvidedDependency(string $key): mixed
32
    {
33 20
        return $this->getContainer()->get($key);
34
    }
35
36 20
    private function getContainer(): Container
37
    {
38 20
        $containerKey = static::class;
39
40 20
        if (!isset(self::$containers[$containerKey])) {
41 12
            self::$containers[$containerKey] = $this->createContainerWithProvidedDependencies();
42
        }
43
44 19
        return self::$containers[$containerKey];
45
    }
46
47 12
    private function createContainerWithProvidedDependencies(): Container
48
    {
49 12
        $container = new Container($this->getServicesToExtend());
50 12
        $dependencyProvider = $this->resolveDependencyProvider();
51 11
        $dependencyProvider->provideModuleDependencies($container);
52
53 11
        return $container;
54
    }
55
56
    /**
57
     * @return array<string,list<Closure>>
58
     */
59 12
    private function getServicesToExtend(): array
60
    {
61 12
        return Config::getInstance()
62 12
            ->getSetupGacela()
63 12
            ->getServicesToExtend();
64
    }
65
66
    /**
67
     * @throws ClassResolver\DependencyProvider\DependencyProviderNotFoundException
68
     */
69 12
    private function resolveDependencyProvider(): AbstractDependencyProvider
70
    {
71 12
        return (new DependencyProviderResolver())->resolve($this);
72
    }
73
}
74