Passed
Push — steps ( c7b5f4...d19fba )
by Tom
02:52
created

Pipeline::setStepsExpression()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\File;
6
7
use Ktomk\Pipelines\File\Pipeline\Step;
8
use Ktomk\Pipelines\File\Pipeline\Steps;
9
use Ktomk\Pipelines\Value\StepExpression;
10
11
class Pipeline
12
{
13
    /**
14
     * @var File
15
     */
16
    private $file;
17
18
    /**
19
     * @var Steps
20
     */
21
    private $steps;
22
23
    /**
24
     * Pipeline constructor.
25
     *
26
     * @param File $file
27
     * @param array $definition
28
     *
29
     * @throws \Ktomk\Pipelines\File\ParseException
30
     */
31 6
    public function __construct(File $file, array $definition)
32
    {
33
        // quick validation
34 6
        if (!isset($definition[0])) {
35 1
            ParseException::__('Pipeline requires a list of steps');
36
        }
37
38 6
        $this->file = $file;
39 6
        $this->steps = new Steps($this, $definition);
40 5
    }
41
42
    /**
43
     * @return File
44
     */
45 2
    public function getFile()
46
    {
47 2
        return $this->file;
48
    }
49
50
    /**
51
     * get id of pipeline within the corresponding file object
52
     *
53
     * @return null|string id, can be null in fake/test conditions
54
     */
55 1
    public function getId()
56
    {
57 1
        return $this->file->getIdFrom($this);
58
    }
59
60
    /**
61
     * @param string $expression [optional]
62
     */
63 1
    public function setStepsExpression($expression = null)
64
    {
65 1
        if (null === $expression) {
66 1
            return;
67
        }
68
69
        // parse, resolve early
70 1
        $array = StepExpression::createFromString($expression)
71 1
            ->resolveSteps($this->getSteps());
72
73 1
        $this->steps->setGetIteratorFunctor(function (Steps $steps) use ($array) {
0 ignored issues
show
Unused Code introduced by
The parameter $steps is not used and could be removed. ( Ignorable by Annotation )

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

73
        $this->steps->setGetIteratorFunctor(function (/** @scrutinizer ignore-unused */ Steps $steps) use ($array) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
74 1
            return new \ArrayIterator($array);
75 1
        });
76 1
    }
77
78
    /**
79
     * @return Step[]|Steps
80
     */
81 2
    public function getSteps()
82
    {
83 2
        return $this->steps;
84
    }
85
86
    /**
87
     * Specify data which should be serialized to JSON
88
     *
89
     * @return array
90
     * @since 5.4.0
91
     */
92 1
    public function jsonSerialize()
93
    {
94 1
        return $this->steps->jsonSerialize();
95
    }
96
}
97