Completed
Push — develop ( 1dfc31...7dc36c )
by Stuart
01:55
created

BuildTypeFromFiles::buildUsingBuilder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 2
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\OperatingSystems\OsType\Values\OsType;
47
48
class BuildTypeFromFiles
49
{
50
    /**
51
     * what kind of operating system are we running on?
52
     *
53
     * @param  array $pathsToFiles
54
     *         a list of paths to use for the supported builders
55
     *         use this to override the default paths where the builders
56
     *         search
57
     * @return OsType|null
58
     *         OsType if we know what kind of operating system we are running on
59
     *         null otherwise
60
     */
61
    public function __invoke($pathsToFiles = [])
62
    {
63
        return self::usingPaths($pathsToFiles);
64
    }
65
66
    /**
67
     * what kind of operating system are we running on?
68
     *
69
     * @return OsType|null
70
     *         OsType if we know what kind of operating system we are running on
71
     *         null otherwise
72
     */
73
    public function usingDefaultPaths()
74
    {
75
        return self::usingPaths();
76
    }
77
78
    /**
79
     * what kind of operating system are we running on?
80
     *
81
     * @param  array $pathsToFiles
82
     *         a list of paths to use for the supported builders
83
     *         use this to override the default paths where the builders
84
     *         search
85
     * @return OsType|null
86
     *         OsType if we know what kind of operating system we are running on
87
     *         null otherwise
88
     */
89
    public static function usingPaths($pathsToFiles = [])
90
    {
91
        foreach (self::$builders as $builderClass) {
92
            $retval = self::buildUsingBuilder($builderClass, $pathsToFiles);
93
            if ($retval) {
94
                return $retval;
95
            }
96
        }
97
98
        return null;
99
    }
100
101
    /**
102
     * use a specific builder to try and work out which operating system
103
     * we are on
104
     *
105
     * @param  string $builderClass
106
     *         the class to use as a builder
107
     * @param  array $pathsToFiles
108
     *         a list of paths to override
109
     * @return OsType|null
110
     *         OsType if we know what kind of operating system we are running on
111
     *         null otherwise
112
     */
113
    private static function buildUsingBuilder($builderClass, $pathsToFiles)
114
    {
115
        /** @var OsType **/
116
        $builder = new $builderClass;
117
        if (isset($pathsToFiles[$builderClass])) {
118
            return $builder($pathsToFiles[$builderClass]);
119
        }
120
        return $builder();
121
    }
122
123
    /**
124
     * a list of builders to try in a specific order
125
     *
126
     * @var array
127
     */
128
    private static $builders = [
129
        BuildTypeFromLsbRelease::class,
130
        BuildTypeFromEtcRedhatRelease::class,
131
        BuildTypeFromEtcIssue::class,
132
        BuildTypeFromSwVers::class,
133
    ];
134
}
135