Completed
Push — master ( 13ce18...d30243 )
by Oleg
05:24
created

PipelineBuilder::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DataFlow;
5
6
use SlayerBirden\DataFlow\Pipe\Copy;
7
use SlayerBirden\DataFlow\Pipe\Delete;
8
use SlayerBirden\DataFlow\Pipe\Filter;
9
use SlayerBirden\DataFlow\Pipe\FilterCallbackInterface;
10
use SlayerBirden\DataFlow\Pipe\Map;
11
use SlayerBirden\DataFlow\Pipe\MapperCallbackInterface;
12
use SlayerBirden\DataFlow\Pipe\Swap;
13
14
class PipelineBuilder implements PipelineBuilderInterface
15
{
16
    /**
17
     * @var PipeLineInterface
18
     */
19
    private $pipeline;
20
21
    private $pipesCount = 0;
22
    /**
23
     * @var EmitterInterface
24
     */
25
    private $emitter;
26
27 5
    public function __construct(EmitterInterface $emitter)
28
    {
29 5
        $this->pipeline = new PipeLine();
30 5
        $this->emitter = $emitter;
31 5
    }
32
33
    /**
34
     * Add arbitrary (not pre-defined) section to pipeline.
35
     *
36
     * @param PipeInterface $handler
37
     * @param int $priority
38
     * @return PipelineBuilder
39
     */
40 5
    public function addSection(PipeInterface $handler, int $priority = 0): PipelineBuilder
41
    {
42 5
        $this->pipeline->insert($handler, $priority);
43 5
        $this->pipesCount++;
44 5
        return $this;
45
    }
46
47 4
    public function map(string $field, MapperCallbackInterface $callback, ?string $id = null): PipelineBuilder
48
    {
49 4
        if (!$id) {
50 4
            $id = 'mapper' . $this->pipesCount++ . '-' . $field;
51
        }
52 4
        return $this->addSection(new Map($id, $field, $callback));
53
    }
54
55 3
    public function filter(FilterCallbackInterface $callback, ?string $id = null): PipelineBuilder
56
    {
57 3
        if (!$id) {
58 3
            $id = 'filter' . $this->pipesCount++ . '-' . get_class($callback);
59
        }
60 3
        return $this->addSection(new Filter($id, $callback));
61
    }
62
63 3
    public function delete(array $names, ?string $id = null): PipelineBuilder
64
    {
65 3
        if (!$id) {
66 3
            $id = 'delete' . $this->pipesCount++ . '-' . json_encode($names);
67
        }
68 3
        return $this->addSection(new Delete($id, ...$names));
69
    }
70
71
    public function swap(string $first, string $second, ?string $id = null): PipelineBuilder
72
    {
73
        if (!$id) {
74
            $id = 'swap' . $this->pipesCount++ . '-' . $first . '-' . $second;
75
        }
76
        return $this->addSection(new Swap($id, $first, $second));
77
    }
78
79 1
    public function cp(string $from, string $to, ?string $id = null): PipelineBuilder
80
    {
81 1
        if (!$id) {
82 1
            $id = 'copy' . $this->pipesCount++ . '-' . $from . '-' . $to;
83
        }
84 1
        return $this->addSection(new Copy($id, $from, $to));
85
    }
86
87 5
    public function build(): PipeLineInterface
88
    {
89 5
        return $this->pipeline;
90
    }
91
}
92