UnitsController   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 43
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A prepareUnits() 0 21 4
A actionPrepare() 0 16 2
1
<?php
2
/**
3
 * PHP Units of Measure Library
4
 *
5
 * @link      https://github.com/hiqdev/php-units
6
 * @package   php-units
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\units\yii2\console;
12
13
use Symfony\Component\Yaml\Yaml;
14
15
class UnitsController extends \yii\console\Controller
16
{
17
    public function actionPrepare()
18
    {
19
        $dir  = dirname(__DIR__, 2);
20
        $src  = "$dir/res/units-tree.yml";
21
        $tree = Yaml::parse(file_get_contents($src));
22
        $this->prepareUnits('', $tree);
23
        $dump = var_export($this->units, true);
24
25
        $dst = "$dir/res/units-tree.php";
26
        $old = file_get_contents($dst);
27
        $new = "<?php\n\nreturn $dump;\n";
28
        if ($old !== $new) {
29
            echo "Written units-tree.php\n";
30
            file_put_contents($dst, $new);
31
        }
32
    }
33
34
    protected $units = [];
35
36
    protected function prepareUnits($parent, $units)
37
    {
38
        foreach ($units as $name => $data) {
39
            if (is_array($data)) {
40
                $this->units[$name] = [
41
                    'parent' => $parent,
42
                    'factor' => 1,
43
                ];
44
                $this->prepareUnits($name, $data);
45
            } else {
46
                if ($name === $parent) {
47
                    $this->units[$name]['factor'] = $data;
48
                } else {
49
                    $this->units[$name] = [
50
                        'parent' => $parent,
51
                        'factor' => $data,
52
                    ];
53
                }
54
            }
55
        }
56
    }
57
}
58