Passed
Push — steps ( cace08...74de34 )
by Tom
02:51
created

StepsIterator::isManual()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 4
nc 4
nop 0
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 4
rs 10
c 1
b 0
f 1
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\File\Pipeline;
6
7
/**
8
 * Class StepsIterator
9
 *
10
 * @package Ktomk\Pipelines\File\Pipeline
11
 */
12
class StepsIterator implements \Iterator
13
{
14
    /**
15
     * @var \Iterator
16
     */
17
    private $inner;
18
19
    /**
20
     * @var int
21
     */
22
    private $index;
23
24
    /**
25
     * @var Step
26
     */
27
    private $current;
28
29
    /**
30
     * @var bool override trigger: manual in iteration
31
     */
32
    private $noManual = false;
33
34
    /**
35
     * StepsIterator constructor.
36
     *
37
     * @param \Iterator $iterator
38
     */
39 3
    public function __construct(\Iterator $iterator)
40
    {
41 3
        $this->inner = $iterator;
42 3
    }
43
44
    /**
45
     * @return null|int
46
     */
47 1
    public function getIndex()
48
    {
49 1
        return $this->index;
50
    }
51
52
    /**
53
     * Index of the pipeline step being iterated
54
     *
55
     * Undefined behaviour if the iteration has not yet been
56
     * started (e.g. the iterator has not yet been rewound)
57
     *
58
     * @return int
59
     */
60 1
    public function getStepIndex()
61
    {
62 1
        return $this->current->getIndex();
63
    }
64
65
    /**
66
     * Iteration might stop at a manual step. If
67
     * that is the case, isManual() will be true
68
     * *after* the iteration.
69
     *
70
     * @return bool
71
     */
72 2
    public function isManual()
73
    {
74 2
        return 0 !== $this->index
75 2
            && !$this->noManual
76 2
            && $this->current()
77 2
            && $this->current->isManual();
78
    }
79
80
    /** @see \Iterator * */
81
82 2
    public function next()
83
    {
84 2
        $this->index++;
85 2
        $this->inner->next();
86 2
    }
87
88 2
    public function key()
89
    {
90 2
        return $this->inner->key();
91
    }
92
93
    /**
94
     * @return Step
95
     */
96 2
    public function current()
97
    {
98 2
        return $this->current = $this->inner->current();
99
    }
100
101 2
    public function valid()
102
    {
103 2
        if ($this->isManual()) {
104 1
            return false;
105
        }
106
107 2
        return $this->inner->valid();
108
    }
109
110 2
    public function rewind()
111
    {
112 2
        $this->index = 0;
113 2
        $this->inner->rewind();
114 2
    }
115
116
    /**
117
     * @param bool $noManual
118
     */
119 1
    public function setNoManual($noManual)
120
    {
121 1
        $this->noManual = (bool)$noManual;
122 1
    }
123
}
124