Completed
Push — 0.3 ( 907796...d1d7da )
by jean
11s
created

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