Failed Conditions
Pull Request — 0.4 (#40)
by jean
05:17
created

ProcessState::setContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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