BuildTypeFromSwVers::__invoke()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * Copyright (c) 2016-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 2016-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-operating-system
42
 */
43
44
namespace GanbaroDigital\OperatingSystem\OsType\ValueBuilders;
45
46
use GanbaroDigital\Filesystem\Checks\IsExecutableFile;
47
use GanbaroDigital\OperatingSystem\OsType\Values\OsType;
48
use GanbaroDigital\OperatingSystem\OsType\Values\OSX;
49
use GanbaroDigital\ProcessRunner\ProcessRunners\PopenProcessRunner;
50
use GanbaroDigital\TextTools\Editors\TrimWhitespace;
51
use GanbaroDigital\TextTools\Filters\FilterForMatchingString;
52
use GanbaroDigital\TextTools\Filters\FilterColumns;
53
54
class BuildTypeFromSwVers implements BuildTypeFromFile
55
{
56
    /**
57
     * use the output of /usr/bin/sw_vers (if present) to determine which
58
     * operating system we are using
59
     *
60
     * @param  string $pathToBinary
61
     *         path to the binary to run
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 know which operating system we are using
64
     *         null otherwise
65
     */
66
    public function __invoke($pathToBinary = "/usr/bin/sw_vers")
67
    {
68
        return self::usingPath($pathToBinary);
69
    }
70
71
    /**
72
     * use the output of /usr/bin/sw_vers (if present) to determine which
73
     * operating system we are using
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 know which operating system we are using
77
     *         null otherwise
78
     */
79
    public static function usingDefaultPath()
80
    {
81
        return self::usingPath("/usr/bin/sw_vers");
82
    }
83
84
    /**
85
     * use the output of /usr/bin/sw_vers (if present) to determine which
86
     * operating system we are using
87
     *
88
     * @param  string $pathToBinary
89
     *         path to the binary to run
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 know which operating system we are using
92
     *         null otherwise
93
     */
94
    public static function usingPath($pathToBinary)
95
    {
96
        list($productName, $productVersion) = self::getOsDetails($pathToBinary);
97
        if ($productName === null || $productVersion === null) {
98
            return null;
99
        }
100
101
        // do we have a match?
102
        if (!isset(self::$osTypes[$productName])) {
103
            return null;
104
        }
105
106
        /** @var OsType */
107
        $osType = new self::$osTypes[$productName]($productVersion);
108
        return $osType;
109
    }
110
111
    /**
112
     * call /usr/bin/sw_vers to get details about this operating system
113
     *
114
     * @param  string $pathToBinary
115
     *         path to the binary to run
116
     * @return array
117
     *         [0] is the operating system name
118
     *         [1] is the operating system version
119
     */
120
    private static function getOsDetails($pathToBinary)
121
    {
122
        $output = self::getOutputFromBinary($pathToBinary);
123
        if ($output === null) {
124
            return [null, null];
125
        }
126
127
        return self::extractOsDetails($output);
128
    }
129
130
    /**
131
     * call /usr/bin/sw_vers and return the output
132
     *
133
     * @param  string $pathToBinary
134
     *         path to the binary to call
135
     * @return string|null
136
     *         output from the binary
137
     */
138
    private static function getOutputFromBinary($pathToBinary)
139
    {
140
        // make sure we have an executable binary
141
        if (!IsExecutableFile::check($pathToBinary)) {
142
            return null;
143
        }
144
145
        // get the info
146
        $result = PopenProcessRunner::run([$pathToBinary]);
147
        if ($result->getReturnCode() !== 0) {
148
            return null;
149
        }
150
151
        // at this point, return the output
152
        return $result->getOutput();
153
    }
154
155
    /**
156
     * parse the output of /usr/bin/sw_vers to get details about
157
     * this operating system
158
     *
159
     * @param  string $output
160
     *         output from /usr/bin/sw_vers
161
     * @return array
162
     *         [0] is the operating system name
163
     *         [1] is the operating system version
164
     */
165
    private static function extractOsDetails($output)
166
    {
167
        // what do we have?
168
        $lines = explode(PHP_EOL, $output);
169
        $productName = self::extractField($lines, 'ProductName:');
170
        $productVersion = self::extractField($lines, 'ProductVersion:');
171
172
        return [$productName, $productVersion];
173
    }
174
175
    /**
176
     * extract a named field from the output of /usr/bin/sw_vers
177
     *
178
     * @param  array $lines
179
     *         the output of /usr/bin/sw_vers
180
     * @param  string $fieldName
181
     *         the field that we are looking for
182
     * @return string|null
183
     *         the value of the field (if found)
184
     */
185
    private static function extractField($lines, $fieldName)
186
    {
187
        $matches = FilterForMatchingString::against($lines, $fieldName);
188
        if (empty($matches)) {
189
            return null;
190
        }
191
        return TrimWhitespace::from(FilterColumns::from($matches[0], '1', ':'));
192
    }
193
194
    /**
195
     * map of operating system names to OsTypes
196
     * @var array
197
     */
198
    private static $osTypes = [
199
        'Mac OS X' => OSX::class,
200
    ];
201
}
202