ConfigValidator::checkIfEmpty()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 3
nc 2
nop 1
1
<?php
2
/**
3
 * @copyright 2018 Aleksander Stelmaczonek <[email protected]>
4
 * @license   MIT License, see license file distributed with this source code
5
 */
6
7
namespace Koriit\PHPDeps\Config;
8
9
use Koriit\PHPDeps\Config\Exceptions\InvalidConfig;
10
11
class ConfigValidator
12
{
13
    /**
14
     * @param Config $config
15
     *
16
     * @throws InvalidConfig
17
     */
18
    public function check(Config $config)
19
    {
20
        $this->checkIfEmpty($config);
21
22
        $this->checkNameDuplication($config);
23
    }
24
25
    /**
26
     * @param Config $config
27
     *
28
     * @throws InvalidConfig
29
     */
30
    private function checkIfEmpty(Config $config)
31
    {
32
        if (empty($config->getModules()) && empty($config->getModuleDetectors())) {
33
            throw new InvalidConfig('Configuration cannot be empty');
34
        }
35
    }
36
37
    /**
38
     * @param Config $config
39
     *
40
     * @throws InvalidConfig
41
     */
42
    private function checkNameDuplication(Config $config)
43
    {
44
        $modules = [];
45
        foreach ($config->getModules() as $module) {
46
            $modules[] = $module->getName();
47
        }
48
49
        if (\count($modules) != \count(\array_unique($modules))) {
50
            throw new InvalidConfig('Two or more of your configured modules have the same name');
51
        }
52
    }
53
}
54