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

AbstractFactory::getConfigServiceName()   A

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
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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