Installer   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 295
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 27
eloc 77
dl 0
loc 295
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A installViaComposer() 0 6 1
A setTimeout() 0 5 1
A run() 0 13 2
A setPath() 0 5 1
B getRepoUrl() 0 32 7
A getBranch() 0 3 2
A getPackageName() 0 7 2
A __construct() 0 6 1
A installViaSubtree() 0 10 1
A getDomainName() 0 5 1
A installViaGit() 0 9 1
A getDestinationPath() 0 7 2
A setConsole() 0 5 1
A setRepository() 0 5 1
A getProcess() 0 11 3
1
<?php
2
3
namespace Salah3id\Domains\Process;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Str;
7
use Salah3id\Domains\Contracts\RepositoryInterface;
8
use Symfony\Component\Process\Process;
9
10
class Installer
11
{
12
    /**
13
     * The domain name.
14
     *
15
     * @var string
16
     */
17
    protected $name;
18
19
    /**
20
     * The version of domain being installed.
21
     *
22
     * @var string
23
     */
24
    protected $version;
25
26
    /**
27
     * The domain repository instance.
28
     * @var \Salah3id\Domains\Contracts\RepositoryInterface
29
     */
30
    protected $repository;
31
32
    /**
33
     * The console command instance.
34
     *
35
     * @var \Illuminate\Console\Command
36
     */
37
    protected $console;
38
39
    /**
40
     * The destionation path.
41
     *
42
     * @var string
43
     */
44
    protected $path;
45
46
    /**
47
     * The process timeout.
48
     *
49
     * @var int
50
     */
51
    protected $timeout = 3360;
52
    /**
53
     * @var null|string
54
     */
55
    private $type;
56
    /**
57
     * @var bool
58
     */
59
    private $tree;
60
61
    /**
62
     * The constructor.
63
     *
64
     * @param string $name
65
     * @param string $version
66
     * @param string $type
67
     * @param bool   $tree
68
     */
69
    public function __construct($name, $version = null, $type = null, $tree = false)
70
    {
71
        $this->name = $name;
72
        $this->version = $version;
73
        $this->type = $type;
74
        $this->tree = $tree;
75
    }
76
77
    /**
78
     * Set destination path.
79
     *
80
     * @param string $path
81
     *
82
     * @return $this
83
     */
84
    public function setPath($path)
85
    {
86
        $this->path = $path;
87
88
        return $this;
89
    }
90
91
    /**
92
     * Set the domain repository instance.
93
     * @param \Salah3id\Domains\Contracts\RepositoryInterface $repository
94
     * @return $this
95
     */
96
    public function setRepository(RepositoryInterface $repository)
97
    {
98
        $this->repository = $repository;
99
100
        return $this;
101
    }
102
103
    /**
104
     * Set console command instance.
105
     *
106
     * @param \Illuminate\Console\Command $console
107
     *
108
     * @return $this
109
     */
110
    public function setConsole(Command $console)
111
    {
112
        $this->console = $console;
113
114
        return $this;
115
    }
116
117
    /**
118
     * Set process timeout.
119
     *
120
     * @param int $timeout
121
     *
122
     * @return $this
123
     */
124
    public function setTimeout($timeout)
125
    {
126
        $this->timeout = $timeout;
127
128
        return $this;
129
    }
130
131
    /**
132
     * Run the installation process.
133
     *
134
     * @return \Symfony\Component\Process\Process
135
     */
136
    public function run()
137
    {
138
        $process = $this->getProcess();
139
140
        $process->setTimeout($this->timeout);
141
142
        if ($this->console instanceof Command) {
0 ignored issues
show
introduced by
$this->console is always a sub-type of Illuminate\Console\Command.
Loading history...
143
            $process->run(function ($type, $line) {
144
                $this->console->line($line);
145
            });
146
        }
147
148
        return $process;
149
    }
150
151
    /**
152
     * Get process instance.
153
     *
154
     * @return \Symfony\Component\Process\Process
155
     */
156
    public function getProcess()
157
    {
158
        if ($this->type) {
159
            if ($this->tree) {
160
                return $this->installViaSubtree();
161
            }
162
163
            return $this->installViaGit();
164
        }
165
166
        return $this->installViaComposer();
167
    }
168
169
    /**
170
     * Get destination path.
171
     *
172
     * @return string
173
     */
174
    public function getDestinationPath()
175
    {
176
        if ($this->path) {
177
            return $this->path;
178
        }
179
180
        return $this->repository->getDomainPath($this->getDomainName());
181
    }
182
183
    /**
184
     * Get git repo url.
185
     *
186
     * @return string|null
187
     */
188
    public function getRepoUrl()
189
    {
190
        switch ($this->type) {
191
            case 'github':
192
                return "[email protected]:{$this->name}.git";
193
194
            case 'github-https':
195
                return "https://github.com/{$this->name}.git";
196
197
            case 'gitlab':
198
                return "[email protected]:{$this->name}.git";
199
200
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
201
202
            case 'bitbucket':
203
                return "[email protected]:{$this->name}.git";
204
205
            default:
206
207
                // Check of type 'scheme://host/path'
208
                if (filter_var($this->type, FILTER_VALIDATE_URL)) {
209
                    return $this->type;
210
                }
211
212
                // Check of type 'user@host'
213
                if (filter_var($this->type, FILTER_VALIDATE_EMAIL)) {
214
                    return "{$this->type}:{$this->name}.git";
215
                }
216
217
                return;
218
219
                break;
220
        }
221
    }
222
223
    /**
224
     * Get branch name.
225
     *
226
     * @return string
227
     */
228
    public function getBranch()
229
    {
230
        return is_null($this->version) ? 'master' : $this->version;
0 ignored issues
show
introduced by
The condition is_null($this->version) is always false.
Loading history...
231
    }
232
233
    /**
234
     * Get domain name.
235
     *
236
     * @return string
237
     */
238
    public function getDomainName()
239
    {
240
        $parts = explode('/', $this->name);
241
242
        return Str::studly(end($parts));
243
    }
244
245
    /**
246
     * Get composer package name.
247
     *
248
     * @return string
249
     */
250
    public function getPackageName()
251
    {
252
        if (is_null($this->version)) {
0 ignored issues
show
introduced by
The condition is_null($this->version) is always false.
Loading history...
253
            return $this->name . ':dev-master';
254
        }
255
256
        return $this->name . ':' . $this->version;
257
    }
258
259
    /**
260
     * Install the domain via git.
261
     *
262
     * @return \Symfony\Component\Process\Process
263
     */
264
    public function installViaGit()
265
    {
266
        return Process::fromShellCommandline(sprintf(
267
            'cd %s && git clone %s %s && cd %s && git checkout %s',
268
            base_path(),
269
            $this->getRepoUrl(),
270
            $this->getDestinationPath(),
271
            $this->getDestinationPath(),
272
            $this->getBranch()
273
        ));
274
    }
275
276
    /**
277
     * Install the domain via git subtree.
278
     *
279
     * @return \Symfony\Component\Process\Process
280
     */
281
    public function installViaSubtree()
282
    {
283
        return Process::fromShellCommandline(sprintf(
284
            'cd %s && git remote add %s %s && git subtree add --prefix=%s --squash %s %s',
285
            base_path(),
286
            $this->getDomainName(),
287
            $this->getRepoUrl(),
288
            $this->getDestinationPath(),
289
            $this->getDomainName(),
290
            $this->getBranch()
291
        ));
292
    }
293
294
    /**
295
     * Install the domain via composer.
296
     *
297
     * @return \Symfony\Component\Process\Process
298
     */
299
    public function installViaComposer()
300
    {
301
        return Process::fromShellCommandline(sprintf(
302
            'cd %s && composer require %s',
303
            base_path(),
304
            $this->getPackageName()
305
        ));
306
    }
307
}
308