Passed
Pull Request — master (#1017)
by Maxim
12:07
created

ConfigChecker   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
eloc 14
c 0
b 0
f 0
dl 0
loc 32
ccs 16
cts 16
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B canInitialize() 0 25 9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Boot\BootloadManager\Checker;
6
7
use Spiral\Boot\Attribute\BootloadConfig;
8
use Spiral\Boot\Bootloader\BootloaderInterface;
9
use Spiral\Boot\EnvironmentInterface;
10
11
final class ConfigChecker implements BootloaderCheckerInterface
12
{
13 532
    public function __construct(
14
        private readonly EnvironmentInterface $environment,
15
    ) {
16 532
    }
17
18 532
    public function canInitialize(BootloaderInterface|string $bootloader, ?BootloadConfig $config = null): bool
19
    {
20 532
        if ($config === null) {
21 423
            return true;
22
        }
23
24 529
        if (!$config->enabled) {
25 4
            return false;
26
        }
27
28 528
        foreach ($config->denyEnv as $env => $denyValues) {
29 22
            $value = $this->environment->get($env);
30 22
            if ($value !== null && \in_array($value, (array) $denyValues, true)) {
31 17
                return false;
32
            }
33
        }
34
35 511
        foreach ($config->allowEnv as $env => $allowValues) {
36 14
            $value = $this->environment->get($env);
37 14
            if ($value === null || !\in_array($value, (array) $allowValues, true)) {
38 10
                return false;
39
            }
40
        }
41
42 501
        return true;
43
    }
44
}
45