Passed
Push — master ( 3491bf...0603fe )
by Jean Paul
01:15
created

Pipeline::execute()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 8
c 0
b 0
f 0
nc 7
nop 0
dl 0
loc 14
rs 9.2222
1
<?php
2
3
namespace Coco\SourceWatcher\Core;
4
5
use Iterator;
6
7
/**
8
 * Class Pipeline
9
 * @package Coco\SourceWatcher\Core
10
 */
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->extractor could return the type null which is incompatible with the type-hinted return Coco\SourceWatcher\Core\Extractor. Consider adding an additional type-check to rule them out.
Loading history...
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();
0 ignored issues
show
Bug introduced by
The method extract() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
        /** @scrutinizer ignore-call */ 
71
        $this->results = $this->extractor->extract();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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 ()
111
    {
112
        return $this->index;
113
    }
114
115
    public function valid ()
116
    {
117
        return isset( $this->results[$this->key()] );
118
    }
119
120
    public function rewind ()
121
    {
122
        $this->index = 0;
123
    }
124
}
125