Passed
Push — master ( becae5...2d4555 )
by Jean Paul
12:24
created

Pipeline::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Coco\SourceWatcher\Core;
4
5
use Coco\SourceWatcher\Core\Extractors\ExecutionExtractor;
6
use Coco\SourceWatcher\Core\IO\Inputs\ExtractorResultInput;
7
use Coco\SourceWatcher\Utils\FileUtils;
8
use Exception;
9
use Iterator;
10
use Monolog\Handler\StreamHandler;
11
use Monolog\Logger;
12
13
/**
14
 * Class Pipeline
15
 *
16
 * @package Coco\SourceWatcher\Core
17
 */
18
class Pipeline implements Iterator
19
{
20
    private array $steps = [];
21
22
    private array $results = [];
23
24
    private Logger $logger;
25
26
    public function __construct ()
27
    {
28
        $this->logger = new Logger( "Connector" );
29
30
        $streamPath = FileUtils::file_build_path( __DIR__, "..", "..", "..", "..", "logs",
31
            "Connector" . "-" . gmdate( "Y-m-d-H-i-s", time() ) . "-" . getmypid() . ".txt" );
32
33
        $this->logger->pushHandler( new StreamHandler( $streamPath ), Logger::DEBUG );
0 ignored issues
show
Unused Code introduced by
The call to Monolog\Logger::pushHandler() has too many arguments starting with Monolog\Logger::DEBUG. ( Ignorable by Annotation )

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

33
        $this->logger->/** @scrutinizer ignore-call */ 
34
                       pushHandler( new StreamHandler( $streamPath ), Logger::DEBUG );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
34
    }
35
36
    public function getSteps () : array
37
    {
38
        return $this->steps;
39
    }
40
41
    public function setSteps ( array $steps ) : void
42
    {
43
        $this->steps = $steps;
44
    }
45
46
    public function pipe ( Step $step ) : void
47
    {
48
        if ( $step instanceof ExecutionExtractor ) {
49
            $step->setInput( new ExtractorResultInput( end( $this->steps ) ) );
50
        }
51
52
        $this->steps[] = $step;
53
    }
54
55
    public function execute () : void
56
    {
57
        foreach ( $this->steps as $index => $currentStep ) {
58
            if ( $currentStep instanceof Extractor ) {
59
                $this->results = $currentStep->extract();
60
            }
61
62
            if ( $currentStep instanceof Transformer ) {
63
                foreach ( $this->results as $currentIndex => $currentItem ) {
64
                    $currentStep->transform( $this->results[$currentIndex] );
65
                }
66
            }
67
68
            if ( $currentStep instanceof Loader ) {
69
                foreach ( $this->results as $currentIndex => $currentItem ) {
70
                    try {
71
                        $currentStep->load( $currentItem );
72
                    } catch ( Exception $exception ) {
73
                        $this->logger->debug( $exception->getMessage() );
74
                    }
75
                }
76
            }
77
        }
78
    }
79
80
    public function getResults () : array
81
    {
82
        return $this->results;
83
    }
84
85
    private int $index = 0;
86
87
    public function current ()
88
    {
89
        return $this->results[$this->index];
90
    }
91
92
    public function next ()
93
    {
94
        $this->index++;
95
    }
96
97
    public function key ()
98
    {
99
        return $this->index;
100
    }
101
102
    public function valid ()
103
    {
104
        return isset( $this->results[$this->key()] );
105
    }
106
107
    public function rewind ()
108
    {
109
        $this->index = 0;
110
    }
111
}
112