PhpVersion   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
eloc 18
dl 0
loc 127
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

11 Methods

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