Passed
Push — master ( 6b5525...2206a3 )
by Dominik
02:24
created

ConstraintViolationDetector::setBlocklist()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Dominikb\ComposerLicenseChecker;
6
7
use Dominikb\ComposerLicenseChecker\Contracts\LicenseConstraintHandler;
8
use Dominikb\ComposerLicenseChecker\Exceptions\LogicException;
9
10
class ConstraintViolationDetector implements LicenseConstraintHandler
11
{
12
    /** @var string[] */
13
    protected $blocklist = [];
14
15
    /** @var string[] */
16
    protected $allowlist = [];
17
18 3
    public function setBlocklist(array $licenses): void
19
    {
20 3
        $this->blocklist = $licenses;
21 3
    }
22
23 3
    public function setAllowlist(array $licenses): void
24
    {
25 3
        $this->allowlist = $licenses;
26 3
    }
27
28
    /**
29
     * @param Dependency[] $dependencies
30
     *
31
     * @return ConstraintViolation[]
32
     * @throws LogicException
33
     */
34 5
    public function detectViolations(array $dependencies): array
35
    {
36 5
        $this->ensureConfigurationIsValid();
37
38
        return [
39 4
            $this->detectBlocklistViolation($dependencies),
40 4
            $this->detectAllowlistViolation($dependencies),
41
        ];
42
    }
43
44
    /**
45
     * @throws LogicException
46
     */
47 5
    public function ensureConfigurationIsValid(): void
48
    {
49 5
        $overlap = array_intersect($this->blocklist, $this->allowlist);
50
51 5
        if (count($overlap) > 0) {
52 1
            $invalidLicenseConditionals = sprintf('"%s"', implode('", "', $overlap));
53 1
            throw new LogicException("Licenses must not be on the block- and allowlist at the same time: ${invalidLicenseConditionals}");
54
        }
55 4
    }
56
57
    /**
58
     * @param Dependency[] $dependencies
59
     */
60 4
    private function detectBlocklistViolation(array $dependencies): ConstraintViolation
61
    {
62 4
        $violation = new ConstraintViolation('Blocked license found!');
63
64 4
        if (! empty($this->blocklist)) {
65 2
            foreach ($dependencies as $dependency) {
66 2
                if ($this->allLicensesOnList($dependency->getLicenses(), $this->blocklist)) {
67 2
                    $violation->add($dependency);
68
                }
69
            }
70
        }
71
72 4
        return $violation;
73
    }
74
75
    /**
76
     * @param Dependency[] $dependencies
77
     */
78 4
    private function detectAllowlistViolation(array $dependencies): ConstraintViolation
79
    {
80 4
        $violation = new ConstraintViolation('Unallowed license found!');
81
82 4
        if (! empty($this->allowlist)) {
83 2
            foreach ($dependencies as $dependency) {
84 2
                if (! $this->anyLicenseOnList($dependency->getLicenses(), $this->allowlist)) {
85 2
                    $violation->add($dependency);
86
                }
87
            }
88
        }
89
90 4
        return $violation;
91
    }
92
93 2
    private function allLicensesOnList(array $licenses, array $list): bool
94
    {
95 2
        return count(array_intersect($licenses, $list)) === count($licenses);
96
    }
97
98 2
    private function anyLicenseOnList(array $licenses, array $list): bool
99
    {
100 2
        return count(array_intersect($licenses, $list)) > 0;
101
    }
102
}
103