CompositeSpecification::isSatisfiedBy()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Larium\Specification;
6
7
class CompositeSpecification extends LeafSpecification
8
{
9
    private $specifications = [];
10
11 3
    public function __construct(Specification ...$specifications)
12
    {
13 3
        $this->specifications = $specifications;
14 3
    }
15
16 3
    public function isSatisfiedBy(Candidate $candidate): bool
17
    {
18 3
        foreach ($this->specifications as $specification) {
19 3
            if ($specification->isSatisfiedBy($candidate) === false) {
20 3
                return false;
21
            }
22
        }
23
24 2
        return true;
25
    }
26
27 1
    public function remainderUnsatisfiedBy(Candidate $candidate): ?Specification
28
    {
29 1
        if ($this->isSatisfiedBy($candidate)) {
30
            return null;
31
        }
32
33 1
        $unsatisfied = [];
34 1
        foreach ($this->specifications as $specification) {
35 1
            if (!$specification->isSatisfiedBy($candidate)) {
36 1
                $unsatisfied[] = $specification;
37
            }
38
        }
39
40 1
        return new static(...$unsatisfied);
41
    }
42
}
43