Completed
Push — develop ( ac3e4a...ad1ee7 )
by Vlad
02:08
created

Producer::setConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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
    const PLACEHOLDER_TASK_SUCCESS = ' +    Ok :';
23
    const PLACEHOLDER_TASK_FAIL = ' -  Fail :';
24
    const PLACEHOLDER_TASK_ERROR = ' - Error :';
25
26
    /**
27
     * @var array
28
     */
29
    protected $taskSet = [];
30
31
    /**
32
     * @var AbstractChecker[]
33
     */
34
    protected $checkerSet = [];
35
36
    /**
37
     * @var AbstractOutput
38
     */
39
    protected $outputHandler;
40
41
    /**
42
     * @var array
43
     */
44
    protected $taskFail = [];
45
46
    /**
47
     * @var array
48
     */
49
    protected $taskError = [];
50
51
    /**
52
     * @var array
53
     */
54
    protected $taskSuccess = [];
55
56
    /**
57
     * Producer constructor.
58
     *
59
     * @param array $taskSet
60
     */
61
    public function __construct(array $taskSet = [])
62
    {
63
        $this->setTaskSet($taskSet);
64
    }
65
66
    /**
67
     * @param array $taskSet
68
     *
69
     * @return $this
70
     */
71
    public function setTaskSet(array $taskSet)
72
    {
73
        $this->taskSet = $taskSet;
74
75
        return $this;
76
    }
77
78
    /**
79
     * @param AbstractOutput $outputHandler
80
     *
81
     * @return $this
82
     */
83
    public function setOutputHandler(AbstractOutput $outputHandler)
84
    {
85
        $this->outputHandler = $outputHandler;
86
87
        return $this;
88
    }
89
90
    /**
91
     * @return AbstractOutput|Console
92
     */
93
    public function getOutputHandler()
94
    {
95
        if ($this->outputHandler === null) {
96
            $this->outputHandler = new Console();
97
        }
98
99
        return $this->outputHandler;
100
    }
101
102
    /**
103
     * @param string $msg
104
     * @param string $type
105
     *
106
     * @return $this
107
     */
108
    public function output($msg = '', $type = AbstractOutput::TYPE_DEFAULT)
109
    {
110
        $this->getOutputHandler()->write($msg, $type);
111
112
        return $this;
113
    }
114
115
    /**
116
     * @return $this
117
     */
118
    public function run()
119
    {
120
        if (empty($this->taskSet)) {
121
            $this->output(self::ERROR_CONFIG_WITH_EMPTY_TASK, AbstractOutput::TYPE_ERROR);
122
123
            return $this;
124
        }
125
126
        $this->renderHeader('Run checker');
127
128
        foreach ($this->taskSet as $index => $task) {
129
            $resultException = $this->runTask($task);
130
            $this->renderTask($resultException);
131
        }
132
133
        $this->renderList($this->taskFail, 'Fail list');
134
        $this->renderList($this->taskError, 'Error list');
135
        $this->renderStats();
136
137
        return $this;
138
    }
139
140
    /**
141
     * @param $checkerAlias
142
     *
143
     * @return AbstractChecker
144
     */
145
    public function createChecker($checkerAlias)
146
    {
147
        if (class_exists($checkerAlias)) {
148
            $class = $checkerAlias;
149
        } else {
150
            $class = __NAMESPACE__ . '\\Checker\\' . ucfirst($checkerAlias);
151
        }
152
153
        if (!isset($this->checkerSet[$class])) {
154
            $this->checkerSet[$class] = new $class;
155
        }
156
157
        return $this->checkerSet[$class];
158
    }
159
160
    /**
161
     * @param AbstractResultException $resultException
162
     *
163
     * @return $this
164
     */
165
    protected function renderTask(AbstractResultException $resultException)
166
    {
167
        $result = 'undefined:';
168
        if ($resultException instanceof SuccessException) {
169
            $result = self::PLACEHOLDER_TASK_SUCCESS;
170
        } elseif ($resultException instanceof FailException) {
171
            $result = self::PLACEHOLDER_TASK_FAIL;
172
        } elseif ($resultException instanceof ErrorException) {
173
            $result = self::PLACEHOLDER_TASK_ERROR;
174
        }
175
176
        $this->output("{$result} {$resultException->getJson()}");
177
178
        return $this;
179
    }
180
181
    /**
182
     * @param $text
183
     *
184
     * @return $this
185
     */
186
    protected function renderHeader($text)
187
    {
188
        $this->output("");
189
        $this->output("=============================================================================");
190
        $this->output("=== {$text}");
191
        $this->output("=============================================================================");
192
        $this->output("");
193
194
        return $this;
195
    }
196
197
    /**
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
}