Total Complexity | 17 |
Total Lines | 112 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
11 | class Pipeline implements Iterator |
||
12 | { |
||
13 | /** |
||
14 | * @var Extractor |
||
15 | */ |
||
16 | private ?Extractor $extractor = null; |
||
17 | |||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | private array $steps = []; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | private array $results = []; |
||
27 | |||
28 | /** |
||
29 | * @return Extractor |
||
30 | */ |
||
31 | public function getExtractor () : Extractor |
||
32 | { |
||
33 | return $this->extractor; |
||
|
|||
34 | } |
||
35 | |||
36 | /** |
||
37 | * @param Extractor $extractor |
||
38 | */ |
||
39 | public function setExtractor ( Extractor $extractor ) : void |
||
40 | { |
||
41 | $this->extractor = $extractor; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @return array |
||
46 | */ |
||
47 | public function getSteps () : array |
||
48 | { |
||
49 | return $this->steps; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @param array $steps |
||
54 | */ |
||
55 | public function setSteps ( array $steps ) : void |
||
56 | { |
||
57 | $this->steps = $steps; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param Step $step |
||
62 | */ |
||
63 | public function pipe ( Step $step ) : void |
||
64 | { |
||
65 | $this->steps[] = $step; |
||
66 | } |
||
67 | |||
68 | public function execute () : void |
||
69 | { |
||
70 | $this->results = $this->extractor->extract(); |
||
71 | |||
72 | foreach ( $this->steps as $currentStep ) { |
||
73 | if ( $currentStep instanceof Transformer ) { |
||
74 | foreach ( $this->results as $currentIndex => $currentItem ) { |
||
75 | $currentStep->transform( $this->results[$currentIndex] ); |
||
76 | } |
||
77 | } |
||
78 | |||
79 | if ( $currentStep instanceof Loader ) { |
||
80 | foreach ( $this->results as $currentIndex => $currentItem ) { |
||
81 | $currentStep->load( $currentItem ); |
||
82 | } |
||
83 | } |
||
84 | } |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @return array |
||
89 | */ |
||
90 | public function getResults () : array |
||
91 | { |
||
92 | return $this->results; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @var int |
||
97 | */ |
||
98 | private int $index = 0; |
||
99 | |||
100 | public function current () |
||
101 | { |
||
102 | return $this->results[$this->index]; |
||
103 | } |
||
104 | |||
105 | public function next () |
||
106 | { |
||
107 | $this->index++; |
||
108 | } |
||
109 | |||
110 | public function key () |
||
113 | } |
||
114 | |||
115 | public function valid () |
||
116 | { |
||
117 | return isset( $this->results[$this->key()] ); |
||
118 | } |
||
119 | |||
120 | public function rewind () |
||
123 | } |
||
124 | } |
||
125 |