Passed
Push — test ( 3e2008...5a4346 )
by Tom
02:40
created

FileShower::tableFileStepsServices()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 4
nc 5
nop 6
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Utility\Show;
6
7
use InvalidArgumentException;
8
use Ktomk\Pipelines\File\Definitions\Service;
9
use Ktomk\Pipelines\File\File;
10
use Ktomk\Pipelines\File\ParseException;
11
use Ktomk\Pipelines\File\Pipeline\Step;
12
use Ktomk\Pipelines\File\Pipeline\Steps;
13
use Ktomk\Pipelines\File\Pipelines;
14
15
/**
16
 * Class FileShower
17
 *
18
 * Shows information about a file
19
 *
20
 * @package Ktomk\Pipelines\Utility
21
 */
22
class FileShower extends FileShowerAbstract
23
{
24
    /**
25
     * @throws InvalidArgumentException
26
     *
27
     * @return int
28
     */
29 2
    public function showImages()
30
    {
31 2
        $images = array();
32
33 2
        foreach ($this->getAllStepsWithServices($this->file) as $step) {
34 2
            $image = $step->getImage();
35 2
            $images[(string)$image] = $image;
36
        }
37
38 2
        foreach ($images as $image) {
39 2
            $this->info($image);
40
        }
41
42 2
        return 0;
43
    }
44
45
    /**
46
     * @return int
47
     */
48 1
    public function showPipelineIds()
49
    {
50 1
        $pipelines = $this->file->getPipelines();
51
52 1
        foreach ($pipelines->getPipelineIds() as $id) {
53 1
            $this->info($id);
54
        }
55
56 1
        return 0;
57
    }
58
59
    /**
60
     * shows pipeline and steps
61
     *
62
     * @return int 0 if there were no errors, 1 if there were errors
63
     */
64 2
    public function showFile()
65
    {
66 2
        $pipelines = $this->file->getPipelines();
67
68 2
        $errors = 0;
69 2
        $table = array(array('PIPELINE ID', 'STEP', 'IMAGE', 'NAME'));
70 2
        foreach ($pipelines->getPipelineIds() as $id) {
71 2
            list($pipeline, $message) = $this->getShowPipeline($pipelines, $id);
72 2
            if ($message) {
73 1
                $table[] = array($id, 'ERROR', $message, '');
74 1
                $errors++;
75
76 1
                continue;
77
            }
78
79 1
            $steps = (null === $pipeline) ? array() : $pipeline->getSteps();
80 1
            list($table, $errors) = $this->tableFileSteps($steps, $id, $table, $errors);
81
        }
82
83 2
        $this->textTable($table);
84
85 2
        return $errors ? 1 : 0;
86
    }
87
88
    /**
89
     * shows summary of the file, first pipelines then pipline services
90
     *
91
     * @return int 0 if there were no errors, 1 if there were errors
92
     */
93 5
    public function showPipelines()
94
    {
95 5
        $pipelines = $this->file->getPipelines();
96
97 5
        $errors = 0;
98 5
        $table = array(array('PIPELINE ID', 'IMAGES', 'STEPS'));
99 5
        foreach ($pipelines->getPipelineIds() as $id) {
100 5
            list($pipeline, $message) = $this->getShowPipeline($pipelines, $id);
101 5
            if ($message) {
102 3
                $table[] = array($id, 'ERROR', $message);
103 3
                $errors++;
104
105 3
                continue;
106
            }
107
108 2
            $steps = (null === $pipeline) ? null : $pipeline->getSteps();
109 2
            list($images, $names) = $this->getImagesAndNames($steps);
110
111 2
            $images = $images ? implode(', ', $images) : '';
112 2
            $steps = sprintf('%d%s', count($steps), $names ? ' ("' . implode('"; "', $names) . '")' : '');
113 2
            $table[] = array($id, $images, $steps);
114
        }
115
116 5
        $this->textTable($table);
117
118 5
        return $errors ? 1 : 0;
119
    }
120
121 5
    public function showServices()
122
    {
123 5
        $file = $this->file;
124 5
        $pipelines = $file->getPipelines();
125
126 5
        $errors = 0;
127 5
        $table = array(array('PIPELINE ID', 'STEP', 'SERVICE', 'IMAGE'));
128
129
        try {
130 5
            $serviceDefinitions = $file->getDefinitions()->getServices();
131 2
        } catch (ParseException $e) {
132 2
            $table[] = array('', '', 'ERROR', $e->getParseMessage());
133 2
            $this->textTable($table);
134
135 2
            return 1;
136
        }
137
138 3
        foreach ($pipelines->getPipelineIds() as $id) {
139 3
            list($pipeline, $message) = $this->getShowPipeline($pipelines, $id);
140 3
            if ($message) {
141 1
                $table[] = array($id, '', 'ERROR', $message);
142 1
                $errors++;
143
144 1
                continue;
145
            }
146
147
            list($table, $errors) =
148 2
                $this->tableStepsServices($pipeline->getSteps(), $serviceDefinitions, $id, $table, $errors);
149
        }
150
151 3
        $this->textTable($table);
152
153 3
        return $errors ? 1 : 0;
154
    }
155
156
    /**
157
     * @param Step[]|Steps $steps
158
     * @param string $id
159
     * @param array $table
160
     * @param int $errors
161
     *
162
     * @return array
163
     */
164 1
    private function tableFileSteps($steps, $id, array $table, $errors)
165
    {
166 1
        foreach ($steps as $index => $step) {
167
            /** @var Step $step */
168 1
            $name = $step->getName();
169 1
            null !== $name && $name = sprintf('"%s"', $name);
170 1
            null === $name && $name = 'no-name';
171
172 1
            $table[] = array($id, $index + 1, $step->getImage(), $name);
173 1
            list($table, $errors) = $this->tableFileStepsServices(
174 1
                $step->getServices()->getServiceNames(),
175
                $step,
176
                $id,
177 1
                $index + 1,
178
                $table,
179
                $errors
180
            );
181
        }
182
183 1
        return array($table, $errors);
184
    }
185
186
    /**
187
     * @param array|string[] $serviceNames
188
     * @param Step $step
189
     * @param string $id
190
     * @param int $stepNumber 1 based
191
     * @param array $table
192
     * @param int $errors
193
     *
194
     * @return array
195
     */
196 1
    private function tableFileStepsServices($serviceNames, Step $step, $id, $stepNumber, array $table, $errors)
197
    {
198 1
        foreach ($serviceNames as $serviceName) {
199
            /** @var Service $service */
200 1
            ($service = $step->getFile()->getDefinitions()->getServices()->getByName($serviceName)) || $errors++;
201 1
            $table[] = array($id, $stepNumber, $service ? $service->getImage() : 'ERROR', 'service:' . $serviceName);
202
        }
203
204 1
        return array($table, $errors);
205
    }
206
207 2
    private function tableStepsServices(Steps $steps, $serviceDefinitions, $id, array $table, $errors)
208
    {
209 2
        foreach ($steps as $step) {
210
            /** @var Step $step */
211 2
            $serviceNames = $step->getServices()->getServiceNames();
212 2
            if (!$serviceNames) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $serviceNames of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
213 1
                continue;
214
            }
215
216 2
            $stepNo = $step->getIndex() + 1;
217
218 2
            foreach ($serviceNames as $name) {
219 2
                if (!$service = $serviceDefinitions->getByName($name)) {
220 1
                    $table[] = array($id, $stepNo, 'ERROR', sprintf('Undefined service: "%s"', $name));
221 1
                    $errors++;
222
                } else {
223 2
                    $table[] = array($id, $stepNo, $name, $service->getImage());
224
                }
225
            }
226
        }
227
228 2
        return array($table, $errors);
229
    }
230
231
    /**
232
     * @param File $file
233
     *
234
     * @return Step[]
235
     */
236 2
    private function getAllSteps(File $file)
237
    {
238 2
        $return = array();
239 2
        foreach ($file->getPipelines()->getPipelines() as $id => $pipeline) {
240 2
            foreach (Steps::fullIter($pipeline->getSteps()) as $index => $step) {
241 2
                $return["${id}:/step/${index}"] = $step;
242
            }
243
        }
244
245 2
        return $return;
246
    }
247
248
    /**
249
     * step iterator w/services
250
     *
251
     * @param File $file
252
     *
253
     * @return Service[]|Step[]
254
     */
255
    private function getAllStepsWithServices(File $file)
256
    {
257 2
        $return = array();
258
259 2
        foreach ($this->getAllSteps($file) as $key => $step) {
260 2
            $return[$key] = $step;
261 2
            foreach ($step->getServices()->getDefinitions() as $name => $service) {
262 1
                $return["${key}/service/${name}"] = $service;
263
            }
264
        }
265
266 2
        return $return;
267
    }
268
269
    /**
270
     * @param Pipelines $pipelines
271
     * @param string $id
272
     *
273
     * @return array
274
     */
275
    private function getShowPipeline(Pipelines $pipelines, $id)
276
    {
277 10
        $pipeline = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $pipeline is dead and can be removed.
Loading history...
278 10
        $message = null;
279
280
        try {
281 10
            $pipeline = $pipelines->getById($id);
282 5
        } catch (ParseException $exception) {
283 4
            $message = $exception->getParseMessage();
284 1
        } catch (InvalidArgumentException $exception) {
285 1
            $message = $exception->getMessage();
286
        }
287
288 10
        return array($pipeline, $message);
289
    }
290
291
    /**
292
     * @param Steps $steps
293
     *
294
     * @return array
295
     */
296
    private function getImagesAndNames(Steps $steps = null)
297
    {
298 2
        $images = array();
299 2
        $names = array();
300
301 2
        foreach (Steps::fullIter($steps) as $step) {
302 2
            $image = $step->getImage()->getName();
303 2
            if (File::DEFAULT_IMAGE !== $image) {
304 2
                $images[] = $image;
305
            }
306 2
            $name = $step->getName();
307 2
            (null !== $name) && $names[] = $name;
308
        }
309
310 2
        $images = array_unique($images);
311
312 2
        return array($images, $names);
313
    }
314
}
315