Passed
Push — master ( b56d32...21ea90 )
by Markus
02:39
created

DeviceList::getFamily()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 9
ccs 0
cts 6
cp 0
crap 12
rs 9.6666
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 3
        } catch (\Exception $e) {
28 1
            throw $e;
29
        }
30 2
    }
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 3
        } catch (ParseException $e) {
43 1
            throw new \Exception($e->getMessage());
44
        }
45 2
        $idx = 0;
46 2
        $sortkey = array();
47 2
        foreach ($archlist as $family => $archlist) {
48 2
            foreach ($archlist as $arch => $archmodels) {
49 2
                foreach ($archmodels as $model) {
50 2
                    $this->devices[$idx] = array(
51 2
                        'arch'   => $arch,
52 2
                        'name'   => $model,
53 2
                        'family' => $family,
54
                    );
55 2
                    $sortkey[$idx] = $model;
56 2
                    $idx++;
57 2
                }
58 2
            }
59 2
        }
60 2
        array_multisort($sortkey, SORT_NATURAL | SORT_FLAG_CASE, $this->devices);
61 2
    }
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
    public function getFamily($arch)
70
    {
71
        foreach ($this->devices as $d) {
72
            if ($d['arch'] == $arch) {
73
                return $d['family'];
74
            }
75
        }
76
        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