Passed
Push — php7 ( 416ff6...9e646d )
by Pascal
02:15
created

DeviceList::getDevices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 $familylist */
41 4
            $familylist = 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
        foreach ($familylist as $family => $archlist) {
48 4
            if (!is_array($archlist) && !is_object($archlist))
49 1
                throw new \Exception("Models list in $family is not an array");
50 3
            foreach ($archlist as $arch => $archmodels) {
51 3
                foreach ($archmodels as $model) {
52 3
                    $this->devices[$idx] = array(
53 3
                        'arch' => $arch,
54 3
                        'name' => $model,
55 3
                        'family' => $family,
56
                    );
57 3
                    $sortkey[$idx] = $model;
58 3
                    $idx++;
59
                }
60
            }
61
        }
62 3
        array_multisort($sortkey/*, SORT_NATURAL | SORT_FLAG_CASE*/, $this->devices);
63 3
    }
64
65
    /**
66
     * Returns the architecture family for given $arch
67
     *
68
     * @param string $arch Architecture
69
     * @return string Family or $arch if not found
70
     */
71 1
    public function getFamily($arch)
72
    {
73 1
        foreach ($this->devices as $d) {
74 1
            if ($d['arch'] == $arch) {
75
                return $d['family'];
76
            }
77
        }
78 1
        return $arch;
79
    }
80
81
    /**
82
     * Returns the list of devices and their architectures.
83
     *
84
     * @return array List of devices and architectures.
85
     */
86 2
    public function getDevices()
87
    {
88 2
        return $this->devices;
89
    }
90
}
91