Completed
Pull Request — 0.4 (#40)
by jean
11:41
created

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