Passed
Push — master ( 646380...d8c5da )
by Jesse
01:28
created

Stringifier   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 29
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A withCondition() 0 5 1
A __construct() 0 4 1
A extract() 0 11 2
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\EntityState\Internal;
4
5
use Stratadox\EntityState\PropertyState;
6
use Stratadox\Specification\Contract\Satisfiable;
7
8
final class Stringifier implements Extractor
9
{
10
    private $next;
11
    private $condition;
12
13
    private function __construct(Extractor $next, Satisfiable $condition)
14
    {
15
        $this->next = $next;
16
        $this->condition = $condition;
17
    }
18
19
    public static function withCondition(
20
        Satisfiable $constraint,
21
        Extractor $next
22
    ): Extractor {
23
        return new self($next, $constraint);
24
    }
25
26
    public function extract(
27
        ExtractionRequest $request,
28
        Extractor $baseExtractor = null
29
    ): array {
30
        if ($this->condition->isSatisfiedBy($request->value())) {
31
            return [PropertyState::with(
32
                (string) $request->objectName(),
33
                (string) $request->value()
34
            )];
35
        }
36
        return $this->next->extract($request, $baseExtractor);
37
    }
38
}
39