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

RemoteLocation::getRosterListCommand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 9.3142
nc 2
cc 2
eloc 8
nop 0
1
<?php
2
3
namespace TemplesOfCode\CodeSanity\Location;
4
5
use TemplesOfCode\CodeSanity\RemoteConnection;
6
use TemplesOfCode\CodeSanity\Location;
7
use TemplesOfCode\Sofa\Command\ShellCommand;
8
use TemplesOfCode\Sofa\CommandChain;
9
10
/**
11
 * Class RemoteLocation
12
 * @package TemplesOfCode\CodeSanity\Location
13
 */
14
class RemoteLocation extends Location
15
{
16
    /**
17
     * @var RemoteConnection
18
     */
19
    protected $remoteConnection = null;
20
21
    /**
22
     * @return RemoteConnection
23
     */
24
    public function getRemoteConnection()
25
    {
26
        return $this->remoteConnection;
27
    }
28
29
    /**
30
     * @param RemoteConnection $remoteConnection
31
     * @return $this
32
     */
33
    public function setRemoteConnection(RemoteConnection $remoteConnection)
34
    {
35
        $this->remoteConnection = $remoteConnection;
36
        return $this;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function isValid()
43
    {
44
        /**
45
         * @var RemoteConnection|null $remoteConnection
46
         */
47
        $remoteConnection = $this->getRemoteConnection();
48
        if (empty($remoteConnection)) {
49
            return false;
50
        }
51
52
        /**
53
         * @var bool $validRemoteConnection
54
         */
55
        $validRemoteConnection = $this
56
            ->remoteConnection
57
            ->isValid()
58
        ;
59
        
60
        if (!$validRemoteConnection) {
61
            return false;
62
        }
63
        /**
64
         * @var bool $validRemoteLocation
65
         */
66
        $validRemoteLocation = $this->isValidRemoteDirectory();
67
        if (!$validRemoteLocation) {
68
            return false;
69
        }
70
71
        return true;
72
    }
73
74
    /**
75
     * @return bool
76
     */
77
    public function isValidRemoteDirectory()
78
    {
79
        /**
80
         * @var RemoteConnection|null $remoteConnection
81
         */
82
        $remoteConnection = $this->getRemoteConnection();
83
        if (empty($remoteConnection)) {
84
            return false;
85
        }
86
87
        /**
88
         * @var CommandChain $commandChain
89
         */
90
        $commandChain = $this->buildTestCommandChain($remoteConnection);
91
92
        /**
93
         * @var int $exitStatus
94
         */
95
        list(
96
            $exitStatus
97
        ) = $commandChain->execute(false);
98
99
        if ($exitStatus) {
100
            return false;
101
        }
102
103
        return true;
104
    }
105
106
    /**
107
     * @param RemoteConnection $remoteConnection
108
     * @return CommandChain
109
     */
110
    protected function buildTestCommandChain(RemoteConnection $remoteConnection)
111
    {
112
        /**
113
         * @var ShellCommand $sshCommand
114
         */
115
        $sshCommand = $remoteConnection->getCommand(true);
116
117
        $sshCommand->addParameter(sprintf(
118
            '"test -w %s"',
119
            $this->directory
120
        ));
121
122
        $sequencedCommandChain = new CommandChain(';');
123
        $sequencedCommandChain
124
            ->addCommand($sshCommand)
125
        ;
126
127
        return $sequencedCommandChain;
128
    }
129
130
    /**
131
     * @return CommandChain
132
     */
133
    public function getRosterListCommand()
134
    {
135
        if (!$this->isValid()) {
136
            throw new \InvalidArgumentException(sprintf(
137
                "Remote location validation failed for Location with directory '%s'",
138
                $this->directory
139
            ));
140
        }
141
142
        /**
143
         * @var RemoteConnection $remoteConnection
144
         */
145
        $remoteConnection = $this->getRemoteConnection();
146
147
        /**
148
         * @var CommandChain $sshCommandChain
149
         */
150
        $sshCommandChain = $this->buildSshCommandChain($remoteConnection);
151
152
        return $sshCommandChain;
153
    }
154
155
    /**
156
     * Auxiliary function.
157
     *
158
     * @param RemoteConnection $remoteConnection
159
     * @return ShellCommand
160
     */
161
    protected function buildSshCommandChain(RemoteConnection $remoteConnection)
162
    {
163
        /**
164
         * @var CommandChain $commandChain
165
         */
166
        $commandChain = $this->buildRemoteCommandChain();
167
168
        $sshCommand = $remoteConnection->getCommand(true);
169
        $sshCommand->addParameter(sprintf(
170
            '"%s"',
171
            $commandChain->getCommand()
172
        ));
173
174
        return $sshCommand;
175
    }
176
177
    /**
178
     * Auxiliary function.
179
     * @return CommandChain
180
     */
181
    protected function buildRemoteCommandChain()
182
    {
183
        /**
184
         * @var CommandChain $pipeChainedCommands
185
         */
186
        $pipeChainedCommands = $this->buildPipeChainedCommands();
187
188
        /**
189
         * @var CommandChain $sequenceChainedCommands
190
         */
191
        $sequenceChainedCommands = $this->buildSequenceChainedCommands();
192
        $sequenceChainedCommands->addCommand($pipeChainedCommands);
193
194
        return $sequenceChainedCommands;
195
    }
196
197
    /**
198
     * {@inheritdoc}
199
     */
200
    public function getName()
201
    {
202
        if (empty($this->name)) {
203
            /**
204
             * @var RemoteConnection
205
             */
206
            $remoteConnection = $this->getRemoteConnection();
207
208
            /**
209
             * @var string
210
             */
211
            $this->name = sprintf(
212
                '%s@%s:%s',
213
                $remoteConnection->getUsername(),
214
                $remoteConnection->getHost(),
215
                $this->getDirectory()
216
            );
217
        }
218
        return $this->name;
219
    }
220
}
221