Producer   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 265
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 93.62%

Importance

Changes 0
Metric Value
wmc 29
lcom 1
cbo 5
dl 0
loc 265
ccs 88
cts 94
cp 0.9362
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setTaskSet() 0 6 1
A setOutputHandler() 0 6 1
A getOutputHandler() 0 8 2
A output() 0 6 1
A run() 0 21 3
A createChecker() 0 14 3
A renderTask() 0 15 4
A renderHeader() 0 10 1
A renderStats() 0 21 3
B renderList() 0 26 3
B runTask() 0 22 6
1
<?php
2
3
namespace Simplario\Checker;
4
5
use Simplario\Checker\Checker\AbstractChecker;
6
use Simplario\Checker\Output\AbstractOutput;
7
use Simplario\Checker\Output\Console;
8
use Simplario\Checker\ResultException\AbstractResultException;
9
use Simplario\Checker\ResultException\ErrorException;
10
use Simplario\Checker\ResultException\FailException;
11
use Simplario\Checker\ResultException\SuccessException;
12
13
/**
14
 * Class Producer
15
 *
16
 * @package Simplario\Checker
17
 */
18
class Producer
19
{
20
21
    const ERROR_CONFIG_WITH_EMPTY_TASK = 'Config with empty task';
22 6
    const PLACEHOLDER_TASK_SUCCESS = ' +    Ok :';
23
    const PLACEHOLDER_TASK_FAIL = ' -  Fail :';
24 6
    const PLACEHOLDER_TASK_ERROR = ' - Error :';
25 6
26
    /**
27 6
     * @var array
28
     */
29 6
    protected $taskSet = [];
30
31 6
    /**
32
     * @var AbstractChecker[]
33
     */
34
    protected $checkerSet = [];
35
36 5
    /**
37
     * @var AbstractOutput
38 5
     */
39 5
    protected $outputHandler;
40
41
    /**
42 3
     * @var array
43
     */
44
    protected $taskFail = [];
45 3
46 1
    /**
47
     * @var array
48
     */
49 3
    protected $taskError = [];
50
51
    /**
52 2
     * @var array
53
     */
54 2
    protected $taskSuccess = [];
55
56 2
    /**
57
     * Producer constructor.
58
     *
59 2
     * @param array $taskSet
60
     */
61 2
    public function __construct(array $taskSet = [])
62
    {
63 2
        $this->setTaskSet($taskSet);
64 1
    }
65
66 1
    /**
67
     * @param array $taskSet
68
     *
69 1
     * @return $this
70 1
     */
71 1
    public function setTaskSet(array $taskSet)
72
    {
73
        $this->taskSet = $taskSet;
74 1
75 1
        return $this;
76 1
    }
77
78 1
    /**
79
     * @param AbstractOutput $outputHandler
80
     *
81
     * @return $this
82
     */
83
    public function setOutputHandler(AbstractOutput $outputHandler)
84
    {
85 3
        $this->outputHandler = $outputHandler;
86
87 3
        return $this;
88
    }
89 3
90 3
    /**
91
     * @return AbstractOutput|Console
92
     */
93 3
    public function getOutputHandler()
94
    {
95
        if ($this->outputHandler === null) {
96
            $this->outputHandler = new Console();
97
        }
98
99
        return $this->outputHandler;
100 1
    }
101
102 1
    /**
103 1
     * @param string $msg
104 1
     * @param string $type
105
     *
106 1
     * @return $this
107
     */
108 1
    public function output($msg = '', $type = AbstractOutput::TYPE_DEFAULT)
109
    {
110
        $this->getOutputHandler()->write($msg, $type);
111 1
112
        return $this;
113 1
    }
114
115
    /**
116 2
     * @return $this
117
     */
118 2
    public function run()
119 2
    {
120 2
        if (empty($this->taskSet)) {
121 2
            $this->output(self::ERROR_CONFIG_WITH_EMPTY_TASK, AbstractOutput::TYPE_ERROR);
122 2
123
            return $this;
124 2
        }
125
126
        $this->renderHeader('Run checker');
127 1
128
        foreach ($this->taskSet as $index => $task) {
129 1
            $resultException = $this->runTask($task);
130 1
            $this->renderTask($resultException);
131 1
        }
132 1
133
        $this->renderList($this->taskFail, 'Fail list');
134 1
        $this->renderList($this->taskError, 'Error list');
135 1
        $this->renderStats();
136
137
        return $this;
138
    }
139
140 1
    /**
141 1
     * @param $checkerAlias
142 1
     *
143 1
     * @return AbstractChecker
144 1
     */
145
    public function createChecker($checkerAlias)
146 1
    {
147
        if (class_exists($checkerAlias)) {
148
            $class = $checkerAlias;
149 1
        } else {
150
            $class = __NAMESPACE__ . '\\Checker\\' . ucfirst($checkerAlias);
151
        }
152 1
153
        if (!isset($this->checkerSet[$class])) {
154
            $this->checkerSet[$class] = new $class;
155
        }
156 1
157
        return $this->checkerSet[$class];
158 1
    }
159
160
    /**
161 1
     * @param AbstractResultException $resultException
162 1
     *
163 1
     * @return $this
164 1
     */
165 1
    protected function renderTask(AbstractResultException $resultException)
166 1
    {
167 1
        $result = 'undefined:';
168
        if ($resultException instanceof SuccessException) {
169
            $result = self::PLACEHOLDER_TASK_SUCCESS;
170
        } elseif ($resultException instanceof FailException) {
171 1
            $result = self::PLACEHOLDER_TASK_FAIL;
172
        } elseif ($resultException instanceof ErrorException) {
173
            $result = self::PLACEHOLDER_TASK_ERROR;
174 1
        }
175
176
        $this->output("{$result} {$resultException->getJson()}");
177 2
178
        return $this;
179
    }
180 2
181 2
    /**
182 2
     * @param $text
183 2
     *
184 2
     * @return $this
185 2
     */
186 2
    protected function renderHeader($text)
187 2
    {
188 1
        $this->output("");
189 1
        $this->output("=============================================================================");
190 1
        $this->output("=== {$text}");
191
        $this->output("=============================================================================");
192
        $this->output("");
193 2
194
        return $this;
195
    }
196
197 2
    /**
198
     * @return $this
199
     */
200
    protected function renderStats()
201
    {
202
        $fail = count($this->taskFail);
203
        $error = count($this->taskError);
204
        $success = count($this->taskSuccess);
205
        $total = $error + $success + $fail;
206
207
        $imgPath = __DIR__ . '/../on-success.txt';
208
        if ($fail + $error === 0 && is_file($imgPath)) {
209
            $img = file_get_contents($imgPath);
210
            $this->output($img);
211
        }
212
213
        $this->renderHeader("Stats");
214
        $this->output(" > Total {$total}");
215
        $this->output(" > Success {$success}");
216
        $this->output(" > Fail {$fail}");
217
        $this->output(" > Error {$error}");
218
219
        return $this;
220
    }
221
222
    /**
223
     * @param array  $list
224
     * @param string $title
225
     *
226
     * @return $this
227
     */
228
    protected function renderList(array $list, $title = '')
229
    {
230
        if (count($list) === 0) {
231
            return $this;
232
        }
233
234
        $this->renderHeader($title);
235
236
        foreach ($list as $index => $resultException) {
237
            /** @var $error AbstractResultException */
238
239
            $text = implode(
240
                PHP_EOL, [
241
                    "# " . ($index + 1),
242
                    "  Checker : {$resultException->getChecker()}",
243
                    "  Task    : {$resultException->getJson()}",
244
                    "  Reason  : {$resultException->getMessage()}",
245
                    ""
246
                ]
247
            );
248
249
            $this->output($text);
250
        }
251
252
        return $this;
253
    }
254
255
    /**
256
     * @param array $task
257
     *
258
     * @return \Exception|ErrorException|FailException|SuccessException
259
     */
260
    public function runTask(array  $task = [])
261
    {
262
        try {
263
            $checker = $this->createChecker($task['checker']);
264
            $checker->check($task);
265
        } catch (SuccessException $resultException) {
266
            $this->taskSuccess[] = $resultException;
267
        } catch (FailException $resultException) {
268
            $this->taskFail[] = $resultException;
269
        } catch (ErrorException $resultException) {
270
            $this->taskError[] = $resultException;
271
        } catch (\Exception $ex) {
272
            $resultException = new ErrorException($ex->getMessage(), $task);;
273
            $this->taskError[] = $resultException;
274
        }
275
276
        if (empty($resultException)) {
277
            $resultException = new ErrorException('Checker fail to respond correct (((', $task);
278
        }
279
280
        return $resultException;
281
    }
282
}