Passed
Push — master ( 9e5fe0...88d9d6 )
by Tom
04:35
created

FileShower::showPipelines()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

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