Test Failed
Push — php7 ( f2a422...5be74d )
by Pascal
02:22
created

DeviceList::parseYaml()   B

Complexity

Conditions 9
Paths 3

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 9.111

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 18
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 28
ccs 16
cts 18
cp 0.8889
crap 9.111
rs 8.0555
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 4
    public function __construct(\SSpkS\Config $config)
19
    {
20 4
        $this->config = $config;
21 4
        $this->yamlFilepath = $this->config->paths['models'];
22 4
        if (!file_exists($this->yamlFilepath)) {
23 1
            throw new \Exception('DeviceList file ' . $this->yamlFilepath . ' not found!');
24
        }
25
        try {
26 3
            $this->parseYaml();
27
        } catch (\Exception $e) {
28
            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 3
    private function parseYaml()
38
    {
39
        try {
40
            /** @var array $archlist */
41 3
            $archlist = Yaml::parse(file_get_contents($this->yamlFilepath));
42
        } catch (ParseException $e) {
43
            throw new \Exception($e->getMessage());
44
        }
45 3
        $idx = 0;
46 3
        $sortkey = array();
47 3
        if (is_array($archlist) || is_object($archlist)) {
0 ignored issues
show
introduced by
The condition is_array($archlist) is always true.
Loading history...
48 3
            foreach ($archlist as $family => $archlist) {
49 3
                if (is_array($archlist) || is_object($archlist)) {
50 2
                    foreach ($archlist as $arch => $archmodels) {
51 2
                        foreach ($archmodels as $model) {
52 2
                            $this->devices[$idx] = array(
53 2
                                'arch' => $arch,
54 2
                                'name' => $model,
55 2
                                'family' => $family,
56
                            );
57 2
                            $sortkey[$idx] = $model;
58 2
                            $idx++;
59
                        }
60
                    }
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
    public function getFamily($arch)
74
    {
75
        foreach ($this->devices as $d) {
76
            if ($d['arch'] == $arch) {
77
                return $d['family'];
78
            }
79
        }
80
        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