Model::topicsInclude()   B
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 8.8571
cc 5
eloc 6
nc 3
nop 2
1
<?php
2
/**
3
 * Opine\PubSubBuild
4
 *
5
 * Copyright (c)2013, 2014 Ryan Mahoney, https://github.com/Opine-Org <[email protected]>
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
 * THE SOFTWARE.
24
 */
25
namespace Opine\PubSub;
26
27
use Exception;
28
use Symfony\Component\Yaml\Yaml;
29
30
class Model
31
{
32
    private $root;
33
    private $bundleModel;
34
35
    public function __construct($root, $bundleModel)
36
    {
37
        $this->root = $root;
38
        $this->bundleModel = $bundleModel;
39
        $this->cacheFile = $this->root.'/../var/cache/topics.json';
0 ignored issues
show
Bug introduced by
The property cacheFile does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
40
    }
41
42
    public function readDiskCache()
43
    {
44
        $topics = [];
0 ignored issues
show
Unused Code introduced by
$topics is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
45
        if (!file_exists($this->cacheFile)) {
46
            return [];
47
        }
48
        $topics = json_decode(file_get_contents($this->cacheFile), true);
49
        if (!isset($topics['topics'])) {
50
            return [];
51
        }
52
53
        return $topics['topics'];
54
    }
55
56
    public function build()
57
    {
58
        $topics = $this->topics();
59
        file_put_contents($this->cacheFile, json_encode($topics, JSON_PRETTY_PRINT));
60
61
        return $topics;
62
    }
63
64
    private function topics()
65
    {
66
        $config = [];
67
        $this->topicsInclude(__DIR__.'/../available/topics.yml', $config);
68
        $this->bundleTopicsInclude($config);
69
        foreach (glob($this->root.'/../config/topics/*.yml') as $filename) {
70
            $this->topicsInclude($filename, $config);
71
        }
72
73
        return $config;
74
    }
75
76
    private function topicsInclude($file, &$config)
77
    {
78
        $topics = $this->topicsRead($file);
79
        if (!isset($topics['topics']) || !is_array($topics['topics']) || count($topics['topics']) == 0) {
80
            return;
81
        }
82
        foreach ($topics['topics'] as $name => $topic) {
83
            $config['topics'][$name] = $topic;
84
        }
85
    }
86
87
    private function bundleTopicsInclude(&$config)
88
    {
89
        $bundles = $this->bundleModel->bundles();
90
        if (!is_array($bundles) || count($bundles) == 0) {
91
            return;
92
        }
93
        foreach ($bundles as $bundleName => $bundle) {
94
            foreach (glob($bundle['root'].'/../config/topics/*.yml') as $filename) {
95
                $this->topicsInclude($filename, $config);
96
            }
97
        }
98
    }
99
100
    private function topicsRead($topicConfig)
101
    {
102
        if (!file_exists($topicConfig)) {
103
            return;
104
        }
105
        $config = Yaml::parse(file_get_contents($topicConfig));
106
        if ($config == false) {
107
            throw new Exception('Can not parse YAML file: '.$topicConfig);
108
        }
109
110
        return $config;
111
    }
112
}
113