Failed Conditions
Push — 0.3 ( 530096...907796 )
by jean
04:25 queued 10s
created

ProcessState   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 166
ccs 61
cts 61
cp 1
rs 10
c 0
b 0
f 0
wmc 22

22 Methods

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