FileShower   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 219
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 73
dl 0
loc 219
ccs 80
cts 80
cp 1
rs 9.92
c 1
b 0
f 0
wmc 31

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllSteps() 0 10 3
A tableStepsServices() 0 15 5
A showImages() 0 14 3
A showServices() 0 16 3
A showPipelineIds() 0 8 1
A showFile() 0 12 3
A tableFileStepsCaches() 0 10 3
A tableFileSteps() 0 8 2
A tableFileStepsServices() 0 8 3
A showPipelines() 0 13 2
A getAllStepsWithServices() 0 12 3
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 8
    public function showFile()
66
    {
67 8
        $pipelines = $this->file->getPipelines();
68
69 8
        $table = new FileTable(array('PIPELINE ID', 'STEP', 'IMAGE', 'NAME'));
70
71 8
        foreach ($this->tablePipelineIdsPipelines($pipelines, $table) as $id => $pipeline) {
72 7
            $steps = (null === $pipeline) ? array() : $pipeline->getSteps();
73 7
            $this->tableFileSteps($steps, $id, $table);
74
        }
75
76 7
        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 9
    public function showPipelines()
85
    {
86 9
        $pipelines = $this->file->getPipelines();
87
88 9
        $table = new FileTable(array('PIPELINE ID', 'IMAGES', 'STEPS'));
89
90 9
        foreach ($this->tablePipelineIdsPipelines($pipelines, $table) as $id => $pipeline) {
91 6
            $info = StepsInfo::fromPipeline($pipeline);
92 6
            $summary = $info->getSummary($hasError);
93 6
            $table->addFlaggedRow($hasError, array($id, $info->getImagesAsString(), $summary));
94
        }
95
96 9
        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 7
    private function tableFileSteps($steps, $id, FileTable $table)
128
    {
129 7
        foreach (new StepsStepInfoIterator($steps) as $info) {
130 7
            $number = $info->getStepNumber();
131 7
            $annotate = $info->annotate($number, null, $errorFree);
132 7
            $table->addFlaggedRow($errorFree, array($id, $annotate, $info->getImage(), $info->getName()));
133 7
            $this->tableFileStepsCaches($step = $info->getStep(), $id, $number, $table);
134 6
            $this->tableFileStepsServices($step, $id, $number, $table);
135
        }
136
    }
137
138
    /**
139
     * @param Step $step
140
     * @param string $id
141
     * @param int $stepNumber 1 based
142
     * @param FileTable $table
143
     *
144
     * @return void
145
     */
146 7
    private function tableFileStepsCaches(Step $step, $id, $stepNumber, FileTable $table)
147
    {
148 7
        $caches = $step->getCaches();
149 6
        $cacheDefinitions = $step->getFile()->getDefinitions()->getCaches();
150
151 6
        foreach ($caches->getNames() as $cacheName) {
152 2
            $cacheLabel = 'cache: ' . $cacheName;
153 2
            $definition = $cacheDefinitions->getByName($cacheName);
154 2
            $cacheDescription = true === $definition ? '*internal*' : $definition;
155 2
            $table->addRow(array($id, $stepNumber, $cacheLabel, $cacheDescription));
156
        }
157
    }
158
159
    /**
160
     * @param Step $step
161
     * @param string $id
162
     * @param int $stepNumber 1 based
163
     * @param FileTable $table
164
     *
165
     * @return void
166
     */
167 6
    private function tableFileStepsServices(Step $step, $id, $stepNumber, FileTable $table)
168
    {
169 6
        foreach ($step->getServices()->getServiceNames() as $serviceName) {
170
            /** @var Service $service */
171 1
            $service = $step->getFile()->getDefinitions()->getServices()->getByName($serviceName);
172 1
            $table->addFlaggedRow(
173
                $service,
174 1
                array($id, $stepNumber, $service ? $service->getImage() : 'ERROR', 'service:' . $serviceName)
175
            );
176
        }
177
    }
178
179
    /**
180
     * @param Steps $steps
181
     * @param Services $serviceDefinitions
182
     * @param string $id
183
     * @param FileTable $table
184
     *
185
     * @return void
186
     */
187 2
    private function tableStepsServices(Steps $steps, Services $serviceDefinitions, $id, FileTable $table)
188
    {
189 2
        foreach ($steps as $step) {
190 2
            $serviceNames = $step->getServices()->getServiceNames();
191 2
            if (empty($serviceNames)) {
192 1
                continue;
193
            }
194
195 2
            $stepNo = $step->getIndex() + 1;
196
197 2
            foreach ($serviceNames as $name) {
198 2
                if ($service = $serviceDefinitions->getByName($name)) {
199 2
                    $table->addRow(array($id, $stepNo, $name, $service->getImage()));
200
                } else {
201 1
                    $table->addErrorRow(array($id, $stepNo, 'ERROR', sprintf('Undefined service: "%s"', $name)));
202
                }
203
            }
204
        }
205
    }
206
207
    /**
208
     * @param File $file
209
     *
210
     * @return Step[]
211
     */
212 2
    private function getAllSteps(File $file)
213
    {
214 2
        $return = array();
215 2
        foreach ($file->getPipelines()->getPipelines() as $id => $pipeline) {
216 2
            foreach (Steps::fullIter($pipeline->getSteps()) as $index => $step) {
217 2
                $return["{$id}:/step/{$index}"] = $step;
218
            }
219
        }
220
221 2
        return $return;
222
    }
223
224
    /**
225
     * step iterator w/services
226
     *
227
     * @param File $file
228
     *
229
     * @return Service[]|Step[]
230
     */
231 2
    private function getAllStepsWithServices(File $file)
232
    {
233 2
        $return = array();
234
235 2
        foreach ($this->getAllSteps($file) as $key => $step) {
236 2
            $return[$key] = $step;
237 2
            foreach ($step->getServices()->getDefinitions() as $name => $service) {
238 1
                $return["{$key}/service/{$name}"] = $service;
239
            }
240
        }
241
242 2
        return $return;
243
    }
244
}
245