Passed
Push — master ( 37aa33...f3b4a1 )
by Gabriel
02:09
created

ModulesManager   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 44.44%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 82
ccs 12
cts 27
cp 0.4444
rs 10
c 1
b 0
f 0
wmc 13

8 Methods

Rating   Name   Duplication   Size   Complexity  
A loadFromConfig() 0 6 3
A getViewPath() 0 3 1
A addModule() 0 4 2
A getModules() 0 7 2
A getNames() 0 3 1
A getModuleDirectory() 0 3 1
A getModulesBaseDirectory() 0 5 2
A setModules() 0 3 1
1
<?php
2
3
namespace Nip\Mvc\Modules;
4
5
/**
6
 * Class ModulesManager
7
 * @package Nip\Mvc\Modules
8
 */
9
class ModulesManager
10
{
11
12
    /**
13
     * @var ModulesCollection
14
     */
15
    protected $modules = null;
16
17
    /**
18
     * @return ModulesCollection
19
     */
20 1
    public function getNames()
21
    {
22 1
        return $this->getModules()->all();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getModules()->all() returns the type array which is incompatible with the documented return type Nip\Mvc\Modules\ModulesCollection.
Loading history...
23
    }
24
25
    /**
26
     * @param $name
27
     */
28
    public function addModule($name)
29
    {
30
        if (!$this->getModules()->offsetExists($name)) {
31
            $this->getModules()->set($name, $name);
32
        }
33
    }
34
35
    /**
36
     * @param $name
37
     *
38
     * @return string
39
     */
40
    public function getViewPath($name)
41
    {
42
        return $this->getModuleDirectory($name) . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR;
43
    }
44
45
    /**
46
     * @param $name
47
     *
48
     * @return string
49
     */
50
    public function getModuleDirectory($name)
51
    {
52
        return $this->getModulesBaseDirectory() . $name;
53
    }
54
55
    /**
56
     * @return string
57
     */
58
    public function getModulesBaseDirectory()
59
    {
60
        return defined('MODULES_PATH') ?
61
            MODULES_PATH
0 ignored issues
show
Bug introduced by
The constant Nip\Mvc\Modules\MODULES_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
62
            : app('path') . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR;
0 ignored issues
show
Bug introduced by
Are you sure app('path') of type mixed|object can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
            : /** @scrutinizer ignore-type */ app('path') . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR;
Loading history...
63
    }
64
65
    /**
66
     * @return ModulesCollection
67
     */
68 2
    public function getModules(): ModulesCollection
69
    {
70 2
        if ($this->modules === null) {
71 2
            $this->modules = new ModulesCollection();
72 2
            $this->loadFromConfig();
73
        }
74 2
        return $this->modules;
75
    }
76
77
    /**
78
     * @param ModulesCollection $sections
79
     */
80
    public function setModules(ModulesCollection $sections): void
81
    {
82
        $this->modules = $sections;
83
    }
84
85 2
    protected function loadFromConfig()
86
    {
87 2
        if (function_exists('config')) {
88 2
            $data = config('mvc.modules', ['admin', 'frontend']);
89 2
            foreach ($data as $key => $row) {
90 2
                $this->modules->set($key, $row);
91
            }
92
        }
93 2
    }
94
}
95