Filter   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A pass() 0 14 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