Location::buildRoster()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 50
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 50
ccs 28
cts 28
cp 1
rs 8.8571
cc 4
eloc 28
nc 4
nop 0
crap 4
1
<?php
2
3
namespace TemplesOfCode\CodeSanity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use TemplesOfCode\Sofa\Exception\ShellExecutionException;
7
use TemplesOfCode\Sofa\Command\FindCommand;
8
use TemplesOfCode\Sofa\Command\SedCommand;
9
use TemplesOfCode\Sofa\Command\SortCommand;
10
use TemplesOfCode\Sofa\Command\Sha1SumCommand;
11
use TemplesOfCode\Sofa\Command\XargsCommand;
12
use TemplesOfCode\Sofa\CommandChain;
13
use TemplesOfCode\Sofa\Command\CdCommand;
14
15
/**
16
 * Class Location
17
 * @package TemplesOfCode\CodeSanity
18
 */
19
abstract class Location
20
{
21
    /**
22
     * @var string
23
     */
24
    protected $tempStorage = '/tmp';
25
26
    /**
27
     * @var Roster
28
     */
29
    protected $roster = null;
30
31
    /**
32
     * @var string
33
     */
34
    protected $directory;
35
36
    /**
37
     * @var string
38
     */
39
    protected $name;
40
41
    /**
42
     * @var string
43
     */
44
    protected $hashesRosterFileName = 'hashes.roster';
45
46
    /**
47
     * Location constructor.
48
     * @param string $directory
49
     */
50 39
    public function __construct($directory)
51
    {
52 39
        $this->directory = $directory;
53 39
    }
54
55
    /**
56
     * @return Roster
57
     */
58 7
    public function getRoster()
59
    {
60 7
        return $this->roster;
61
    }
62
63
    /**
64
     * @param Roster $roster
65
     * @return $this
66
     */
67 10
    public function setRoster(Roster $roster)
68
    {
69 10
        $this->roster = $roster;
70 10
        return $this;
71
    }
72
73
    /**
74
     * @return string
75
     */
76 17
    public function getDirectory()
77
    {
78 17
        return $this->directory;
79
    }
80
81
    /**
82
     * @param string $directory
83
     * @return $this
84
     */
85 1
    public function setDirectory($directory)
86
    {
87 1
        $this->directory = $directory;
88 1
        return $this;
89
    }
90
91
    /**
92
     * @return string
93
     */
94 1
    public function getHashesRosterFileName()
95
    {
96 1
        return $this->hashesRosterFileName;
97
    }
98
99
    /**
100
     * @param string $hashesRosterFileName
101
     * @return $this
102
     */
103 1
    public function setHashesRosterFileName($hashesRosterFileName)
104
    {
105 1
        $this->hashesRosterFileName = $hashesRosterFileName;
106 1
        return $this;
107
    }
108
109
    /**
110
     * @return bool
111
     */
112
    abstract public function isValid();
113
114
    /**
115
     * @return CommandChain
116
     */
117
    abstract protected function getRosterListCommand();
118
119
    /**
120
     * @return string
121
     */
122
    abstract public function getName();
123
124
    /**
125
     * @param RosterItem $rosterItem
126
     * @return string
127
     */
128
    abstract public function getFullPath(RosterItem $rosterItem);
129
130
    /**
131
     * {@inheritdoc}
132
     */
133 6
    public function buildRoster()
134
    {
135
136
        /**
137
         * @var CommandChain $rosterListCommand
138
         */
139 6
        $rosterListCommand = $this->getRosterListCommand();
140
141
        list(
142
            $status,
143
            $output
144 4
        ) = $rosterListCommand->execute(true);
145
146 4
        if ($status) {
147 2
            $shellException = new ShellExecutionException(sprintf(
148 2
                "Failed to execute the shell script successfully:\n\t%s",
149 2
                $rosterListCommand->getCommand()
150 2
            ));
151
152 2
            $shellException->setOutput($output);
153
154 2
            throw $shellException;
155
        }
156
157 2
        $rosterItems = new ArrayCollection();
158
159 2
        $roster = new Roster();
160 2
        $roster->setLocation($this);
161
162 2
        foreach ($output as $line) {
163 2
            $hashAndFile = preg_split('/\s+/', $line);
164
165 2
            $item = new RosterItem();
166 2
            $item->setHash($hashAndFile[0]);
167
168 2
            $fileName = $hashAndFile[1];
169 2
            if ('./' == substr($fileName, 0, 2)) {
170 2
                $fileName = substr($fileName, 2);
171 2
            }
172 2
            $item->setRelativeFileName($fileName);
173 2
            $item->setRoster($roster);
174
175 2
            $rosterItems->set($fileName, $item);
176 2
        }
177
178 2
        $roster->setRosterItems($rosterItems);
179 2
        $this->setRoster($roster);
180
181 2
        return $this->roster;
182
    }
183
184
185
    /**
186
     * @return CommandChain
187
     */
188 6
    protected function buildPipeChainedCommands()
189
    {
190
        /**
191
         * @var string $chainLink
192
         */
193 6
        $chainLink = ' | ';
194
195 6
        $pipeChainedCommands = new CommandChain($chainLink);
196
197 6
        $findCommand = new FindCommand();
198 6
        $findCommand->addParameter('.');
199 6
        $findCommand->addParameter('! -type d');
200 6
        $findCommand->addParameter('! -type l');
201 6
        $findCommand->addParameter('-print');
202 6
        $pipeChainedCommands->addCommand($findCommand);
203
204 6
        $sedCommand = new SedCommand();
205 6
        $sedCommand->addArgument('e', 's/[^[:alnum:]]/\\\\&/g');
206 6
        $pipeChainedCommands->addCommand($sedCommand);
207
208 6
        $sortCommand = new SortCommand();
209 6
        $pipeChainedCommands->addCommand($sortCommand);
210
211 6
        $sha1sumCommand = new Sha1SumCommand();
212
213 6
        $xargsCommand = new XargsCommand();
214 6
        $xargsCommand->addParameter('-n1');
215 6
        $xargsCommand->addParameter($sha1sumCommand->getCommand());
216 6
        $pipeChainedCommands->addCommand($xargsCommand);
217
218 6
        return $pipeChainedCommands;
219
    }
220
221
    /**
222
     * @return CommandChain
223
     */
224 7
    protected function buildSequenceChainedCommands()
225
    {
226 7
        $sequenceChainedCommands = new CommandChain(';');
227
228 7
        $cdCommand = new CdCommand();
229 7
        $cdCommand->addParameter($this->getDirectory());
230 7
        $sequenceChainedCommands->addCommand($cdCommand);
231
232 7
        return $sequenceChainedCommands;
233
    }
234
235
236
}
237