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

BuildTypeFromEtcIssue::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\HasEtcIssue;
48
use GanbaroDigital\OperatingSystem\OsType\Values\CentOS;
49
use GanbaroDigital\OperatingSystem\OsType\Values\Debian;
50
use GanbaroDigital\OperatingSystem\OsType\Values\LinuxMint;
51
use GanbaroDigital\OperatingSystem\OsType\Values\OsType;
52
use GanbaroDigital\OperatingSystem\OsType\Values\Ubuntu;
53
54
class BuildTypeFromEtcIssue
55
{
56
    /**
57
     * use /etc/issue (if it exists) to work out what operating system we
58
     * are looking at
59
     *
60
     * @param  string $path
61
     *         path to the file to parse
62
     * @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...
63
     *         OsType if we can determine the operating system
64
     *         null if we cannot
65
     */
66
    public function __invoke($path = '/etc/issue')
67
    {
68
        return self::from($path);
69
    }
70
71
    /**
72
     * use /etc/issue (if it exists) to work out what operating system we
73
     * are looking at
74
     *
75
     * @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...
76
     *         OsType if we can determine the operating system
77
     *         null if we cannot
78
     */
79
    public function inDefaultLocation()
80
    {
81
        return self::from('/etc/issue');
82
    }
83
84
    /**
85
     * use /etc/issue (if it exists) to work out what operating system we
86
     * are looking at
87
     *
88
     * @param  string $path
89
     *         path to the file to parse
90
     * @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...
91
     *         OsType if we can determine the operating system
92
     *         null if we cannot
93
     */
94
    public static function from($path)
95
    {
96
        // make sure we have the file!
97
        if (!HasEtcIssue::check($path)) {
98
            return null;
99
        }
100
101
        // make sure the file is readable
102
        RequireReadableFile::check($path);
103
104
        // what do we have?
105
        $fileContents = file_get_contents($path);
106
107
        // do we have a match?
108
        foreach (self::$osTypes as $regex => $type) {
109
            if ($result = self::matchTypeToRegex($type, $regex, $fileContents)) {
110
                return $result;
111
            }
112
        }
113
114
        return null;
115
    }
116
117
    /**
118
     * does the given regex match our file
119
     *
120
     * @param  string $type
121
     *         the OsType class to return if the regex matches
122
     * @param  string $regex
123
     *         the regex to try
124
     * @param  string $fileContents
125
     *         the text that we apply the regex to
126
     * @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...
127
     *         OsType if the regex matches
128
     *         null otherwise
129
     */
130
    private static function matchTypeToRegex($type, $regex, $fileContents)
131
    {
132
        $matches=[];
133
        if (!preg_match($regex, $fileContents, $matches)) {
134
            return null;
135
        }
136
137
        // if we get here, we have a match
138
        /** @var OsType a type of operating system */
139
        $osType = new $type($matches['version']);
140
        return $osType;
141
142
    }
143
144
    /**
145
     * a map of regexes to OsType classes
146
     *
147
     * @var array
148
     */
149
    private static $osTypes = [
150
        "|^CentOS release (?<version>\d+\.\d+)|" => CentOS::class,
151
        "|^Ubuntu (?<version>\d+\.\d+)|" => Ubuntu::class,
152
    ];
153
}