Completed
Pull Request — master (#30)
by Moshe
01:21
created

AliasRecord::isContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace Consolidation\SiteAlias;
3
4
use Consolidation\Config\Config;
5
use Consolidation\Config\ConfigInterface;
6
use Consolidation\Config\Util\ArrayUtil;
7
use Consolidation\SiteAlias\Util\FsUtils;
8
9
/**
10
 * An alias record is a configuration record containing well-known items.
11
 *
12
 * @see AliasRecordInterface for documentation
13
 */
14
class AliasRecord extends Config implements AliasRecordInterface
15
{
16
    /**
17
     * @var string
18
     */
19
    protected $name;
20
21
    /**
22
     * @inheritdoc
23
     */
24
    public function __construct(array $data = null, $name = '', $env = '')
25
    {
26
        parent::__construct($data);
27
        if (!empty($env)) {
28
            $name .= ".$env";
29
        }
30
        $this->name = $name;
31
    }
32
33
    /**
34
     * @inheritdoc
35
     */
36
    public function getConfig(ConfigInterface $config, $key, $default = null)
37
    {
38
        if ($this->has($key)) {
39
            return $this->get($key, $default);
40
        }
41
        return $config->get($key, $default);
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function name()
48
    {
49
        return $this->name;
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55
    public function setName($name)
56
    {
57
        $this->name = $name;
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63
    public function hasRoot()
64
    {
65
        return $this->has('root');
66
    }
67
68
    /**
69
     * @inheritdoc
70
     *
71
     * @throws \Exception when the alias does not specify a root.
72
     */
73
    public function root()
74
    {
75
        if (!$this->hasRoot()) {
76
            throw new \Exception('Site alias ' . $this->name . ' does not specify a root.');
77
        }
78
        $root = $this->get('root');
79
        if ($this->isLocal()) {
80
            return FsUtils::realpath($root);
81
        }
82
        return $root;
83
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88
    public function uri()
89
    {
90
        return $this->get('uri');
91
    }
92
93
    /**
94
     * @inheritdoc
95
     */
96
    public function setUri($uri)
97
    {
98
        return $this->set('uri', $uri);
99
    }
100
101
    /**
102
     * @inheritdoc
103
     */
104
    public function remoteHostWithUser()
105
    {
106
        $result = $this->remoteHost();
107
        if (!empty($result) && $this->hasRemoteUser()) {
108
            $result = $this->remoteUser() . '@' . $result;
109
        }
110
        return $result;
111
    }
112
113
    /**
114
     * @inheritdoc
115
     */
116
    public function remoteUser()
117
    {
118
        return $this->get('user');
119
    }
120
121
    /**
122
     * @inheritdoc
123
     */
124
    public function hasRemoteUser()
125
    {
126
        return $this->has('user');
127
    }
128
129
    /**
130
     * @inheritdoc
131
     */
132
    public function remoteHost()
133
    {
134
        return $this->get('host');
135
    }
136
137
    /**
138
     * @inheritdoc
139
     */
140
    public function isRemote()
141
    {
142
        return $this->has('host');
143
    }
144
145
    /**
146
     * @inheritdoc
147
     */
148
    public function isLocal()
149
    {
150
        return !$this->isRemote() && !$this->isContainer();
151
    }
152
153
    /**
154
     * @inheritdoc
155
     */
156
    public function isContainer()
157
    {
158
        return $this->has('docker');
159
    }
160
161
    /**
162
     * @inheritdoc
163
     */
164
    public function isNone()
165
    {
166
        return empty($this->root()) && $this->isLocal();
167
    }
168
169
    /**
170
     * @inheritdoc
171
     */
172
    public function localRoot()
173
    {
174
        if ($this->isLocal() && $this->hasRoot()) {
175
            return $this->root();
176
        }
177
178
        return false;
179
    }
180
181
    /**
182
     * os returns the OS that this alias record points to. For local alias
183
     * records, PHP_OS will be returned. For remote alias records, the
184
     * value from the `os` element will be returned. If there is no `os`
185
     * element, then the default assumption is that the remote system is Linux.
186
     *
187
     * @return string
188
     *   Linux
189
     *   WIN* (e.g. WINNT)
190
     *   CYGWIN
191
     *   MINGW* (e.g. MINGW32)
192
     */
193
    public function os()
194
    {
195
        if ($this->isLocal()) {
196
            return PHP_OS;
197
        }
198
        return $this->get('os', 'Linux');
199
    }
200
201
    /**
202
     * @inheritdoc
203
     */
204
    public function exportConfig()
205
    {
206
        return $this->remap($this->export());
207
    }
208
209
    /**
210
     * Reconfigure data exported from the form it is expected to be in
211
     * inside an alias record to the form it is expected to be in when
212
     * inside a configuration file.
213
     */
214
    protected function remap($data)
215
    {
216
        foreach ($this->remapOptionTable() as $from => $to) {
217
            if (isset($data[$from])) {
218
                unset($data[$from]);
219
            }
220
            $value = $this->get($from, null);
221
            if (isset($value)) {
222
                $data['options'][$to] = $value;
223
            }
224
        }
225
226
        return new Config($data);
227
    }
228
229
    /**
230
     * Fetch the parameter-specific options from the 'alias-parameters' section of the alias.
231
     * @param string $parameterName
232
     * @return array
233
     */
234
    protected function getParameterSpecificOptions($aliasData, $parameterName)
0 ignored issues
show
Unused Code introduced by
The parameter $aliasData is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
235
    {
236
        if (!empty($parameterName) && $this->has("alias-parameters.{$parameterName}")) {
237
            return $this->get("alias-parameters.{$parameterName}");
238
        }
239
        return [];
240
    }
241
242
    /**
243
     * Convert the data in this record to the layout that was used
244
     * in the legacy code, for backwards compatiblity.
245
     */
246
    public function legacyRecord()
247
    {
248
        $result = $this->exportConfig()->get('options', []);
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
249
250
        // Backend invoke needs a couple of critical items in specific locations.
251
        if ($this->has('paths.drush-script')) {
252
            $result['path-aliases']['%drush-script'] = $this->get('paths.drush-script');
253
        }
254
        if ($this->has('ssh.options')) {
255
            $result['ssh-options'] = $this->get('ssh.options');
256
        }
257
        return $result;
258
    }
259
260
    /**
261
     * Conversion table from old to new option names. These all implicitly
262
     * go in `options`, although they can come from different locations.
263
     */
264
    protected function remapOptionTable()
265
    {
266
        return [
267
            'user' => 'remote-user',
268
            # This remap is curently required for firing the RedispatchHook https://github.com/drush-ops/drush/blob/85826faff6ad5ae162af843e94df99157f8b717e/src/Runtime/RedispatchHook.php#L58-L66
269
            'docker.service' => 'remote-host',
270
            'host' => 'remote-host',
271
            'root' => 'root',
272
            'uri' => 'uri',
273
        ];
274
    }
275
}
276