Passed
Push — scrutinizer-test ( 6f2f69...953bdc )
by Pascal
02:22
created

DeviceList::parseYaml()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 16
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 24
ccs 19
cts 19
cp 1
crap 5
rs 9.4222
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 4
        } 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 4
        } catch (ParseException $e) {
43 1
            throw new \Exception($e->getMessage());
44
        }
45 3
        $idx = 0;
46 3
        $sortkey = array();
47 3
        foreach ($archlist as $family => $archlist) {
48 3
            foreach ($archlist as $arch => $archmodels) {
49 3
                foreach ($archmodels as $model) {
50 3
                    $this->devices[$idx] = array(
51 3
                        'arch'   => $arch,
52 3
                        'name'   => $model,
53 3
                        'family' => $family,
54
                    );
55 3
                    $sortkey[$idx] = $model;
56 3
                    $idx++;
57 3
                }
58 3
            }
59 3
        }
60 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

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