Filter::pass()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
ccs 8
cts 8
cp 1
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DataFlow\Pipe;
5
6
use SlayerBirden\DataFlow\DataBagInterface;
7
use SlayerBirden\DataFlow\Exception\FlowTerminationException;
8
use SlayerBirden\DataFlow\PipeInterface;
9
use SlayerBirden\DataFlow\IdentificationTrait;
10
11
class Filter implements PipeInterface
12
{
13
    use IdentificationTrait;
14
    /**
15
     * @var FilterCallbackInterface
16
     */
17
    private $callback;
18
    /**
19
     * @var string
20
     */
21
    private $identifier;
22
23 3
    public function __construct(string $identifier, FilterCallbackInterface $callback)
24
    {
25 3
        $this->identifier = $identifier;
26 3
        $this->callback = $callback;
27 3
    }
28
29
    /**
30
     * Filtering handler.
31
     * Terminate data flow based on a callback applied to DataBag.
32
     *
33
     * {@inheritdoc}
34
     */
35 2
    public function pass(DataBagInterface $dataBag): DataBagInterface
36
    {
37 2
        if (!($this->callback)($dataBag)) {
38 2
            throw new FlowTerminationException(
39 2
                sprintf(
40 2
                    'Flow was terminated by Filter (%s) for data %s.',
41 2
                    $this->getIdentifier(),
42 2
                    json_encode($dataBag)
43
                )
44
            );
45
        }
46
47 2
        return $dataBag;
48
    }
49
}
50