Failed Conditions
Pull Request — master (#11)
by jean
12:30
created

ProcessState::getData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: darkilliant
5
 * Date: 5/8/18
6
 * Time: 9:47 AM.
7
 */
8
9
namespace Darkilliant\ProcessBundle\State;
10
11
use Psr\Log\AbstractLogger;
12
use Psr\Log\LoggerInterface;
13
use Darkilliant\ProcessBundle\Runner\StepRunner;
14
15
class ProcessState extends AbstractLogger
16
{
17
    const RESULT_KO = 1;
18
    const RESULT_SKIP = 2;
19
    const RESULT_OK = 3;
20
21
    private $data;
22
    private $context;
23
    private $options = [];
24
    private $logger;
25
    private $result;
26
27
    private $dryRun = false;
28
29
    /** @var StepRunner */
30
    private $stepRunner;
31 54
32
    /** @var \Traversable */
33 54
    private $iterator;
34 54
35 54
    private $loop;
36 54
37
    public function __construct(array $context, LoggerInterface $logger, StepRunner $stepRunner)
38
    {
39
        $this->context = $context;
40
        $this->logger = $logger;
41 22
        $this->stepRunner = $stepRunner;
42
    }
43 22
44
    /**
45
     * @return mixed
46
     */
47
    public function getData()
48
    {
49 19
        return $this->data;
50
    }
51 19
52 19
    /**
53
     * @param mixed $data
54
     */
55
    public function setData($data)
56
    {
57 3
        $this->data = $data;
58
    }
59 3
60
    /**
61
     * @return mixed
62
     */
63
    public function getContext($key)
64
    {
65 5
        return $this->context[$key];
66
    }
67 5
68 5
    public function getLoop()
69
    {
70 6
        return $this->loop;
71
    }
72 6
73
    public function noLoop()
74
    {
75
        $this->loop = null;
76
    }
77
78 5
    public function loop(int $index, int $count, bool $last)
79
    {
80 5
        $this->loop = [
81
            'index' => $index,
82
            'count' => $count,
83
            'last' => $last,
84
        ];
85
    }
86 33
87
    public function isLoop()
88 33
    {
89
        return (bool) $this->loop;
90
    }
91
92
    /**
93
     * @param mixed $context
94 31
     */
95
    public function setContext($key, $value)
96 31
    {
97
        $this->context[$key] = $value;
98 31
    }
99
100
    public function getRawContext(): array
101 21
    {
102
        return $this->context;
103 21
    }
104 21
105
    /**
106 10
     * @return LoggerInterface
107
     */
108 10
    public function getLogger(): LoggerInterface
109
    {
110
        return $this->logger;
111 4
    }
112
113 4
    /**
114 4
     * @return array
115
     */
116 1
    public function getOptions(): array
117
    {
118 1
        return $this->options;
119 1
    }
120
121 7
    /**
122
     * @param array $options
123 7
     */
124 7
    public function setOptions(array $options): ProcessState
125
    {
126 3
        $this->options = $options;
127
128 3
        return $this;
129
    }
130
131 1
    public function log($level, $message, array $context = [])
132
    {
133 1
        $this->logger->log($level, $message, array_merge($context, $this->context));
134 1
    }
135
136 1
    public function getResult()
137
    {
138
        return $this->result;
139
    }
140
141
    public function markFail()
142 13
    {
143
        $this->result = self::RESULT_KO;
144 13
    }
145
146
    public function markIgnore()
147
    {
148
        $this->result = self::RESULT_SKIP;
149
    }
150 16
151
    public function markSuccess()
152 16
    {
153 16
        $this->result = self::RESULT_OK;
154
    }
155
156
    public function getStepRunner(): StepRunner
157
    {
158
        return $this->stepRunner;
159
    }
160
161
    public function duplicate($logger = null): self
162
    {
163
        $duplicate = new self($this->context, $logger ?? $this->logger, $this->stepRunner);
164
        $duplicate->setData($this->data);
165
166
        return $duplicate;
167
    }
168
169
    /**
170
     * @return \Iterator|\Countable
171
     */
172
    public function getIterator()
173
    {
174
        return $this->iterator;
175
    }
176
177
    /**
178
     * @param \Traversable $iterator
179
     */
180
    public function setIterator($iterator)
181
    {
182
        $this->iterator = $iterator;
183
    }
184
185
    public function setDryRun(bool $dryRun)
186
    {
187
        $this->dryRun = $dryRun;
188
    }
189
190
    public function isDryRun(): bool
191
    {
192
        return $this->dryRun;
193
    }
194
}
195