Passed
Push — php7 ( 5be74d...d0ad73 )
by Pascal
02:05
created

DeviceList::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 11
ccs 8
cts 8
cp 1
crap 3
rs 10
1
<?php
2
3
namespace SSpkS\Device;
4
5
use \Symfony\Component\Yaml\Yaml;
6
use \Symfony\Component\Yaml\Exception\ParseException;
7
8
class DeviceList
9
{
10
    private $config;
11
    private $yamlFilepath;
12
    private $devices = array();
13
14
    /**
15
     * @param \SSpkS\Config $config Config object
16
     * @throws \Exception if file is not found or parsing error.
17
     */
18 5
    public function __construct(\SSpkS\Config $config)
19
    {
20 5
        $this->config = $config;
21 5
        $this->yamlFilepath = $this->config->paths['models'];
22 5
        if (!file_exists($this->yamlFilepath)) {
23 1
            throw new \Exception('DeviceList file ' . $this->yamlFilepath . ' not found!');
24
        }
25
        try {
26 4
            $this->parseYaml();
27 1
        } catch (\Exception $e) {
28 1
            throw $e;
29
        }
30 3
    }
31
32
    /**
33
     * Parse Yaml file with device data.
34
     *
35
     * @throws \Exception if Yaml couldn't be parsed.
36
     */
37 4
    private function parseYaml()
38
    {
39
        try {
40
            /** @var array $archlist */
41 4
            $archlist = Yaml::parse(file_get_contents($this->yamlFilepath));
42
        } catch (ParseException $e) {
43
            throw new \Exception($e->getMessage());
44
        }
45 4
        $idx = 0;
46 4
        $sortkey = array();
47 4
        if (!is_array($archlist) && !is_object($archlist))
0 ignored issues
show
introduced by
The condition is_array($archlist) is always true.
Loading history...
48
            throw new \Exception('Architectures list is not an array');
49 4
        foreach ($archlist as $family => $archlist) {
50 4
            if (!is_array($archlist) && !is_object($archlist))
51 1
                throw new \Exception("Models list in $family is not an array");
52 3
            foreach ($archlist as $arch => $archmodels) {
53 3
                foreach ($archmodels as $model) {
54 3
                    $this->devices[$idx] = array(
55 3
                        'arch' => $arch,
56 3
                        'name' => $model,
57 3
                        'family' => $family,
58
                    );
59 3
                    $sortkey[$idx] = $model;
60 3
                    $idx++;
61
                }
62
            }
63
        }
64 3
        array_multisort($sortkey, SORT_NATURAL | SORT_FLAG_CASE, $this->devices);
0 ignored issues
show
Bug introduced by
SSpkS\Device\SORT_NATURA...S\Device\SORT_FLAG_CASE cannot be passed to array_multisort() as the parameter $rest expects a reference. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
        array_multisort($sortkey, /** @scrutinizer ignore-type */ SORT_NATURAL | SORT_FLAG_CASE, $this->devices);
Loading history...
65 3
    }
66
67
    /**
68
     * Returns the architecture family for given $arch
69
     *
70
     * @param string $arch Architecture
71
     * @return string Family or $arch if not found
72
     */
73 1
    public function getFamily($arch)
74
    {
75 1
        foreach ($this->devices as $d) {
76 1
            if ($d['arch'] == $arch) {
77
                return $d['family'];
78
            }
79
        }
80 1
        return $arch;
81
    }
82
83
    /**
84
     * Returns the list of devices and their architectures.
85
     *
86
     * @return array List of devices and architectures.
87
     */
88 2
    public function getDevices()
89
    {
90 2
        return $this->devices;
91
    }
92
}
93