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

StepsInfo::annotate()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 2
rs 10
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\File\Info;
6
7
use Ktomk\Pipelines\File\Pipeline;
8
use Ktomk\Pipelines\File\Pipeline\Steps;
9
10
/**
11
 * info about steps (for show output)
12
 *
13
 * info about steps include {@see StepInfo}.
14
 */
15
final class StepsInfo implements \Countable
16
{
17
    /**
18
     * @var ?Steps
19
     */
20
    private $steps;
21
22 1
    public static function fromPipeline(Pipeline $pipeline = null)
23
    {
24 1
        return new self($pipeline ? $pipeline->getSteps() : null);
25
    }
26
27
    /**
28
     * @param ?Steps $steps
29
     */
30 1
    public function __construct(Steps $steps = null)
31
    {
32 1
        $this->steps = $steps;
33 1
    }
34
35
    /**
36
     * @return array|string[] image names (unique), w/o the default image name
37
     */
38 1
    public function getImageNames()
39
    {
40 1
        $images = array();
41 1
        foreach (new StepsStepInfoIterator($this->steps) as $info) {
42 1
            if (false === $info->hasDefaultImage()) {
43 1
                $images[] = $info->getImageName();
44
            }
45
        }
46
47 1
        return array_unique($images);
48
    }
49
50 1
    public function getImagesAsString($separator = ', ')
51
    {
52 1
        $images = $this->getImageNames();
53
54 1
        return $images ? implode($separator, $images) : '';
55
    }
56
57 1
    public function getSummary()
58
    {
59 1
        list($names, $annotations) = $this->getNamesAndAnnotations();
60
61 1
        return sprintf(
62 1
            '%d%s',
63 1
            $this->count(),
64 1
            $names ? ' (' . implode('; ', $this->annotate($names, ' *', $annotations)) . ')' : ''
65
        );
66
    }
67
68 1
    public function count()
69
    {
70 1
        return $this->steps ? count($this->steps) : 0;
71
    }
72
73
    /**
74
     * @return array [string[] images, string[] names, array<string>[] annotations]
75
     */
76 1
    private function getNamesAndAnnotations()
77
    {
78 1
        $names = array();
79 1
        $annotations = array();
80
81 1
        foreach (new StepsStepInfoIterator($this->steps) as $info) {
82 1
            $names[] = $info->getName();
83 1
            $annotations[] = $info->getAnnotations();
84
        }
85
86 1
        return array($names, $annotations);
87
    }
88
89
    /**
90
     * @param array<string> $names
91
     * @param string $annotator
92
     * @param array<string> $annotations
93
     *
94
     * @return array|string[]
95
     */
96 1
    private function annotate(array $names, $annotator, array $annotations)
97
    {
98 1
        return array_map(function ($name, $annotations) use ($annotator) {
99 1
            if ($annotations) {
100 1
                $name .= $annotator . implode('', $annotations);
101
            }
102
103 1
            return $name;
104 1
        }, $names, $annotations);
105
    }
106
}
107