Plumber::pour()   A
last analyzed

Complexity

Conditions 5
Paths 11

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.0592

Importance

Changes 0
Metric Value
cc 5
nc 11
nop 0
dl 0
loc 21
ccs 13
cts 15
cp 0.8667
crap 5.0592
rs 9.2728
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DataFlow;
5
6
use SlayerBirden\DataFlow\Exception\FlowTerminationException;
7
8
class Plumber
9
{
10
    /**
11
     * @var ProviderInterface
12
     */
13
    private $source;
14
    /**
15
     * @var PipeLineInterface
16
     */
17
    private $pipeLine;
18
    /**
19
     * @var EmitterInterface
20
     */
21
    private $emitter;
22
23 4
    public function __construct(ProviderInterface $source, PipeLineInterface $pipeLine, EmitterInterface $emitter)
24
    {
25 4
        $this->source = $source;
26 4
        $this->pipeLine = $pipeLine;
27 4
        $this->emitter = $emitter;
28 4
    }
29
30
    /**
31
     * Pour source into pipeline.
32
     */
33 4
    public function pour(): void
34
    {
35 4
        $provider = $this->source->getCask();
36 4
        foreach ($provider as $dataBag) {
37 4
            if ($dataBag instanceof \Throwable) {
38
                $this->emitter->emit('provider_error', $dataBag->getMessage());
39
                continue;
40
            }
41
            try {
42 4
                $this->pipeLine->rewind();
43 4
                while ($this->pipeLine->valid()) {
44 4
                    $handler = $this->pipeLine->current();
45 4
                    $dataBag = $handler->pass($dataBag);
46 4
                    $this->pipeLine->next();
47
                }
48 2
            } catch (FlowTerminationException $exception) {
49 4
                $this->emitter->emit('valve_closed', $exception->getIdentifier(), $dataBag);
50
            }
51
        }
52 4
        $this->emitter->emit('empty_cask');
53 4
    }
54
}
55