Passed
Push — master ( cb14e2...009e7e )
by Keoghan
03:39
created

PhpVersion::getList()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 1
nop 1
crap 2
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
     * Return a list of PHP versions and optionally highlight one.
34
     *
35
     * @param string|null $highlight
36
     *
37
     * @return mixed
38
     */
39 2
    public static function getList($highlight = null)
40
    {
41 2
        return static::pluck('version_number', 'id')
42
            ->map(function ($version) use ($highlight) {
43 2
                return $version.($version == $highlight ? ' (current)' : '');
44 2
            })->toArray();
45
    }
46
47
    /**
48
     * Get a safe version of the version number to use in paths.
49
     *
50
     * @return null|string
51
     */
52 5
    public function getSafeAttribute()
53
    {
54 5
        return static::cleanVersionNumber($this->version_number);
55
    }
56
57
    /**
58
     * Get a major version number.
59
     *
60
     * @return null|string
61
     */
62 1
    public function getMajorAttribute()
63
    {
64 1
        return str_before($this->version_number, '.');
65
    }
66
67
    /**
68
     * Get a short form version number with no separators.
69
     *
70
     * @return null|string
71
     */
72 1
    public function getShortFormAttribute()
73
    {
74 1
        return static::cleanVersionNumber($this->version_number, '');
75
    }
76
77
    /**
78
     * Get cli container name.
79
     *
80
     * @return null|string
81
     */
82 2
    public function getCliNameAttribute()
83
    {
84 2
        return 'php_cli_'.$this->safe;
85
    }
86
87
    /**
88
     * Get fpm container name.
89
     *
90
     * @return null|string
91
     */
92 2
    public function getFpmNameAttribute()
93
    {
94 2
        return 'php_fpm_'.$this->safe;
95
    }
96
97
    /**
98
     * Find by a user input version number.
99
     *
100
     * @param $number
101
     *
102
     * @return static|null
103
     */
104 1
    public static function findByDirtyVersionNumber($number)
105
    {
106 1
        return static::where('version_number', static::cleanVersionNumber($number, '.'))->first();
107
    }
108
109
    /**
110
     * Scope for the active PHP versions for porter - any used in sites plus default.
111
     *
112
     * @param $scope
113
     *
114
     * @return mixed
115
     */
116 6
    public function scopeActive($scope)
117
    {
118 6
        return $scope->where('default', true)->orWhereIn('id', Site::all()->pluck('php_version_id'));
119
    }
120
121
    /**
122
     * Clean php version number for file naming.
123
     *
124
     * @param $number
125
     * @param string $separator
126
     *
127
     * @return null|string
128
     */
129 7
    public static function cleanVersionNumber($number, $separator = '-')
130
    {
131 7
        return preg_replace('/[^\d]/', $separator, $number);
132
    }
133
}
134