Test Failed
Branch feature__set_up_scrutinizer (ea6624)
by Robin
06:04 queued 02:46
created

PhpVersion::getSafeAttribute()   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 Illuminate\Database\Eloquent\Model;
6
7
class PhpVersion extends Model
8
{
9
    protected $guarded = [];
10
11
    /**
12
     * Return the default version
13
     *
14
     * @return mixed
15
     */
16 4
    public static function defaultVersion()
17
    {
18 4
        return static::where('default', true)->first();
19
    }
20
21
    /**
22
     * Set the default PHP version
23
     *
24
     * @param $id
25
     */
26 1
    public static function setDefaultVersion($id)
27
    {
28 1
        static::where('default', true)->update(['default' => false]);
29 1
        static::find($id)->update(['default' => true]);
30 1
    }
31
32
    /**
33
     * Get a safe version of the version number to use in paths
34
     *
35
     * @return null|string
36
     */
37 5
    public function getSafeAttribute()
38
    {
39 5
        return static::cleanVersionNumber($this->version_number);
40
    }
41
42
    /**
43
     * Get a major version number
44
     *
45
     * @return null|string
46
     */
47 1
    public function getMajorAttribute()
48
    {
49 1
        return str_before($this->version_number, '.');
50
    }
51
52
    /**
53
     * Get a short form version number with no separators
54
     *
55
     * @return null|string
56
     */
57 1
    public function getShortFormAttribute()
58
    {
59 1
        return static::cleanVersionNumber($this->version_number, '');
60
    }
61
62
    /**
63
     * Get cli container name
64
     *
65
     * @return null|string
66
     */
67 2
    public function getCliNameAttribute()
68
    {
69 2
        return 'php_cli_'.$this->safe;
70
    }
71
72
    /**
73
     * Get fpm container name
74
     *
75
     * @return null|string
76
     */
77 2
    public function getFpmNameAttribute()
78
    {
79 2
        return 'php_fpm_'.$this->safe;
80
    }
81
82
    /**
83
     * Find by a user input version number
84
     *
85
     * @param $number
86
     * @return static|null
87
     */
88 1
    public static function findByDirtyVersionNumber($number)
89
    {
90 1
        return static::where('version_number', static::cleanVersionNumber($number, '.'))->first();
91
    }
92
93
    /**
94
     * Scope for the active PHP versions for porter - any used in sites plus default
95
     *
96
     * @param $scope
97
     * @return mixed
98
     */
99 5
    public function scopeActive($scope)
100
    {
101 5
        return $scope->where('default', true)->orWhereIn('id', Site::all()->pluck('php_version_id'));
102
    }
103
104
105
    /**
106
     * Clean php version number for file naming
107
     *
108
     * @param $number
109
     * @param string $separator
110
     * @return null|string
111
     */
112 7
    public static function cleanVersionNumber($number, $separator = '-')
113
    {
114 7
        return preg_replace('/[^\d]/', $separator, $number);
115
    }
116
}
117