Completed
Push — master ( 1e2def...61d955 )
by Omar
02:27
created

Output::writeDiffItem()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 45
Code Lines 22

Duplication

Lines 19
Ratio 42.22 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 19
loc 45
rs 8.8571
cc 3
eloc 22
nc 4
nop 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
    public function __construct(ArrayCollection $differences, OutputInterface $output)
50
    {
51
        $this->differences = $differences;
52
        $this->output = $output;
53
54
        $this->init();
55
    }
56
57
    /**
58
     * @return boolean
59
     */
60
    public function isHeaderEnabled()
61
    {
62
        return $this->headerEnabled;
63
    }
64
65
    /**
66
     * @param boolean $headerEnabled
67
     * @return $this
68
     */
69
    public function setHeaderEnabled($headerEnabled)
70
    {
71
        $this->headerEnabled = $headerEnabled;
72
        return $this;
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    protected function getMask()
79
    {
80
        return $this->mask;
81
    }
82
83
    /**
84
     * @param DiffItem $diffItem
85
     */
86
    protected function writeDiffItem(DiffItem $diffItem)
87
    {
88
        $sot = $sotHash = 'Missing';
89
90
        /**
91
         * @var RosterItem $sotRosterItem
92
         */
93
        $sotRosterItem = $diffItem->getSotRosterItem();
94 View Code Duplication
        if (!empty($sotRosterItem)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
            /**
96
             * @var string $sotName
97
             */
98
            $sotName = $sotRosterItem->getRoster()->getLocation()->getName();
99
            $sotFileName = $sotRosterItem->getRelativeFileName();
100
            $sot = realpath($sotName . '/' . $sotFileName);
101
            $sotHash = $sotRosterItem->getHash();
102
        }
103
104
        $target = $targetHash = 'Missing';
105
106
        /**
107
         * @var RosterItem $targetRosterItem
108
         */
109
        $targetRosterItem = $diffItem->getTargetRosterItem();
110 View Code Duplication
        if (!empty($targetRosterItem)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
            /**
112
             * @var string $targetName
113
             */
114
            $targetName = $targetRosterItem->getRoster()->getLocation()->getName();
115
            $targetFileName = $targetRosterItem->getRelativeFileName();
116
            $target = realpath($targetName . '/' . $targetFileName);
117
118
            $targetHash = $targetRosterItem->getHash();
119
        }
120
121
        $line = sprintf(
122
            $this->getMask(),
123
            $sot,
124
            $sotHash,
125
            $target,
126
            $targetHash
127
        );
128
129
        $this->output->writeln($line);
130
    }
131
132
    /**
133
     *
134
     */
135
    abstract public function write();
136
137
    /**
138
     * Perform any logic at construction time.
139
     */
140
    abstract protected function init();
141
}
142