Completed
Push — master ( 66cd91...6aa991 )
by Gabriel
01:42
created

ModulesManager   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 40%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 75
ccs 10
cts 25
cp 0.4
rs 10
wmc 12

7 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 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
    /**
19
     * @param $name
20
     */
21
    public function addModule($name)
22
    {
23
        if (!$this->getModules()->offsetExists($name)) {
24
            $this->getModules()->set($name, $name);
25
        }
26
    }
27
28
    /**
29
     * @param $name
30
     *
31
     * @return string
32
     */
33
    public function getViewPath($name)
34
    {
35
        return $this->getModuleDirectory($name) . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR;
36
    }
37
38
    /**
39
     * @param $name
40
     *
41
     * @return string
42
     */
43
    public function getModuleDirectory($name)
44
    {
45
        return $this->getModulesBaseDirectory() . $name;
46
    }
47
48
    /**
49
     * @return string
50
     */
51
    public function getModulesBaseDirectory()
52
    {
53
        return defined('MODULES_PATH') ?
54
            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...
55
            : 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

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