Output::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php
2
3
4
namespace TemplesOfCode\CodeSanity;
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Symfony\Component\Console\Output\OutputInterface;
7
8
/**
9
 * Class Output
10
 * @package TemplesOfCode\CodeSanity
11
 */
12
abstract class Output
13
{
14
    /**
15
     * @var array
16
     */
17
    protected static $header = [
18
        'Source of truth Location',
19
        'Source of truth File Hash',
20
        'Target Location',
21
        'Target File Hash'
22
    ];
23
24
    /**
25
     * @var string
26
     */
27
    protected $mask = '';
28
29
    /**
30
     * @var bool
31
     */
32
    protected $headerEnabled;
33
34
    /**
35
     * @var OutputInterface
36
     */
37
    protected  $output;
38
39
    /**
40
     * @var ArrayCollection
41
     */
42
    protected $differences;
43
44
    /**
45
     * Output constructor.
46
     * @param ArrayCollection $differences
47
     * @param OutputInterface $output
48
     */
49 6
    public function __construct(ArrayCollection $differences, OutputInterface $output)
50
    {
51 6
        $this->differences = $differences;
52 6
        $this->output = $output;
53
54 6
        $this->init();
55 6
    }
56
57
    /**
58
     * @return boolean
59
     */
60 1
    public function isHeaderEnabled()
61
    {
62 1
        return $this->headerEnabled;
63
    }
64
65
    /**
66
     * @param boolean $headerEnabled
67
     * @return $this
68
     */
69 4
    public function setHeaderEnabled($headerEnabled)
70
    {
71 4
        $this->headerEnabled = $headerEnabled;
72 4
        return $this;
73
    }
74
75
    /**
76
     * @return string
77
     */
78 3
    protected function getMask()
79
    {
80 3
        return $this->mask;
81
    }
82
83
    /**
84
     * @param DiffItem $diffItem
85
     */
86 3
    protected function writeDiffItem(DiffItem $diffItem)
87
    {
88 3
        $sot = $sotHash = 'Missing';
89
90
        /**
91
         * @var RosterItem $sotRosterItem
92
         */
93 3
        $sotRosterItem = $diffItem->getSotRosterItem();
94 3
        if (!empty($sotRosterItem)) {
95
            /**
96
             * @var string $sotHash
97
             * @var string $sot
98
             */
99
            list(
100
                $sot,
101
                $sotHash
102 3
            ) = $this->getRosterInfo($sotRosterItem);
103 3
        }
104
105 3
        $target = $targetHash = 'Missing';
106
107
        /**
108
         * @var RosterItem $targetRosterItem
109
         */
110 3
        $targetRosterItem = $diffItem->getTargetRosterItem();
111 3
        if (!empty($targetRosterItem)) {
112
            /**
113
             * @var string $targetHash
114
             * @var string $target
115
             */
116
            list(
117
                $target,
118
                $targetHash
119 3
            ) = $this->getRosterInfo($targetRosterItem);
120 3
        }
121
122 3
        $line = sprintf(
123 3
            $this->getMask(),
124 3
            $sot,
125 3
            $sotHash,
126 3
            $target,
127
            $targetHash
128 3
        );
129
130 3
        $this->output->writeln($line);
131 3
    }
132
133
    /**
134
     * @param RosterItem $rosterItem
135
     * @return array
136
     */
137 3
    private function getRosterInfo(RosterItem $rosterItem)
138
    {
139
        /**
140
         * @var Location $location
141
         */
142 3
        $location = $rosterItem->getRoster()->getLocation();
143
144
        /**
145
         * @var string $path
146
         */
147 3
        $path = $location->getFullPath($rosterItem);
148
149 3
        $hash = $rosterItem->getHash();
150
151
        return array(
152 3
            $path,
153
            $hash
154 3
        );
155
    }
156
157
    /**
158
     *
159
     */
160
    abstract public function write();
161
162
    /**
163
     * Perform any logic at construction time.
164
     */
165
    abstract protected function init();
166
}
167