Filter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
ccs 4
cts 4
cp 1
crap 1
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