FileSettingsProvider   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 52
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 11 2
B checkDoNotHaveRequiredParametersOnCreate() 0 21 7
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Application\Settings;
4
5
/**
6
 * Copyright 2015-2020 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Limoncello\Application\Exceptions\InvalidSettingsClassException;
22
use Limoncello\Common\Reflection\ClassIsTrait;
23
use Limoncello\Contracts\Settings\SettingsInterface;
24
use ReflectionClass;
25
use ReflectionException;
26
use function assert;
27
28
/**
29
 * @package Limoncello\Application
30
 */
31
class FileSettingsProvider extends InstanceSettingsProvider
32
{
33
    use ClassIsTrait;
34
35
    /**
36
     * @param string $path
37
     *
38
     * @return FileSettingsProvider
39 9
     *
40
     * @throws ReflectionException
41 9
     */
42 8
    public function load(string $path): FileSettingsProvider
43
    {
44 8
        foreach ($this->selectClasses($path, SettingsInterface::class) as $settingsClass) {
45 8
            assert($this->checkDoNotHaveRequiredParametersOnCreate($settingsClass));
46
            /** @var SettingsInterface $settings */
47
            $settings = new $settingsClass();
48 9
            $this->register($settings);
49
        }
50
51
        return $this;
52
    }
53
54
    /**
55
     * @param string $className
56 11
     *
57
     * @return bool
58
     *
59 11
     * @SuppressWarnings(PHPMD.IfStatementAssignment)
60 10
     */
61 1
    private function checkDoNotHaveRequiredParametersOnCreate(string $className): bool
62
    {
63 9
        try {
64 9
            $reflection  = new ReflectionClass($className);
65 9
            if ($reflection->isInstantiable() === false) {
66
                throw new InvalidSettingsClassException($className);
67 9
            }
68
            if (($constructor = $reflection->getConstructor()) !== null) {
69
                foreach ($constructor->getParameters() as $parameter) {
70
                    if ($parameter->isOptional() === false && $parameter->isDefaultValueAvailable() === false) {
71 3
                        // there is no default constructor
72 1
                        throw new InvalidSettingsClassException($className);
73
                    }
74
                }
75 8
            }
76
        } catch (ReflectionException $exception) {
77
            throw new InvalidSettingsClassException($className, 0, $exception);
78
        }
79
80
        return true;
81
    }
82
}
83