Passed
Push — master ( 503f62...3f0369 )
by Tom
13:13 queued 10:28
created

FileShower::getImagesAndNames()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

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