Passed
Branch master (b0d099)
by Markus
03:49
created

DeviceList   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 60
rs 10
c 1
b 0
f 0
ccs 27
cts 27
cp 1
wmc 8
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
B parseYaml() 0 22 4
A getDevices() 0 4 1
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 $yamlFilepath;
11
    private $devices = array();
12
13
    /**
14
     * @param string $yamlFilepath Filename of Yaml file containing model list
15
     * @throws \Exception if file is not found or parsing error.
16
     */
17 4
    public function __construct($yamlFilepath)
18
    {
19 4
        $this->yamlFilepath = $yamlFilepath;
20 4
        if (!file_exists($this->yamlFilepath)) {
21 1
            throw new \Exception('DeviceList file ' . $this->yamlFilepath . ' not found!');
22
        }
23
        try {
24 3
            $this->parseYaml();
25 3
        } catch (\Exception $e) {
26 1
            throw $e;
27
        }
28 2
    }
29
30
    /**
31
     * Parse Yaml file with device data.
32
     *
33
     * @throws \Exception if Yaml couldn't be parsed.
34
     */
35 3
    private function parseYaml()
36
    {
37
        try {
38
            /** @var array $archlist */
39 3
            $archlist = Yaml::parse(file_get_contents($this->yamlFilepath));
40 3
        } catch (ParseException $e) {
41 1
            throw new \Exception($e->getMessage());
42
        }
43 2
        $idx = 0;
44 2
        $sortkey = array();
45 2
        foreach ($archlist as $arch => $archmodels) {
46 2
            foreach ($archmodels as $model) {
47 2
                $this->devices[$idx] = array(
48 2
                    'arch' => $arch,
49 2
                    'name' => $model,
50
                );
51 2
                $sortkey[$idx] = $model;
52 2
                $idx++;
53 2
            }
54 2
        }
55 2
        array_multisort($sortkey, SORT_NATURAL|SORT_FLAG_CASE, $this->devices);
56 2
    }
57
58
    /**
59
     * Returns the list of devices and their architectures.
60
     *
61
     * @return array List of devices and architectures.
62
     */
63 2
    public function getDevices()
64
    {
65 2
        return $this->devices;
66
    }
67
}
68