DeviceList   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 91.43%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 35
c 3
b 0
f 0
dl 0
loc 82
ccs 32
cts 35
cp 0.9143
rs 10
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 3
B parseYaml() 0 27 7
A getFamily() 0 8 3
A getDevices() 0 3 1
1
<?php
2
3
namespace SSpkS\Device;
4
5
use \Symfony\Component\Yaml\Yaml;
0 ignored issues
show
Bug introduced by
The type \Symfony\Component\Yaml\Yaml was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use \Symfony\Component\Yaml\Exception\ParseException;
0 ignored issues
show
Bug introduced by
The type \Symfony\Component\Yaml\Exception\ParseException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
            }
51 3
            foreach ($archlist as $arch => $archmodels) {
52 3
                foreach ($archmodels as $model) {
53 3
                    $this->devices[$idx] = array(
54 3
                        'arch' => $arch,
55 3
                        'name' => $model,
56 3
                        'family' => $family,
57
                    );
58 3
                    $sortkey[$idx] = $model;
59 3
                    $idx++;
60
                }
61
            }
62
        }
63 3
        array_multisort($sortkey/*, SORT_NATURAL | SORT_FLAG_CASE*/, $this->devices);
64 3
    }
65
66
    /**
67
     * Returns the architecture family for given $arch
68
     *
69
     * @param string $arch Architecture
70
     * @return string Family or $arch if not found
71
     */
72 1
    public function getFamily($arch)
73
    {
74 1
        foreach ($this->devices as $d) {
75 1
            if ($d['arch'] == $arch) {
76
                return $d['family'];
77
            }
78
        }
79 1
        return $arch;
80
    }
81
82
    /**
83
     * Returns the list of devices and their architectures.
84
     *
85
     * @return array List of devices and architectures.
86
     */
87 2
    public function getDevices()
88
    {
89 2
        return $this->devices;
90
    }
91
}
92