Modular   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 237
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 8
dl 0
loc 237
rs 10
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A seed() 0 7 1
A loadRoutes() 0 12 1
A loadFactories() 0 8 1
A loadFactoryFile() 0 4 1
A loadServiceProviders() 0 7 1
A withRoutes() 0 4 1
A withFactories() 0 4 1
A withServiceProviders() 0 4 1
A withSeeders() 0 4 1
A filterActiveByMethod() 0 7 2
A all() 0 4 1
A active() 0 6 1
A modules() 0 8 2
A loadModules() 0 8 1
A find() 0 7 1
A exists() 0 4 1
1
<?php
2
3
namespace Mnabialek\LaravelModular\Services;
4
5
use Illuminate\Database\Seeder;
6
use Illuminate\Support\Collection;
7
use Illuminate\Contracts\Routing\Registrar;
8
use Mnabialek\LaravelModular\Models\Module;
9
use Mnabialek\LaravelModular\Traits\Replacer;
10
use Mnabialek\LaravelModular\Traits\Normalizer;
11
use Illuminate\Contracts\Foundation\Application;
12
13
class Modular
14
{
15
    use Replacer;
16
    use Normalizer;
17
18
    /**
19
     * @var Application
20
     */
21
    protected $app;
22
23
    /**
24
     * @var Collection|null
25
     */
26
    protected $modules = null;
27
28
    /**
29
     * @var Config
30
     */
31
    protected $config;
32
33
    /**
34
     * Modular constructor.
35
     *
36
     * @param Application $app
37
     * @param Config $config
38
     */
39
    public function __construct(Application $app, Config $config)
40
    {
41
        $this->app = $app;
42
        $this->config = $config;
43
    }
44
45
    /**
46
     * Runs main seeders for all active modules
47
     *
48
     * @param Seeder $seeder
49
     */
50
    public function seed(Seeder $seeder)
51
    {
52
        $this->withSeeders()->each(function ($module) use ($seeder) {
53
            /* @var Module $module */
54
            $seeder->call($module->seederClass());
55
        });
56
    }
57
58
    /**
59
     * Load routes for active modules
60
     *
61
     * @param Registrar $router
62
     * @param string|null $type
63
     */
64
    public function loadRoutes(Registrar $router, $type = null)
65
    {
66
        $this->withRoutes($type)->each(function ($module) use ($router, $type) {
67
            /* @var Module $module */
68
            $router->group(['namespace' => $module->routingControllerNamespace()],
69
                function ($router) use ($module, $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $router is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
70
                    $this->app['files']->getRequire($this->app->basePath() .
71
                        DIRECTORY_SEPARATOR .
72
                        $module->routesFilePath($module->routePrefix(compact('type'))));
73
                });
74
        });
75
    }
76
77
    /**
78
     * Load factories for active modules
79
     *
80
     * @param $factory
81
     */
82
    public function loadFactories($factory)
83
    {
84
        $this->withFactories()->each(function ($module) use ($factory) {
85
            /* @var Module $module */
86
            $this->loadFactoryFile($this->app->basePath() . DIRECTORY_SEPARATOR .
87
                $module->factoryFilePath(), $factory);
88
        });
89
    }
90
91
    /**
92
     * Load file
93
     *
94
     * @param string $file
95
     * @param $factory
96
     *
97
     * @codeCoverageIgnore
98
     */
99
    protected function loadFactoryFile($file, $factory)
100
    {
101
        require $file;
102
    }
103
104
    /**
105
     * Load service providers for active modules
106
     */
107
    public function loadServiceProviders()
108
    {
109
        $this->withServiceProviders()->each(function ($module) {
110
            /* @var Module $module */
111
            $this->app->register($module->serviceProviderClass());
112
        });
113
    }
114
115
    /**
116
     * Get all routable modules (active and having routes file)
117
     *
118
     * @param string $type
119
     *
120
     * @return array
121
     */
122
    public function withRoutes($type)
123
    {
124
        return $this->filterActiveByMethod('hasRoutes', compact('type'));
125
    }
126
127
    /**
128
     * Get all routable modules (active and having routes file)
129
     *
130
     * @return array
131
     */
132
    public function withFactories()
133
    {
134
        return $this->filterActiveByMethod('hasFactory');
135
    }
136
137
    /**
138
     * Get all modules that have service providers (active and having service
139
     * provider file)
140
     *
141
     * @return array
142
     */
143
    public function withServiceProviders()
144
    {
145
        return $this->filterActiveByMethod('hasServiceProvider');
146
    }
147
148
    /**
149
     * Get all modules that have seeders (active and having seeder file)
150
     *
151
     * @return array
152
     */
153
    public function withSeeders()
154
    {
155
        return $this->filterActiveByMethod('hasSeeder');
156
    }
157
158
    /**
159
     * Get active modules that also pass given requirement
160
     *
161
     * @param string $requirement
162
     *
163
     * @param array $data
164
     *
165
     * @return Collection
166
     */
167
    protected function filterActiveByMethod($requirement, array $data = [])
168
    {
169
        return $this->modules()
170
            ->filter(function ($module) use ($requirement, $data) {
171
                return $module->active() && $module->$requirement($data);
172
            })->values();
173
    }
174
175
    /**
176
     * Get all modules
177
     *
178
     * @return Collection
179
     */
180
    public function all()
181
    {
182
        return $this->modules();
183
    }
184
185
    /**
186
     * Get active modules
187
     *
188
     * @return Collection
189
     */
190
    public function active()
191
    {
192
        return $this->modules()->filter(function ($module) {
193
            return $module->active();
194
        })->values();
195
    }
196
197
    /**
198
     * Load modules (if not loaded) and get modules
199
     *
200
     * @return Collection
201
     */
202
    protected function modules()
203
    {
204
        if ($this->modules === null) {
205
            $this->loadModules();
206
        }
207
208
        return $this->modules;
209
    }
210
211
    /**
212
     * Load modules from config
213
     */
214
    protected function loadModules()
215
    {
216
        $this->modules = collect();
217
218
        collect($this->config->modules())->each(function ($options, $name) {
219
            $this->modules->push(new Module($name, $this->app, $options));
220
        });
221
    }
222
223
    /**
224
     * Find given module by name
225
     *
226
     * @param string $name
227
     *
228
     * @return Module
229
     */
230
    public function find($name)
231
    {
232
        return $this->modules()->first(function ($module) use ($name) {
233
            /* @var Module $module */
234
            return $module->name() == $name;
235
        });
236
    }
237
238
    /**
239
     * Verify whether module with given name already exists
240
     *
241
     * @param $name
242
     *
243
     * @return bool
244
     */
245
    public function exists($name)
246
    {
247
        return $this->find($name) !== null;
248
    }
249
}
250