Completed
Push — master ( 416b5c...1e089b )
by Fumio
07:08
created

Environment::addons()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Jumilla\Addomnipot\Laravel;
4
5
use Illuminate\Contracts\Foundation\Application;
6
7
class Environment
8
{
9
    /**
10
     * @return array
11
     */
12
    protected $addons = null;
13
14
    /**
15
     * @return array
16
     */
17
    protected $addonsPaths = [];
18
19
    /**
20
     * @var \Illuminate\Contracts\Foundation\Application
21
     */
22
    protected $app;
23
24
    /**
25
     * @param \Illuminate\Contracts\Foundation\Application $app
26
     */
27 28
    public function __construct(Application $app)
28
    {
29 28
        $this->app = $app;
30
31 28
        $this->makeAddonsPaths();
32 28
    }
33
34
    /**
35
     * @return void
36
     */
37 28
    private function makeAddonsPaths()
38
    {
39 28
        $addonsDirectoryPath = $this->path();
40
41 28
        $this->addonsPaths[''] = $addonsDirectoryPath;
42 28
        foreach ($this->app['config']->get('addon.additional_paths', []) as $name => $path) {
43
            $this->addonsPaths[$name] = $this->app->basePath().'/'.$path;
44 28
        }
45 28
    }
46
47
    /**
48
     * @param string $name
49
     *
50
     * @return string
51
     */
52 28
    public function path($name = null)
53
    {
54 28
        if ($name !== null) {
55 8
            return static::path().'/'.$name;
56
        } else {
57 28
            return $this->app->basePath().'/'.$this->app['config']->get('addon.path', 'addons');
58
        }
59
    }
60
61
    /**
62
     * @param string $name
63
     *
64
     * @return bool
65
     */
66 5
    public function exists($name)
67
    {
68 5
        return is_dir($this->path($name));
69
    }
70
71
    /**
72
     * @param string $relativeClassName
73
     *
74
     * @return string
75
     */
76 2
    public function classToPath($relativeClassName)
77
    {
78 2
        return str_replace('\\', '/', $relativeClassName).'.php';
79
    }
80
81
    /**
82
     * @param string $relativePath
83
     *
84
     * @return mixed
85
     */
86 2
    public function pathToClass($relativePath)
87
    {
88 2
        if (strpos($relativePath, '/') !== false) {
89 1
            $relativePath = dirname($relativePath).'/'.basename($relativePath, '.php');
90 1
        } else {
91 2
            $relativePath = basename($relativePath, '.php');
92
        }
93
94 2
        return str_replace('/', '\\', $relativePath);
95
    }
96
97
    /**
98
     * @return array
99
     */
100 5
    public function loadAddons()
101
    {
102 5
        $files = $this->app['files'];
103
104 5
        $addons = [];
105
106 5
        foreach ($this->addonsPaths as $path) {
107
            // make directory
108 5
            if (!$files->exists($path)) {
109
                $files->makeDirectory($path);
110
            }
111
112
            // load addons
113 5
            foreach ($files->directories($path) as $dir) {
114 2
                $addon = Addon::create($dir);
115
116 2
                $addons[$addon->name()] = $addon;
117 5
            }
118 5
        }
119
120 5
        return $addons;
121
    }
122
123
    /**
124
     * @return array
125
     */
126 5
    public function addons()
127
    {
128 5
        if ($this->addons === null) {
129 5
            $this->addons = $this->loadAddons();
130 5
        }
131
132 5
        return $this->addons;
133
    }
134
135
    /**
136
     * @return \Jumilla\Addomnipot\Laravel\Addons\Addon
137
     */
138 1
    public function addon($name)
139
    {
140 1
        return array_get($this->addons(), $name ?: '', null);
141
    }
142
143
    /**
144
     * @return array
145
     */
146 2
    public function addonConsoleCommands()
147
    {
148 2
        $commands = [];
149
150 2
        foreach ($this->addons() as $addon) {
151
            $commands = array_merge($commands, $addon->config('addon.console.commands', []));
152 2
        }
153
154 2
        return $commands;
155
    }
156
157
    /**
158
     * @return array
159
     */
160 1 View Code Duplication
    public function addonHttpMiddlewares()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
161
    {
162 1
        $middlewares = [];
163
164 1
        foreach ($this->addons() as $addon) {
165
            $middlewares = array_merge($middlewares, $addon->config('addon.http.middlewares', []));
166 1
        }
167
168 1
        return $middlewares;
169
    }
170
171
    /**
172
     * @return array
173
     */
174 1 View Code Duplication
    public function addonRouteMiddlewares()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175
    {
176 1
        $middlewares = [];
177
178 1
        foreach ($this->addons() as $addon) {
179
            $middlewares = array_merge($middlewares, $addon->config('addon.http.route_middlewares', []));
180 1
        }
181
182 1
        return $middlewares;
183
    }
184
}
185