Completed
Push — master ( 50fa0d...d5d074 )
by Fumio
06:54
created

Environment::addonHttpMiddlewares()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

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