Passed
Pull Request — master (#27)
by Keoghan
03:17 queued 23s
created

Site::getPorterLibrary()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace App\Models;
4
5
use App\Porter;
6
use App\PorterLibrary;
7
use App\Support\Contracts\Cli;
8
use App\Support\Nginx\SiteConfBuilder;
9
use App\Support\Ssl\CertificateBuilder;
10
use Illuminate\Database\Eloquent\Model;
11
12
class Site extends Model
13
{
14
    protected $guarded = [];
15
16
    /**
17
     * PHP Version.
18
     *
19
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
20
     */
21 3
    public function php_version()
22
    {
23 3
        return $this->belongsTo(PhpVersion::class);
24
    }
25
26
    /**
27
     * Resolve the site from the current working directory.
28
     *
29
     * @param null $path
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $path is correct as it would always require null to be passed?
Loading history...
30
     *
31
     * @return null
32
     */
33 4
    public static function resolveFromPathOrCurrentWorkingDirectory($path = null)
34
    {
35 4
        $name = static::nameFromPath($path ?: app(Cli::class)->currentWorkingDirectory());
36
37 4
        if (!$name) {
38
            return;
39
        }
40
41 4
        return static::where('name', $name)->first();
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::where('name', $name)->first() also could return the type App\Models\Site which is incompatible with the documented return type null.
Loading history...
42
    }
43
44
    /**
45
     * Resolve the site from the current working directory
46
     * Fail if not found.
47
     *
48
     * @param null $path
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $path is correct as it would always require null to be passed?
Loading history...
49
     *
50
     * @throws \Exception
51
     *
52
     * @return null
53
     */
54 1
    public static function resolveFromPathOrCurrentWorkingDirectoryOrFail($path = null)
55
    {
56 1
        $site = static::resolveFromPathOrCurrentWorkingDirectory($path);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $site is correct as static::resolveFromPathO...WorkingDirectory($path) targeting App\Models\Site::resolve...rrentWorkingDirectory() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
57
58 1
        if (!$site) {
0 ignored issues
show
introduced by
$site is of type null, thus it always evaluated to false.
Loading history...
59 1
            throw new \Exception('Site not found.');
60
        }
61
62 1
        return $site;
63
    }
64
65
    /**
66
     * Get the url for this site.
67
     *
68
     * @return string
69
     */
70 7
    public function getUrlAttribute()
71
    {
72 7
        return $this->name.'.'.setting('domain');
73
    }
74
75
    /**
76
     * Get the scheme for this site.
77
     *
78
     * @return string
79
     */
80 1
    public function getSchemeAttribute()
81
    {
82 1
        return ($this->secure ? 'https' : 'http').'://';
83
    }
84
85
    /**
86
     * Return the path for the NGiNX config file.
87
     *
88
     * @return string
89
     */
90 2
    public function getNginxConfPathAttribute()
91
    {
92 2
        return $this->getPorterLibrary()->configPath()."/nginx/conf.d/{$this->name}.conf";
93
    }
94
95
    /**
96
     * Return the full NGiNX template to use.
97
     *
98
     * @return string
99
     */
100 2
    public function getNginxConfTemplateAttribute()
101
    {
102 2
        $type = $this->nginx_conf ?? 'default';
103
104 2
        return "nginx.{$type}.domain".(($this->secure ?? false) ? '_secure' : '');
105
    }
106
107
    /**
108
     * Build the files for this site (e.g. nginx conf).
109
     *
110
     * @throws \Throwable
111
     */
112 6
    public function buildFiles()
113
    {
114 6
        $this->getSiteConfigBuilder()->build($this);
115 6
    }
116
117
    /**
118
     * Destroy the files for this site (e.g. NGiNX conf).
119
     */
120 1
    public function destroyFiles()
121
    {
122 1
        $this->getSiteConfigBuilder()->destroy($this);
123 1
    }
124
125
    /**
126
     * Secure the site. Build certs.
127
     */
128 1
    public function secure()
129
    {
130 1
        $this->buildCertificate();
131
132 1
        $this->update(['secure' => true]);
133
134 1
        $this->buildFiles();
135
136 1
        $this->getPorter()->restartServing();
137 1
    }
138
139
    /**
140
     * Unsecure this site.
141
     */
142 1
    public function unsecure()
143
    {
144 1
        $this->destroyCertificate();
145
146 1
        $this->update(['secure' => false]);
147
148 1
        $this->buildFiles();
149
150 1
        $this->getPorter()->restartServing();
151 1
    }
152
153
    /**
154
     * Remove this site and associated files.
155
     *
156
     * @throws \Exception
157
     * @throws \Throwable
158
     */
159 1
    public function remove()
160
    {
161 1
        $this->destroyCertificate();
162
163 1
        $this->getSiteConfigBuilder()->destroy($this);
164
165 1
        $this->delete();
166
167 1
        $this->getPorter()->restartServing();
168 1
    }
169
170
    /**
171
     * Set the PHP version for the site.
172
     *
173
     * @param int|null $phpVersionId
174
     *
175
     * @throws \Throwable
176
     */
177 2
    public function setPhpVersion($phpVersionId = null)
178
    {
179 2
        $this->update(['php_version_id' => $phpVersionId ?: PhpVersion::defaultVersion()->id]);
180
181 2
        $this->buildFiles();
182
183 2
        $this->getPorter()->restartServing();
184 2
    }
185
186
    /**
187
     * Set the nginx type for the site (we have different template configs we can use).
188
     *
189
     * @param $type
190
     *
191
     * @throws \Throwable
192
     */
193 1
    public function setNginxType($type)
194
    {
195 1
        $this->update(['nginx_conf' => $type ?? 'default']);
196
197 1
        $this->buildFiles();
198
199 1
        $this->getPorter()->restartServing();
200 1
    }
201
202
    /**
203
     * Get the first site based on name, or create a new record.
204
     *
205
     *
206
     * @param $name
207
     *
208
     * @return mixed
209
     */
210 1
    public static function firstOrCreateForName($name)
211
    {
212 1
        $result = static::where('name', $name)->first();
213
214 1
        if ($result) {
215 1
            return $result;
216
        }
217
218 1
        return static::createForName($name);
219
    }
220
221
    /**
222
     * Create an new site based on the name.
223
     *
224
     * @param $name
225
     *
226
     * @return mixed
227
     */
228 2
    public static function createForName($name)
229
    {
230 2
        return static::create([
231 2
            'name'           => $name,
232 2
            'nginx_conf'     => 'default',
233 2
            'php_version_id' => PhpVersion::defaultVersion()->id,
234
            'secure'         => false,
235
        ]);
236
    }
237
238
    /**
239
     * Get Certificate builder.
240
     *
241
     * @return CertificateBuilder
242
     */
243 5
    protected function getCertificateBuilder()
244
    {
245 5
        return app(CertificateBuilder::class);
246
    }
247
248
    /**
249
     * Get Site Config Builder.
250
     *
251
     * @return \App\Support\Nginx\SiteConfBuilder
252
     */
253 8
    protected function getSiteConfigBuilder()
254
    {
255 8
        return app(SiteConfBuilder::class);
256
    }
257
258
    /**
259
     * Get Porter.
260
     *
261
     * @return Porter
262
     */
263 6
    protected function getPorter()
264
    {
265 6
        return app(Porter::class);
266
    }
267
268
    /**
269
     * Get PorterLibrary.
270
     *
271
     * @return PorterLibrary
272
     */
273 2
    protected function getPorterLibrary()
274
    {
275 2
        return app(PorterLibrary::class);
276
    }
277
278
    /**
279
     * Build Certificate for site.
280
     */
281 2
    public function buildCertificate()
282
    {
283 2
        $this->getCertificateBuilder()->build($this->url);
284 2
    }
285
286
    /**
287
     * Destroy Certificate for site.
288
     */
289 3
    public function destroyCertificate()
290
    {
291 3
        $this->getCertificateBuilder()->destroy($this->url);
292 3
    }
293
294
    /**
295
     * Return a site directory name from a path, after checking it is within the
296
     * home directory.
297
     *
298
     * @param $path
299
     *
300
     * @return null|string
301
     */
302 6
    public static function nameFromPath($path)
303
    {
304 6
        $home = setting('home');
305
306 6
        if (strpos($path, $home) !== 0) {
307 1
            return;
308
        }
309
310 5
        $path = trim(str_after($path, $home), DIRECTORY_SEPARATOR);
311
312 5
        return str_before($path, DIRECTORY_SEPARATOR);
313
    }
314
}
315