Completed
Push — develop ( f58326...d96df9 )
by Stuart
03:14
created

BuildTypeFromEtcRedhatRelease::matchTypeToRegex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 2
eloc 6
nc 2
nop 3
1
<?php
2
3
/**
4
 * Copyright (c) 2015-present Ganbaro Digital Ltd
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 *
11
 *   * Redistributions of source code must retain the above copyright
12
 *     notice, this list of conditions and the following disclaimer.
13
 *
14
 *   * Redistributions in binary form must reproduce the above copyright
15
 *     notice, this list of conditions and the following disclaimer in
16
 *     the documentation and/or other materials provided with the
17
 *     distribution.
18
 *
19
 *   * Neither the names of the copyright holders nor the names of his
20
 *     contributors may be used to endorse or promote products derived
21
 *     from this software without specific prior written permission.
22
 *
23
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34
 * POSSIBILITY OF SUCH DAMAGE.
35
 *
36
 * @category  Libraries
37
 * @package   OperatingSystem/OsType/ValueBuilders
38
 * @author    Stuart Herbert <[email protected]>
39
 * @copyright 2015-present Ganbaro Digital Ltd www.ganbarodigital.com
40
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
41
 * @link      http://code.ganbarodigital.com/php-text-tools
42
 */
43
44
namespace GanbaroDigital\OperatingSystem\OsType\ValueBuilders;
45
46
use GanbaroDigital\Filesystem\Requirements\RequireReadableFile;
47
use GanbaroDigital\OperatingSystem\OsType\Checks\HasEtcRedhatRelease;
48
use GanbaroDigital\OperatingSystem\OsType\Values\CentOS;
49
use GanbaroDigital\OperatingSystem\OsType\Values\OsType;
50
51
class BuildTypeFromEtcRedhatRelease
52
{
53
    /**
54
     * use /etc/redhat-release (if it exists) to work out what flavour of
55
     * RedHat Linux we are looking at
56
     *
57
     * @param  string $path
58
     *         path to the file to parse
59
     * @return null|OsType
0 ignored issues
show
Documentation introduced by
Should the return type not be null|object?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
60
     *         OsType if we can determine the operating system
61
     *         null if we cannot
62
     */
63
    public function __invoke($path = '/etc/redhat-release')
64
    {
65
        return self::from($path);
66
    }
67
68
    /**
69
     * use /etc/redhat-release (if it exists) to work out what flavour of
70
     * RedHat Linux we are looking at
71
     *
72
     * @return null|OsType
0 ignored issues
show
Documentation introduced by
Should the return type not be null|object?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
73
     *         OsType if we can determine the operating system
74
     *         null if we cannot
75
     */
76
    public function inDefaultLocation()
77
    {
78
        return self::from('/etc/redhat-release');
79
    }
80
81
    /**
82
     * use /etc/redhat-release (if it exists) to work out what flavour of
83
     * RedHat Linux we are looking at
84
     *
85
     * @param  string $path
86
     *         path to the file to parse
87
     * @return null|OsType
0 ignored issues
show
Documentation introduced by
Should the return type not be null|object?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
88
     *         OsType if we can determine the operating system
89
     *         null if we cannot
90
     */
91
    public static function from($path)
92
    {
93
        // make sure we have the file!
94
        if (!HasEtcRedhatRelease::check($path)) {
95
            return null;
96
        }
97
98
        // make sure the file is readable
99
        RequireReadableFile::check($path);
100
101
        // what do we have?
102
        $fileContents = file_get_contents($path);
103
104
        // do we have a match?
105
        foreach (self::$osTypes as $regex => $type) {
106
            if ($result = self::matchTypeToRegex($type, $regex, $fileContents)) {
107
                return $result;
108
            }
109
        }
110
111
        return null;
112
    }
113
114
    /**
115
     * does the given regex match our file
116
     *
117
     * @param  string $type
118
     *         the OsType class to return if the regex matches
119
     * @param  string $regex
120
     *         the regex to try
121
     * @param  string $fileContents
122
     *         the text that we apply the regex to
123
     * @return null|OsType
0 ignored issues
show
Documentation introduced by
Should the return type not be null|object?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
124
     *         OsType if the regex matches
125
     *         null otherwise
126
     */
127
    private static function matchTypeToRegex($type, $regex, $fileContents)
128
    {
129
        $matches=[];
130
        if (!preg_match($regex, $fileContents, $matches)) {
131
            return null;
132
        }
133
134
        // if we get here, we have a match
135
        /** @var OsType a type of operating system */
136
        $osType = new $type($matches['version']);
137
        return $osType;
138
139
    }
140
141
    /**
142
     * a map of regexes to OsType classes
143
     *
144
     * @var array
145
     */
146
    private static $osTypes = [
147
        "|^CentOS release (?<version>\d+\.\d+)|" => CentOS::class,
148
        "|^CentOS Linux release (?<version>\d+\.\d+)|" => CentOS::class,
149
    ];
150
}