Completed
Push — PSR-11-2 ( a5ad88...7f5041 )
by Nikolaos
03:59
created

ConfigFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.0156

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 4
cp 0.75
rs 10
cc 1
nc 1
nop 1
crap 1.0156
1
<?php
2
3
/**
4
 * This file is part of the Phalcon Framework.
5
 *
6
 * (c) Phalcon Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Phalcon\Config;
15
16
use Phalcon\Config;
17
use Phalcon\Factory\AbstractFactory;
18
use Phalcon\Factory\Exception as FactoryException;
19
use Phalcon\Helper\Arr;
20
21
use function is_array;
22
use function is_object;
23
use function is_string;
24
use function lcfirst;
25
use function pathinfo;
26
use function strpos;
27
use function strtolower;
28
29
use const PATHINFO_EXTENSION;
30
31
class ConfigFactory extends AbstractFactory
32
{
33
    /**
34
     * ConfigFactory constructor.
35
     *
36
     * @param array $services
37
     */
38 21
    public function __construct(array $services = [])
39
    {
40 21
        $this->init($services);
41 21
    }
42
43
    /**
44
     * Load a config to create a new instance
45
     *
46
     * @param string|array|Config $config          = [
47
     *                                             'adapter' => 'ini',
48
     *                                             'filePath' => 'config.ini',
49
     *                                             'mode' => null,
50
     *                                             'callbacks' => null
51
     *                                             ]
52
     *
53
     * @return object
54
     * @throws Exception
55
     * @throws FactoryException
56
     */
57 20
    public function load($config): object
58
    {
59 20
        if (is_string($config)) {
60 4
            $oldConfig = $config;
61 4
            $extension = pathinfo($config, PATHINFO_EXTENSION);
62
63 4
            $this->checkOptionsExtension($extension);
64
65
            $config = [
66 3
                "adapter"  => $extension,
67 3
                "filePath" => $oldConfig,
68
            ];
69
        }
70
71 20
        if (is_object($config) && $config instanceof Config) {
0 ignored issues
show
Bug introduced by
The class Phalcon\Config does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
72 1
            $config = $config->toArray();
73
        }
74
75
        $this
76 20
            ->checkOptionsIsArray($config)
77 20
            ->checkOptionsFilePath($config)
78 20
            ->checkOptionsAdapter($config)
79
        ;
80
81 19
        $adapter = strtolower($config["adapter"]);
82 19
        $first   = $config["filePath"];
83
84 19
        if (!strpos($first, ".")) {
85 2
            $first = $first . "." . lcfirst($adapter);
86
        }
87
88 19
        $second = $this->checkSecond($config, $adapter);
89
90 19
        return $this->newInstance($adapter, $first, $second);
91
    }
92
93
    /**
94
     * Returns a new Config instance
95
     *
96
     * @param string     $name
97
     * @param string     $fileName
98
     * @param null|array $params
99
     *
100
     * @return object
101
     * @throws FactoryException
102
     */
103 20
    public function newInstance(
104
        string $name,
105
        string $fileName,
106
        $params = null
107
    ): object {
108 20
        $this->checkService($name);
109
110 20
        $definition = $this->mapper[$name];
111 20
        $options    = [];
112 20
        $options[]  = $fileName;
113
114 20
        if ("json" !== $name && "php" !== $name) {
115 5
            $options[] = $params;
116
        }
117
118 20
        return new $definition(...$options);
119
    }
120
121
    /**
122
     * Returns the adapters for the factory
123
     */
124 21
    protected function getAdapters(): array
125
    {
126
        return [
127 21
            "grouped" => "Phalcon\\Config\\Adapter\\Grouped",
128
            "ini"     => "Phalcon\\Config\\Adapter\\Ini",
129
            "json"    => "Phalcon\\Config\\Adapter\\Json",
130
            "php"     => "Phalcon\\Config\\Adapter\\Php",
131
            "yaml"    => "Phalcon\\Config\\Adapter\\Yaml",
132
        ];
133
    }
134
135
    /**
136
     * @param mixed $extension
137
     *
138
     * @throws Exception
139
     */
140 4
    private function checkOptionsExtension($extension): void
141
    {
142 4
        if (empty($extension)) {
143 1
            throw new Exception(
144 1
                "You need to provide the extension in the file path"
145
            );
146
        }
147 3
    }
148
149
    /**
150
     * @param mixed $config
151
     *
152
     * @return ConfigFactory
153
     * @throws Exception
154
     */
155 20
    private function checkOptionsIsArray($config): ConfigFactory
156
    {
157 20
        if (!is_array($config)) {
158 1
            throw new Exception(
159 1
                "Config must be array or Phalcon\\Config object"
160
            );
161
        }
162
163 20
        return $this;
164
    }
165
166
    /**
167
     * @param array $config
168
     *
169
     * @return ConfigFactory
170
     * @throws Exception
171
     */
172 20
    private function checkOptionsFilePath(array $config): ConfigFactory
173
    {
174 20
        if (!isset($config["filePath"])) {
175 1
            throw new Exception(
176 1
                "You must provide 'filePath' option in factory config parameter."
177
            );
178
        }
179
180 20
        return $this;
181
    }
182
183
    /**
184
     * @param array $config
185
     *
186
     * @return ConfigFactory
187
     * @throws Exception
188
     */
189 20
    private function checkOptionsAdapter(array $config): ConfigFactory
190
    {
191 20
        if (!isset($config["adapter"])) {
192 1
            throw new Exception(
193 1
                "You must provide 'adapter' option in factory config parameter."
194
            );
195
        }
196
197 19
        return $this;
198
    }
199
200
    /**
201
     * @param array  $config
202
     * @param string $adapter
203
     *
204
     * @return mixed|null
205
     */
206 19
    private function checkSecond(array $config, string $adapter)
207
    {
208 19
        $second = null;
209
210 19
        if ("ini" === $adapter) {
211 3
            $second = Arr::get($config, "mode", 1);
212 16
        } elseif ("yaml" === $adapter) {
213 1
            $second = Arr::get($config, "callbacks", []);
214
        }
215
216 19
        return $second;
217
    }
218
}
219