Completed
Push — master ( 7a7da5...1e2def )
by Omar
02:35
created

Location::buildSequenceChainedCommands()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 10
rs 9.4285
nc 1
cc 1
eloc 6
nop 0
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
    public function __construct($directory)
51
    {
52
        $this->directory = $directory;
53
    }
54
55
    /**
56
     * @return Roster
57
     */
58
    public function getRoster()
59
    {
60
        return $this->roster;
61
    }
62
63
    /**
64
     * @param Roster $roster
65
     * @return $this
66
     */
67
    public function setRoster(Roster $roster)
68
    {
69
        $this->roster = $roster;
70
        return $this;
71
    }
72
73
    /**
74
     * @return string
75
     */
76
    public function getDirectory()
77
    {
78
        return $this->directory;
79
    }
80
81
    /**
82
     * @param string $directory
83
     * @return $this
84
     */
85
    public function setDirectory($directory)
86
    {
87
        $this->directory = $directory;
88
        return $this;
89
    }
90
91
    /**
92
     * @return string
93
     */
94
    public function getHashesRosterFileName()
95
    {
96
        return $this->hashesRosterFileName;
97
    }
98
99
    /**
100
     * @param string $hashesRosterFileName
101
     * @return $this
102
     */
103
    public function setHashesRosterFileName($hashesRosterFileName)
104
    {
105
        $this->hashesRosterFileName = $hashesRosterFileName;
106
        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
     * {@inheritdoc}
126
     */
127
    public function buildRoster()
128
    {
129
130
        /**
131
         * @var CommandChain $rosterListCommand
132
         */
133
        $rosterListCommand =$this->getRosterListCommand();
134
135
        list(
136
            $status,
137
            $output
138
        ) = $rosterListCommand->execute(true);
139
140
        if ($status) {
141
            $shellException = new ShellExecutionException(sprintf(
142
                "Failed to execute the shell script successfully:\n\t%s",
143
                $rosterListCommand->getCommand()
144
            ));
145
146
            $shellException->setOutput($output);
147
148
            throw $shellException;
149
        }
150
151
        $rosterItems = new ArrayCollection();
152
153
        $roster = new Roster();
154
        $roster->setLocation($this);
155
156
        foreach ($output as $line) {
157
            $hashAndFile = preg_split('/\s+/', $line);
158
159
            $item = new RosterItem();
160
            $item->setHash($hashAndFile[0]);
161
            $item->setRelativeFileName($hashAndFile[1]);
162
            $item->setRoster($roster);
163
164
            $rosterItems->set($hashAndFile[1], $item);
165
        }
166
167
        $roster->setRosterItems($rosterItems);
168
        $this->setRoster($roster);
169
170
        return $this->roster;
171
    }
172
173
174
    /**
175
     * @return CommandChain
176
     */
177
    protected function buildPipeChainedCommands()
178
    {
179
        /**
180
         * @var string $chainLink
181
         */
182
        $chainLink = ' | ';
183
184
        $pipeChainedCommands = new CommandChain($chainLink);
185
186
        $findCommand = new FindCommand();
187
        $findCommand->addParameter('.');
188
        $findCommand->addParameter('! -type d');
189
        $findCommand->addParameter('! -type l');
190
        $findCommand->addParameter('-print');
191
        $pipeChainedCommands->addCommand($findCommand);
192
193
        $sedCommand = new SedCommand();
194
        $sedCommand->addArgument('e', 's/[^[:alnum:]]/\\\\&/g');
195
        $pipeChainedCommands->addCommand($sedCommand);
196
197
        $sortCommand = new SortCommand();
198
        $pipeChainedCommands->addCommand($sortCommand);
199
200
        $sha1sumCommand = new Sha1SumCommand();
201
202
        $xargsCommand = new XargsCommand();
203
        $xargsCommand->addParameter('-n1');
204
        $xargsCommand->addParameter($sha1sumCommand->getCommand());
205
        $pipeChainedCommands->addCommand($xargsCommand);
206
207
        return $pipeChainedCommands;
208
    }
209
210
    /**
211
     * @return CommandChain
212
     */
213
    protected function buildSequenceChainedCommands()
214
    {
215
        $sequenceChainedCommands = new CommandChain(';');
216
217
        $cdCommand = new CdCommand();
218
        $cdCommand->addParameter($this->getDirectory());
219
        $sequenceChainedCommands->addCommand($cdCommand);
220
221
        return $sequenceChainedCommands;
222
    }
223
224
225
}
226