Completed
Push — develop ( d96df9...2df89c )
by Stuart
02:49
created

BuildTypeFromEtcIssue::matchContentsToType()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 5
nc 3
nop 1
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
        return self::matchContentsToType($fileContents);
109
    }
110
111
    /**
112
     * do we have a regex that matches the contents of our file?
113
     *
114
     * @param  string $fileContents
115
     *         the contents of the file to check
116
     * @return null|OsType
0 ignored issues
show
Documentation introduced by
Should the return type not be object|null?

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...
117
     *         OsType if we can determine the operating system
118
     *         null if we cannot
119
     */
120
    private static function matchContentsToType($fileContents)
121
    {
122
        foreach (self::$osTypes as $regex => $type) {
123
            if ($result = self::matchTypeToRegex($type, $regex, $fileContents)) {
124
                return $result;
125
            }
126
        }
127
128
        return null;
129
    }
130
131
    /**
132
     * does the given regex match our file
133
     *
134
     * @param  string $type
135
     *         the OsType class to return if the regex matches
136
     * @param  string $regex
137
     *         the regex to try
138
     * @param  string $fileContents
139
     *         the text that we apply the regex to
140
     * @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...
141
     *         OsType if the regex matches
142
     *         null otherwise
143
     */
144
    private static function matchTypeToRegex($type, $regex, $fileContents)
145
    {
146
        $matches=[];
147
        if (!preg_match($regex, $fileContents, $matches)) {
148
            return null;
149
        }
150
151
        // if we get here, we have a match
152
        /** @var OsType a type of operating system */
153
        $osType = new $type($matches['version']);
154
        return $osType;
155
156
    }
157
158
    /**
159
     * a map of regexes to OsType classes
160
     *
161
     * @var array
162
     */
163
    private static $osTypes = [
164
        "|^CentOS release (?<version>\d+\.\d+)|" => CentOS::class,
165
        "|^Ubuntu (?<version>\d+\.\d+)|" => Ubuntu::class,
166
    ];
167
}