PropertySetConfig   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 15
c 4
b 0
f 0
lcom 2
cbo 1
dl 0
loc 132
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getConfig() 0 4 1
A getInstance() 0 8 2
B init() 0 38 6
A getUserConfig() 0 4 1
A getArgs() 0 8 2
A getClassName() 0 9 2
1
<?php
2
/**
3
 * @link https://github.com/old-town/old-town-propertyset
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace OldTown\PropertySet\Config;
7
8
use OldTown\PropertySet\Exception\InvalidArgumentException;
9
10
/**
11
 * Class PropertySetConfig
12
 *
13
 * @package OldTown\PropertySet\Config
14
 */
15
class PropertySetConfig
16
{
17
    /**
18
     * @var PropertySetConfig
19
     */
20
    protected static $instance;
21
22
    /**
23
     * @var array
24
     */
25
    protected $propertySetArgs;
26
27
    /**
28
     * @var array
29
     */
30
    protected $propertySets;
31
32
    /**
33
     *
34
     * @throws \OldTown\PropertySet\Exception\InvalidArgumentException
35
     */
36
    protected function __construct()
37
    {
38
        $this->init();
39
    }
40
41
    /**
42
     *
43
     * @return PropertySetConfig
44
     */
45
    public static function getConfig()
46
    {
47
        return self::getInstance();
48
    }
49
50
    /**
51
     *
52
     * @return PropertySetConfig
53
     */
54
    public static function getInstance()
55
    {
56
        if (null === self::$instance) {
57
            self::$instance = new self();
58
        }
59
60
        return self::$instance;
61
    }
62
63
    /**
64
     *
65
     * @return $this
66
     *
67
     * @throws \OldTown\PropertySet\Exception\InvalidArgumentException
68
     */
69
    protected function init()
70
    {
71
        $defaultConfig = include __DIR__ . '/../../config/propertyset.default.config.php';
72
        $userConfig = $this->getUserConfig();
73
74
        $config = array_merge_recursive($defaultConfig, $userConfig);
75
76
        foreach ($config as $name => $data) {
77
            if (!is_array($data)) {
78
                $errMsg = sprintf(
79
                    'Некорректный формат данных для сервиса %s',
80
                    $name
81
                );
82
                throw new InvalidArgumentException($errMsg);
83
            }
84
85
86
            if (!array_key_exists('class', $data)) {
87
                $errMsg = sprintf(
88
                    'Отсутствует имя класса для сервиса %s',
89
                    $name
90
                );
91
                throw new InvalidArgumentException($errMsg);
92
            }
93
94
95
96
            $this->propertySets[$name] = (string)$data['class'];
97
98
99
            $flagHasArgs = array_key_exists('args', $data) && is_array($data['args']);
100
            $this->propertySetArgs[$name] = $flagHasArgs  ? $data['args'] : [];
101
        }
102
103
104
105
        return $this;
106
    }
107
108
    /**
109
     * @todo Реализовать возможность добавления пользователем конфигов
110
     *
111
     * @return array
112
     */
113
    protected function getUserConfig()
114
    {
115
        return [];
116
    }
117
118
    /**
119
     * @param $name
120
     *
121
     * @return array|null
122
     */
123
    public function getArgs($name)
124
    {
125
        $name = (string)$name;
126
        if (array_key_exists($name, $this->propertySetArgs)) {
127
            return $this->propertySetArgs[$name];
128
        }
129
        return null;
130
    }
131
132
    /**
133
     * @param $name
134
     *
135
     * @return string|null
136
     */
137
    public function getClassName($name)
138
    {
139
        $name = (string)$name;
140
        if (array_key_exists($name, $this->propertySets)) {
141
            return $this->propertySets[$name];
142
        }
143
144
        return null;
145
    }
146
}
147