Passed
Push — test ( 9918b9...de3f09 )
by Tom
04:09 queued 22s
created

FileShower::showPipelines()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 2
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;
10
use Ktomk\Pipelines\File\File;
11
use Ktomk\Pipelines\File\Info\StepsInfo;
12
use Ktomk\Pipelines\File\Info\StepsStepInfoIterator;
13
use Ktomk\Pipelines\File\ParseException;
14
use Ktomk\Pipelines\File\Pipeline\Step;
15
use Ktomk\Pipelines\File\Pipeline\Steps;
16
17
/**
18
 * Class FileShower
19
 *
20
 * Shows information about a file
21
 *
22
 * @package Ktomk\Pipelines\Utility
23
 */
24
class FileShower extends FileShowerAbstract
25
{
26
    /**
27
     * @throws InvalidArgumentException
28
     *
29
     * @return int
30
     */
31 2
    public function showImages()
32
    {
33 2
        $images = array();
34
35 2
        foreach ($this->getAllStepsWithServices($this->file) as $step) {
36 2
            $image = $step->getImage();
37 2
            $images[(string)$image] = $image;
38
        }
39
40 2
        foreach ($images as $image) {
41 2
            $this->info((string)$image);
42
        }
43
44 2
        return 0;
45
    }
46
47
    /**
48
     * @return int
49
     */
50 1
    public function showPipelineIds()
51
    {
52 1
        $pipelines = $this->file->getPipelines();
53
54 1
        foreach ($pipelines->getPipelineIds() as $id) {
55 1
            $this->info($id);
56
        }
57
58 1
        return 0;
59
    }
60
61
    /**
62
     * shows pipeline and steps
63
     *
64
     * @return int 0 if there were no errors, 1 if there were errors
65
     */
66 6
    public function showFile()
67
    {
68 6
        $pipelines = $this->file->getPipelines();
69
70 6
        $table = new FileTable(array('PIPELINE ID', 'STEP', 'IMAGE', 'NAME'));
71
72 6
        foreach ($this->tablePipelineIdsPipelines($pipelines, $table) as $id => $pipeline) {
73 5
            $steps = (null === $pipeline) ? array() : $pipeline->getSteps();
74 5
            $this->tableFileSteps($steps, $id, $table);
75
        }
76
77 5
        return $this->outputTableAndReturn($table);
78
    }
79
80
    /**
81
     * shows summary of the file, first pipelines then pipeline services
82
     *
83
     * @return int 0 if there were no errors, 1 if there were errors
84
     */
85 7
    public function showPipelines()
86
    {
87 7
        $pipelines = $this->file->getPipelines();
88
89 7
        $table = new FileTable(array('PIPELINE ID', 'IMAGES', 'STEPS'));
90
91 7
        foreach ($this->tablePipelineIdsPipelines($pipelines, $table) as $id => $pipeline) {
92 4
            $info = StepsInfo::fromPipeline($pipeline);
93 4
            $table->addRow(array($id, $info->getImagesAsString(), $info->getSummary()));
94
        }
95
96 7
        return $this->outputTableAndReturn($table);
97
    }
98
99
    /**
100
     * @return int
101
     */
102 5
    public function showServices()
103
    {
104 5
        $file = $this->file;
105
106 5
        $table = new FileTable(array('PIPELINE ID', 'STEP', 'SERVICE', 'IMAGE'));
107
108
        try {
109 5
            $serviceDefinitions = $file->getDefinitions()->getServices();
110 3
            foreach ($this->tablePipelineIdsPipelines($file->getPipelines(), $table) as $id => $pipeline) {
111 2
                $this->tableStepsServices($pipeline->getSteps(), $serviceDefinitions, $id, $table);
112
            }
113 2
        } catch (ParseException $e) {
114 2
            $table->addErrorRow(array('', '', 'ERROR', $e->getParseMessage()));
115
        }
116
117 5
        return $this->outputTableAndReturn($table);
118
    }
119
120
    /**
121
     * @param Step[]|Steps $steps
122
     * @param string $id
123
     * @param FileTable $table
124
     *
125
     * @return void
126
     */
127 5
    private function tableFileSteps($steps, $id, FileTable $table)
128
    {
129 5
        foreach (new StepsStepInfoIterator($steps) as $info) {
130 5
            $table->addRow(array($id, $info->annotate($number = $info->getStepNumber()), $info->getImage(), $info->getName()));
131 5
            $this->tableFileStepsCaches($step = $info->getStep(), $id, $number, $table);
132 4
            $this->tableFileStepsServices($step, $id, $number, $table);
133
        }
134 4
    }
135
136
    /**
137
     * @param Step $step
138
     * @param string $id
139
     * @param int $stepNumber 1 based
140
     * @param FileTable $table
141
     *
142
     * @return void
143
     */
144 5
    private function tableFileStepsCaches(Step $step, $id, $stepNumber, FileTable $table)
145
    {
146 5
        $caches = $step->getCaches();
147 4
        $cacheDefinitions = $step->getFile()->getDefinitions()->getCaches();
148
149 4
        foreach ($caches->getNames() as $cacheName) {
150 2
            $cacheLabel = 'cache: ' . $cacheName;
151 2
            $definition = $cacheDefinitions->getByName($cacheName);
152 2
            $cacheDescription = true === $definition ? '*internal*' : $definition;
153 2
            $table->addRow(array($id, $stepNumber, $cacheLabel, $cacheDescription));
154
        }
155 4
    }
156
157
    /**
158
     * @param Step $step
159
     * @param string $id
160
     * @param int $stepNumber 1 based
161
     * @param FileTable $table
162
     *
163
     * @return void
164
     */
165 4
    private function tableFileStepsServices(Step $step, $id, $stepNumber, FileTable $table)
166
    {
167 4
        foreach ($step->getServices()->getServiceNames() as $serviceName) {
168
            /** @var Service $service */
169 1
            $service = $step->getFile()->getDefinitions()->getServices()->getByName($serviceName);
170 1
            $table->addFlaggedRow(
171 1
                $service,
172 1
                array($id, $stepNumber, $service ? $service->getImage() : 'ERROR', 'service:' . $serviceName)
173
            );
174
        }
175 4
    }
176
177
    /**
178
     * @param Steps $steps
179
     * @param Services $serviceDefinitions
180
     * @param string $id
181
     * @param FileTable $table
182
     *
183
     * @return void
184
     */
185 2
    private function tableStepsServices(Steps $steps, Services $serviceDefinitions, $id, FileTable $table)
186
    {
187 2
        foreach ($steps as $step) {
188 2
            $serviceNames = $step->getServices()->getServiceNames();
189 2
            if (empty($serviceNames)) {
190 1
                continue;
191
            }
192
193 2
            $stepNo = $step->getIndex() + 1;
194
195 2
            foreach ($serviceNames as $name) {
196 2
                if ($service = $serviceDefinitions->getByName($name)) {
197 2
                    $table->addRow(array($id, $stepNo, $name, $service->getImage()));
198
                } else {
199 1
                    $table->addErrorRow(array($id, $stepNo, 'ERROR', sprintf('Undefined service: "%s"', $name)));
200
                }
201
            }
202
        }
203 2
    }
204
205
    /**
206
     * @param File $file
207
     *
208
     * @return Step[]
209
     */
210 2
    private function getAllSteps(File $file)
211
    {
212 2
        $return = array();
213 2
        foreach ($file->getPipelines()->getPipelines() as $id => $pipeline) {
214 2
            foreach (Steps::fullIter($pipeline->getSteps()) as $index => $step) {
215 2
                $return["${id}:/step/${index}"] = $step;
216
            }
217
        }
218
219 2
        return $return;
220
    }
221
222
    /**
223
     * step iterator w/services
224
     *
225
     * @param File $file
226
     *
227
     * @return Service[]|Step[]
228
     */
229 2
    private function getAllStepsWithServices(File $file)
230
    {
231 2
        $return = array();
232
233 2
        foreach ($this->getAllSteps($file) as $key => $step) {
234 2
            $return[$key] = $step;
235 2
            foreach ($step->getServices()->getDefinitions() as $name => $service) {
236 1
                $return["${key}/service/${name}"] = $service;
237
            }
238
        }
239
240 2
        return $return;
241
    }
242
}
243