Passed
Push — test ( a9e6d4...a1eca9 )
by Tom
02:30
created

FileShower   A

Complexity

Total Complexity 40

Size/Duplication

Total Lines 245
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 83
c 1
b 0
f 0
dl 0
loc 245
ccs 94
cts 94
cp 1
rs 9.2
wmc 40

12 Methods

Rating   Name   Duplication   Size   Complexity  
A showImages() 0 14 3
A showPipelineIds() 0 9 2
A tableFileStepsCaches() 0 10 3
A tableFileSteps() 0 9 3
A getImagesAndNames() 0 17 4
A getAllSteps() 0 10 3
A tableFileStepsServices() 0 6 3
A showPipelines() 0 16 5
A tableStepsServices() 0 15 5
A showFile() 0 12 3
A getAllStepsWithServices() 0 12 3
A showServices() 0 16 3

How to fix   Complexity   

Complex Class

Complex classes like FileShower often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use FileShower, and based on these observations, apply Extract Interface, too.

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
        $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
            $name = $step->getName();
133 3
            $name = null === $name ? 'no-name' : sprintf('"%s"', $name);
134
135 3
            $table->addRow(array($id, ++$index, $step->getImage(), $name));
136 3
            $this->tableFileStepsCaches($step, $id, $index, $table);
137 2
            $this->tableFileStepsServices($step, $id, $index, $table);
138
        }
139 2
    }
140
141
    /**
142
     * @param Step $step
143
     * @param string $id
144
     * @param int $stepNumber 1 based
145
     * @param FileTable $table
146
     *
147
     * @return void
148
     */
149 3
    private function tableFileStepsCaches(Step $step, $id, $stepNumber, FileTable $table)
150
    {
151 3
        $caches = $step->getCaches();
152 2
        $cacheDefinitions = $step->getFile()->getDefinitions()->getCaches();
153
154 2
        foreach ($caches->getNames() as $cacheName) {
155 2
            $cacheLabel = 'cache: ' . $cacheName;
156 2
            $definition = $cacheDefinitions->getByName($cacheName);
157 2
            $cacheDescription = true === $definition ? '*internal*' : $definition;
158 2
            $table->addRow(array($id, $stepNumber, $cacheLabel, $cacheDescription));
159
        }
160 2
    }
161
162
    /**
163
     * @param Step $step
164
     * @param string $id
165
     * @param int $stepNumber 1 based
166
     * @param FileTable $table
167
     *
168
     * @return void
169
     */
170 2
    private function tableFileStepsServices(Step $step, $id, $stepNumber, FileTable $table)
171
    {
172 2
        foreach ($step->getServices()->getServiceNames() as $serviceName) {
173
            /** @var Service $service */
174 1
            $service = $step->getFile()->getDefinitions()->getServices()->getByName($serviceName);
175 1
            $table->addFlaggedRow($service, array($id, $stepNumber, $service ? $service->getImage() : 'ERROR', 'service:' . $serviceName));
176
        }
177 2
    }
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 2
    }
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
    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
    /**
246
     * @param Steps $steps
247
     *
248
     * @return array
249
     */
250
    private function getImagesAndNames(Steps $steps = null)
251
    {
252 2
        $images = array();
253 2
        $names = array();
254
255 2
        foreach (Steps::fullIter($steps) as $step) {
256 2
            $image = $step->getImage()->getName();
257 2
            if (File::DEFAULT_IMAGE !== $image) {
258 2
                $images[] = $image;
259
            }
260 2
            $name = $step->getName();
261 2
            (null !== $name) && $names[] = $name;
262
        }
263
264 2
        $images = array_unique($images);
265
266 2
        return array($images, $names);
267
    }
268
}
269