ProcessState::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Darkilliant\ProcessBundle\State;
6
7
use Psr\Log\AbstractLogger;
8
use Psr\Log\LoggerInterface;
9
use Darkilliant\ProcessBundle\Runner\StepRunner;
10
11
class ProcessState extends AbstractLogger
12
{
13
    const RESULT_KO = 1;
14
    const RESULT_SKIP = 2;
15
    const RESULT_OK = 3;
16
    const RESULT_BREAK = 4;
17
    const RESULT_EXIT = 5;
18
19
    private $data;
20
    private $loopContext;
21
    private $logContext = [];
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
32
    /** @var \Traversable */
33
    private $iterator;
34
35
    private $name;
36
37
    private $loop;
38
39 160
    public function __construct(array $context, LoggerInterface $logger, StepRunner $stepRunner)
40
    {
41 160
        $this->context = $context;
42 160
        $this->logger = $logger;
43 160
        $this->stepRunner = $stepRunner;
44 160
    }
45
46 9
    public function setName(string $name)
47
    {
48 9
        $this->name = $name;
49 9
    }
50
51
    /**
52
     * @return mixed
53
     */
54 40
    public function getData()
55
    {
56 40
        return $this->data;
57
    }
58
59
    /**
60
     * @param mixed $data
61
     */
62 45
    public function setData($data)
63
    {
64 45
        $this->data = $data;
65 45
    }
66
67
    /**
68
     * @return mixed
69
     */
70 9
    public function getContext($key, $default = null)
71
    {
72 9
        return $this->context[$key] ?? $default;
73
    }
74
75 3
    public function getLoop()
76
    {
77 3
        return $this->loop;
78
    }
79
80 4
    public function noLoop()
81
    {
82 4
        $this->loop = null;
83 4
        $this->loopContext = null;
84 4
    }
85
86 8
    public function loop(int $index, int $count, bool $last)
87
    {
88 8
        $this->loop = [
89 8
            'index' => $index,
90 8
            'count' => $count,
91 8
            'last' => $last,
92
        ];
93 8
    }
94
95 2
    public function isLoop()
96
    {
97 2
        return (bool) $this->loop;
98
    }
99
100 24
    public function setContext(string $key, $value, bool $logContext = true)
101
    {
102 24
        $this->context[$key] = $value;
103 24
        if ($logContext) {
104 24
            $this->logContext[$key] = $value;
105
        }
106 24
    }
107
108 1
    public function hasContext(string $key): bool
109
    {
110 1
        return isset($this->context[$key]);
111
    }
112
113 4
    public function setLoopContext($value)
114
    {
115 4
        $this->loopContext = $value;
116 4
    }
117
118 4
    public function getLoopContext()
119
    {
120 4
        return $this->loopContext;
121
    }
122
123 8
    public function getRawContext(): array
124
    {
125 8
        return $this->context;
126
    }
127
128
    /**
129
     * @return LoggerInterface
130
     */
131 2
    public function getLogger(): LoggerInterface
132
    {
133 2
        return $this->logger;
134
    }
135
136
    /**
137
     * @return array
138
     */
139 86
    public function getOptions(): array
140
    {
141 86
        return $this->options;
142
    }
143
144 10
    public function getOption($name, $default = null)
145
    {
146 10
        return $this->options[$name] ?? $default;
147
    }
148
149
    /**
150
     * @param array $options
151
     */
152 103
    public function setOptions(array $options): ProcessState
153
    {
154 103
        $this->options = $options;
155
156 103
        return $this;
157
    }
158
159 1
    public function hasOption(string $name): bool
160
    {
161 1
        return isset($this->options[$name]);
162
    }
163
164 40
    public function log($level, $message, array $context = [])
165
    {
166 40
        $this->logger->log($level, $message, array_merge($context, $this->logContext));
167 40
    }
168
169 26
    public function getResult()
170
    {
171 26
        return $this->result;
172
    }
173
174 5
    public function markFail()
175
    {
176 5
        $this->result = self::RESULT_KO;
177 5
    }
178
179 4
    public function markIgnore()
180
    {
181 4
        $this->result = self::RESULT_SKIP;
182 4
    }
183
184 21
    public function markSuccess()
185
    {
186 21
        $this->result = self::RESULT_OK;
187 21
    }
188
189 5
    public function markBreak()
190
    {
191 5
        $this->result = self::RESULT_BREAK;
192 5
    }
193
194 1
    public function markExit()
195
    {
196 1
        $this->result = self::RESULT_EXIT;
197 1
    }
198
199 3
    public function getStepRunner(): StepRunner
200
    {
201 3
        return $this->stepRunner;
202
    }
203
204 1
    public function duplicate($logger = null): self
205
    {
206 1
        $duplicate = new self($this->context, $logger ?? $this->logger, $this->stepRunner);
207 1
        $duplicate->setData($this->data);
208
209 1
        return $duplicate;
210
    }
211
212
    /**
213
     * @return \ArrayIterator
214
     */
215 19
    public function getIterator()
216
    {
217 19
        return $this->iterator;
218
    }
219
220
    /**
221
     * @param \Traversable $iterator
222
     */
223 23
    public function setIterator($iterator)
224
    {
225 23
        $this->iterator = $iterator;
226 23
    }
227
228 15
    public function setDryRun(bool $dryRun)
229
    {
230 15
        $this->dryRun = $dryRun;
231 15
    }
232
233 10
    public function isDryRun(): bool
234
    {
235 10
        return $this->dryRun;
236
    }
237
238 1
    public function getName(): string
239
    {
240 1
        return $this->name;
241
    }
242
}
243