Passed
Push — master ( 6e4265...822c3c )
by Gabriel
01:38
created

ModulesManager::hasModule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 array
19
     */
20 1
    public function getNames()
21
    {
22 1
        return $this->getModules()->values();
23
    }
24
25
    /**
26
     * @param $name
27
     * @return bool
28
     */
29 1
    public function hasModule($name)
30
    {
31 1
        return $this->getModules()->has($name);
32
    }
33
34
    /**
35
     * @param $name
36
     */
37
    public function addModule($name)
38
    {
39
        if (!$this->getModules()->offsetExists($name)) {
40
            $this->getModules()->set($name, $name);
41
        }
42
    }
43
44
    /**
45
     * @param $name
46
     *
47
     * @return string
48
     */
49
    public function getViewPath($name)
50
    {
51
        return $this->getModuleDirectory($name) . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR;
52
    }
53
54
    /**
55
     * @param $name
56
     *
57
     * @return string
58
     */
59
    public function getModuleDirectory($name)
60
    {
61
        return $this->getModulesBaseDirectory() . $name;
62
    }
63
64
    /**
65
     * @return string
66
     */
67
    public function getModulesBaseDirectory()
68
    {
69
        return defined('MODULES_PATH') ?
70
            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...
71
            : 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

71
            : /** @scrutinizer ignore-type */ app('path') . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR;
Loading history...
72
    }
73
74
    /**
75
     * @return ModulesCollection
76
     */
77 3
    public function getModules(): ModulesCollection
78
    {
79 3
        if ($this->modules === null) {
80 3
            $this->modules = new ModulesCollection();
81 3
            $this->loadFromConfig();
82
        }
83 3
        return $this->modules;
84
    }
85
86
    /**
87
     * @param ModulesCollection $sections
88
     */
89
    public function setModules(ModulesCollection $sections): void
90
    {
91
        $this->modules = $sections;
92
    }
93
94 3
    protected function loadFromConfig()
95
    {
96 3
        if (function_exists('config')) {
97 3
            $data = config('mvc.modules', ['admin', 'frontend']);
98 3
            foreach ($data as $key => $row) {
99 3
                $this->modules->set($row, $row);
100
            }
101
        }
102 3
    }
103
}
104