Completed
Push — master ( 803408...f4be61 )
by Nikola
01:44
created

AbstractFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 70%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 0
loc 63
ccs 14
cts 20
cp 0.7
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 12 1
createContainer() 0 1 ?
configure() 0 1 ?
A getConfig() 0 4 1
A getConfigServiceName() 0 4 1
B getDiConfig() 0 18 6
1
<?php
2
/**
3
 * This file is part of the PHP App Foundation package.
4
 *
5
 * Copyright (c) Nikola Posa
6
 *
7
 * For full copyright and license information, please refer to the LICENSE file,
8
 * located at the package root folder.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Foundation\Di\Container\Factory;
14
15
use Foundation\Config\Config;
16
17
/**
18
 * @author Nikola Posa <[email protected]>
19
 */
20
abstract class AbstractFactory implements FactoryInterface
21
{
22
    /**
23
     * @var Config
24
     */
25
    private $config;
26
27
    /**
28
     * @var string
29
     */
30
    private $diConfigKey;
31
32
    /**
33
     * @var string
34
     */
35
    private $configServiceName;
36
37 7
    public function __invoke(Config $config, string $diConfigKey = self::DEFAULT_DI_CONFIG_KEY, string $configServiceName = self::DEFAULT_CONFIG_SERVICE_NAME)
38
    {
39 7
        $this->config = $config;
40 7
        $this->diConfigKey = $diConfigKey;
41 7
        $this->configServiceName = $configServiceName;
42
43 7
        $container = $this->createContainer();
44
45 7
        $this->configure($container);
46
47 7
        return $container;
48
    }
49
50
    abstract protected function createContainer();
51
52
    abstract protected function configure($container);
53
54 7
    final protected function getConfig()
55
    {
56 7
        return $this->config;
57
    }
58
59 7
    final protected function getConfigServiceName()
60
    {
61 7
        return $this->configServiceName;
62
    }
63
64 7
    final protected function getDiConfig(string $type = null) : array
65
    {
66 7
        if (empty($this->config[$this->diConfigKey]) || !is_array($this->config[$this->diConfigKey])) {
67 7
            return [];
68
        }
69
70
        $diConfig = $this->config[$this->diConfigKey];
71
72
        if (null === $type) {
73
            return $diConfig;
74
        }
75
76
        if (empty($diConfig[$type]) || !is_array($diConfig[$type])) {
77
            return [];
78
        }
79
80
        return $diConfig[$type];
81
    }
82
}
83