Passed
Push — test ( 9e5fe0...6e7932 )
by Tom
02:50
created

FileShower::showFile()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 6
nop 0
dl 0
loc 14
ccs 9
cts 9
cp 1
crap 4
rs 10
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\Definitions\Services as Services;
10
use Ktomk\Pipelines\File\File;
11
use Ktomk\Pipelines\File\ParseException;
12
use Ktomk\Pipelines\File\Pipeline\Step;
13
use Ktomk\Pipelines\File\Pipeline\Steps;
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 4
    public function showFile()
65
    {
66 4
        $pipelines = $this->file->getPipelines();
67
68 4
        $errors = 0;
69 4
        $table = array(array('PIPELINE ID', 'STEP', 'IMAGE', 'NAME'));
70 4
        foreach ($this->tablePipelineIdsPipelines($pipelines, $table, $errors) as $id => $pipeline) {
71 3
            $steps = (null === $pipeline) ? array() : $pipeline->getSteps();
72 3
            list($table, $errors) = $this->tableFileSteps($steps, $id, $table, $errors);
73
        }
74
75 4
        $this->textTable($table);
76
77 4
        return $errors ? 1 : 0;
78
    }
79
80
    /**
81
     * shows summary of the file, first pipelines then pipline services
82
     *
83
     * @return int 0 if there were no errors, 1 if there were errors
84
     */
85 5
    public function showPipelines()
86
    {
87 5
        $pipelines = $this->file->getPipelines();
88
89 5
        $errors = 0;
90 5
        $table = array(array('PIPELINE ID', 'IMAGES', 'STEPS'));
91 5
        foreach ($this->tablePipelineIdsPipelines($pipelines, $table, $errors) as $id => $pipeline) {
92 2
            $steps = (null === $pipeline) ? null : $pipeline->getSteps();
93 2
            list($images, $names) = $this->getImagesAndNames($steps);
94
95 2
            $images = $images ? implode(', ', $images) : '';
96 2
            $steps = sprintf('%d%s', count($steps), $names ? ' ("' . implode('"; "', $names) . '")' : '');
97 2
            $table[] = array($id, $images, $steps);
98
        }
99
100 5
        $this->textTable($table);
101
102 5
        return $errors ? 1 : 0;
103
    }
104
105
    /**
106
     * @return int
107
     */
108 5
    public function showServices()
109
    {
110 5
        $file = $this->file;
111 5
        $pipelines = $file->getPipelines();
112
113 5
        $errors = 0;
114 5
        $table = array(array('PIPELINE ID', 'STEP', 'SERVICE', 'IMAGE'));
115
116
        try {
117 5
            $serviceDefinitions = $file->getDefinitions()->getServices();
118 2
        } catch (ParseException $e) {
119 2
            $table[] = array('', '', 'ERROR', $e->getParseMessage());
120 2
            $this->textTable($table);
121
122 2
            return 1;
123
        }
124
125 3
        foreach ($this->tablePipelineIdsPipelines($pipelines, $table, $errors) as $id => $pipeline) {
126
            list($table, $errors)
127 2
                = $this->tableStepsServices($pipeline->getSteps(), $serviceDefinitions, $id, $table, $errors);
128
        }
129
130 3
        $this->textTable($table);
131
132 3
        return $errors ? 1 : 0;
133
    }
134
135
    /**
136
     * @param Step[]|Steps $steps
137
     * @param string $id
138
     * @param array $table
139
     * @param int $errors
140
     *
141
     * @return array
142
     */
143 3
    private function tableFileSteps($steps, $id, array $table, $errors)
144
    {
145 3
        foreach ($steps as $index => $step) {
146 3
            $name = $step->getName();
147 3
            null !== $name && $name = sprintf('"%s"', $name);
148 3
            null === $name && $name = 'no-name';
149
150 3
            $table[] = array($id, $index + 1, $step->getImage(), $name);
151 3
            list($table, $errors) = $this->tableFileStepsCaches(
152 3
                $step,
153
                $id,
154 3
                $index + 1,
155
                $table,
156
                $errors
157
            );
158
159 3
            list($table, $errors) = $this->tableFileStepsServices(
160 3
                $step->getServices()->getServiceNames(),
161
                $step,
162
                $id,
163 3
                $index + 1,
164
                $table,
165
                $errors
166
            );
167
        }
168
169 3
        return array($table, $errors);
170
    }
171
172
    /**
173
     * @param Step $step
174
     * @param string $id
175
     * @param int $stepNumber 1 based
176
     * @param array $table
177
     * @param int $errors
178
     *
179
     * @return array
180
     */
181 3
    private function tableFileStepsCaches(Step $step, $id, $stepNumber, array $table, $errors)
182
    {
183 3
        $caches = $step->getCaches();
184 3
        $cacheDefinitions = $step->getFile()->getDefinitions()->getCaches();
185
186 3
        $cacheNames = $caches->getNames();
187 3
        $available = $cacheDefinitions->getByNames($cacheNames);
188
189 3
        foreach ($cacheNames as $cacheName) {
190 3
            $cacheLabel = 'cache: ' . $cacheName;
191 3
            if ('docker' === $cacheName) {
192 1
                $table[] = array($id, $stepNumber, $cacheLabel, '*internal*');
193
194 1
                continue;
195
            }
196 3
            if (!isset($available[$cacheName])) {
197 1
                $table[] = array($id, $stepNumber, $cacheLabel, 'ERROR');
198 1
                $errors++;
199
200 1
                continue;
201
            }
202
203 2
            $table[] = array($id, $stepNumber, $cacheLabel, $available[$cacheName]);
204
        }
205
206 3
        return array($table, $errors);
207
    }
208
209
    /**
210
     * @param array|string[] $serviceNames
211
     * @param Step $step
212
     * @param string $id
213
     * @param int $stepNumber 1 based
214
     * @param array $table
215
     * @param int $errors
216
     *
217
     * @return array
218
     */
219 3
    private function tableFileStepsServices($serviceNames, Step $step, $id, $stepNumber, array $table, $errors)
220
    {
221 3
        foreach ($serviceNames as $serviceName) {
222
            /** @var Service $service */
223 1
            ($service = $step->getFile()->getDefinitions()->getServices()->getByName($serviceName)) || $errors++;
224 1
            $table[] = array($id, $stepNumber, $service ? $service->getImage() : 'ERROR', 'service:' . $serviceName);
225
        }
226
227 3
        return array($table, $errors);
228
    }
229
230
    /**
231
     * @param Steps $steps
232
     * @param Services $serviceDefinitions
233
     * @param string $id
234
     * @param array $table
235
     * @param int $errors
236
     *
237
     * @return array
238
     */
239 2
    private function tableStepsServices(Steps $steps, Services $serviceDefinitions, $id, array $table, $errors)
240
    {
241 2
        foreach ($steps as $step) {
242 2
            $serviceNames = $step->getServices()->getServiceNames();
243 2
            if (empty($serviceNames)) {
244 1
                continue;
245
            }
246
247 2
            $stepNo = $step->getIndex() + 1;
248
249 2
            foreach ($serviceNames as $name) {
250 2
                if (!$service = $serviceDefinitions->getByName($name)) {
251 1
                    $table[] = array($id, $stepNo, 'ERROR', sprintf('Undefined service: "%s"', $name));
252 1
                    $errors++;
253
                } else {
254 2
                    $table[] = array($id, $stepNo, $name, $service->getImage());
255
                }
256
            }
257
        }
258
259 2
        return array($table, $errors);
260
    }
261
262
    /**
263
     * @param File $file
264
     *
265
     * @return Step[]
266
     */
267 2
    private function getAllSteps(File $file)
268
    {
269 2
        $return = array();
270 2
        foreach ($file->getPipelines()->getPipelines() as $id => $pipeline) {
271 2
            foreach (Steps::fullIter($pipeline->getSteps()) as $index => $step) {
272 2
                $return["${id}:/step/${index}"] = $step;
273
            }
274
        }
275
276 2
        return $return;
277
    }
278
279
    /**
280
     * step iterator w/services
281
     *
282
     * @param File $file
283
     *
284
     * @return Service[]|Step[]
285
     */
286
    private function getAllStepsWithServices(File $file)
287
    {
288 2
        $return = array();
289
290 2
        foreach ($this->getAllSteps($file) as $key => $step) {
291 2
            $return[$key] = $step;
292 2
            foreach ($step->getServices()->getDefinitions() as $name => $service) {
293 1
                $return["${key}/service/${name}"] = $service;
294
            }
295
        }
296
297 2
        return $return;
298
    }
299
300
    /**
301
     * @param Steps $steps
302
     *
303
     * @return array
304
     */
305
    private function getImagesAndNames(Steps $steps = null)
306
    {
307 2
        $images = array();
308 2
        $names = array();
309
310 2
        foreach (Steps::fullIter($steps) as $step) {
311 2
            $image = $step->getImage()->getName();
312 2
            if (File::DEFAULT_IMAGE !== $image) {
313 2
                $images[] = $image;
314
            }
315 2
            $name = $step->getName();
316 2
            (null !== $name) && $names[] = $name;
317
        }
318
319 2
        $images = array_unique($images);
320
321 2
        return array($images, $names);
322
    }
323
}
324