Passed
Pull Request — 2.x (#1446)
by Harings
13:09
created

Capsule   B

Complexity

Total Complexity 52

Size/Duplication

Total Lines 330
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 90
c 1
b 0
f 0
dl 0
loc 330
rs 7.44
wmc 52

45 Methods

Rating   Name   Duplication   Size   Complexity  
A getFormRequestClass() 0 3 1
A getConfig() 0 6 2
A getRevisionModel() 0 3 1
A getPsr4Path() 0 4 1
A getDatabaseNamespace() 0 3 1
A getModelNamespace() 0 4 1
A getBaseNamespace() 0 4 1
A getConfigFile() 0 3 1
A autoloadConfigFiles() 0 11 3
A registerConfig() 0 10 1
A loadMigrations() 0 10 2
A getPlural() 0 3 1
A getRepositoryClass() 0 3 1
A getSingular() 0 3 1
A loadTranslations() 0 9 2
A getMigrationsPath() 0 3 1
A routesFileExists() 0 3 1
A getBasePath() 0 4 1
A registerRoutes() 0 2 1
A getTranslationModel() 0 3 1
A getRepositoriesDir() 0 3 1
A getModule() 0 3 1
A getViewPrefix() 0 3 1
A getControllerClass() 0 3 1
A getType() 0 2 1
A getRepositoriesNamespace() 0 4 1
A getViewsPath() 0 3 1
A getRequestsNamespace() 0 4 1
A getSeedsPsr4Path() 0 3 1
A getResourcesPath() 0 3 1
A getSeedsNamespace() 0 3 1
A getModel() 0 3 1
A getModelsDir() 0 3 1
A getDatabasePsr4Path() 0 3 1
A boot() 0 15 2
A registerServiceProvider() 0 7 2
A getSlugModel() 0 3 1
A getControllersNamespace() 0 4 1
A getRequestsDir() 0 3 1
A getControllersDir() 0 3 1
A __construct() 0 16 1
A getDisplayName() 0 3 1
A getRoutesFile() 0 3 1
A registerViews() 0 3 1
A getLanguagesPath() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like Capsule often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Capsule, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace A17\Twill\Helpers;
4
5
use A17\Twill\Facades\TwillRoutes;
6
use Illuminate\Contracts\Translation\Translator;
7
use Illuminate\Database\Migrations\Migrator;
8
use Illuminate\Support\Facades\App;
9
use Illuminate\Support\Facades\Config;
10
use Illuminate\Support\Facades\File;
11
use Illuminate\Support\Facades\View;
12
use Illuminate\Support\Str;
13
14
class Capsule
15
{
16
    /**
17
     * @var string
18
     */
19
    public $name;
20
21
    /**
22
     * @var string
23
     */
24
    public $path;
25
26
    /**
27
     * @var bool
28
     */
29
    public $enabled;
30
31
    /**
32
     * @var bool
33
     */
34
    public $packageCapsule = false;
35
36
    /**
37
     * @var bool
38
     */
39
    public $loaded = false;
40
41
    /**
42
     * @var string|null
43
     */
44
    private $singular;
45
46
    /**
47
     * @var string
48
     */
49
    private $namespace;
50
51
    public function __construct(
52
        string $name,
53
        string $namespace,
54
        string $path,
55
        string $singular = null,
56
        bool $enabled = true,
57
        bool $packageCapsule = false
58
    ) {
59
        $this->name = $name;
60
        $this->path = $path;
61
        $this->enabled = $enabled;
62
        $this->namespace = $namespace;
63
        $this->singular = $singular;
64
        $this->packageCapsule = $packageCapsule;
65
66
        $this->boot();
67
    }
68
69
    public function boot(): void
70
    {
71
        $this->autoloadConfigFiles();
72
        $this->registerServiceProvider();
73
        $this->registerViews();
74
        $this->loadMigrations();
75
76
        if ($this->packageCapsule) {
77
            $this->registerConfig();
78
        }
79
80
        $this->registerRoutes();
81
        $this->loadTranslations();
82
83
        $this->loaded = true;
84
    }
85
86
    public function registerServiceProvider(): void
87
    {
88
        $serviceProviderName = $this->name . 'CapsuleServiceProvider';
89
90
        if (File::exists($this->path . '/' . $serviceProviderName . '.php')) {
91
            // @todo: test.
92
            App::register($this->namespace . '\\' . $serviceProviderName);
93
        }
94
    }
95
96
    public function autoloadConfigFiles(): void
97
    {
98
        $files = $this->getConfig()['autoload']['files'] ?? null;
99
100
        if (blank($files)) {
101
            return;
102
        }
103
104
        collect($files)->each(function ($file) {
105
            if (file_exists($file)) {
106
                require_once $file;
107
            }
108
        });
109
    }
110
111
    public function registerViews(): void
112
    {
113
        View::addLocation(Str::replaceLast('/' . $this->name, '', $this->path));
114
    }
115
116
    public function loadMigrations(): void
117
    {
118
        $callback = function (Migrator $migrator) {
119
            $migrator->path($this->getMigrationsPath());
120
        };
121
122
        App()->afterResolving('migrator', $callback);
123
124
        if (app()->resolved('migrator')) {
125
            $callback(App::make('migrator'));
126
        }
127
    }
128
129
    public function registerRoutes(): void {
130
        TwillRoutes::registerCapsuleRoutes(App::get('router'), $this);
131
    }
132
133
    public function loadTranslations(): void {
134
        $callback = function (Translator $translator) {
135
            $translator->addNamespace($this->getLanguagesPath(), 'twill:capsules:' . $this->getModule());
136
        };
137
138
        App()->afterResolving('translator', $callback);
139
140
        if (app()->resolved('translator')) {
141
            $callback(App::make('translator'));
142
        }
143
    }
144
145
    public function getBasePath(string $path): string
146
    {
147
        $exploded = explode('/', $path);
148
        return implode('/', array_pop($exploded));
149
    }
150
151
    public function getModule(): string
152
    {
153
        return Str::camel($this->name);
154
    }
155
156
    public function getDisplayName(): string
157
    {
158
        return Str::studly($this->name);
159
    }
160
161
    public function getPlural(): string
162
    {
163
        return $this->name;
164
    }
165
166
    public function getSingular(): string
167
    {
168
        return $this->singular ?? Str::singular($this->name);
169
    }
170
171
    public function getBaseNamespace(): string
172
    {
173
        $explodedNamespace = explode('\\', $this->namespace);
174
        return implode('\\', array_pop($explodedNamespace));
175
    }
176
177
    public function getDatabaseNamespace(): string
178
    {
179
        return $this->namespace . '\\Database';
180
    }
181
182
    public function getDatabasePsr4Path(): string
183
    {
184
        return $this->path . '/database';
185
    }
186
187
    public function getSeedsNamespace(): string
188
    {
189
        return $this->namespace . '\\Seeds\\Database';
190
    }
191
192
    public function getSeedsPsr4Path(): string
193
    {
194
        return $this->getDatabasePsr4Path() . '/seeds';
195
    }
196
197
    public function getMigrationsPath(): string
198
    {
199
        return $this->getDatabasePsr4Path() . '/migrations';
200
    }
201
202
    public function getResourcesPath(): string
203
    {
204
        return $this->getPsr4Path() . '/resources';
205
    }
206
207
    public function getLanguagesPath(): string
208
    {
209
        return $this->getResourcesPath() . '/lang';
210
    }
211
212
    public function getViewsPath(): string
213
    {
214
        return $this->getResourcesPath() . '/views';
215
    }
216
217
    public function getModelNamespace(): string
218
    {
219
        // @todo: config('twill.capsules.namespaces.models');
220
        return $this->namespace . '\\Models';
221
    }
222
223
    public function getModelsDir(): string
224
    {
225
        return $this->getPsr4Path() . '/Models';
226
    }
227
228
    public function getRepositoriesNamespace(): string
229
    {
230
        // @todo: config('twill.capsules.namespaces.repositories');
231
        return $this->namespace . '\\Repositories';
232
    }
233
234
    public function getRepositoriesDir(): string
235
    {
236
        return $this->getPsr4Path() . '/Repositories';
237
    }
238
239
    public function getControllersNamespace(): string
240
    {
241
        // @todo: config('twill.capsules.namespaces.controllers');
242
        return $this->namespace . '\\Http\\Controllers';
243
    }
244
245
    public function getControllersDir(): string
246
    {
247
        return $this->getPsr4Path() . '/Http/Controllers';
248
    }
249
250
    public function getRequestsNamespace(): string
251
    {
252
        // @todo: config('twill.capsules.namespaces.requests');
253
        return $this->namespace . '\\Http\\Requests';
254
    }
255
256
    public function getRequestsDir(): string
257
    {
258
        return $this->getPsr4Path() . '/Http/Requests';
259
    }
260
261
    public function getPsr4Path(): string
262
    {
263
        // @todo: config('twill.capsules.namespaces.subdir');
264
        return $this->path;
265
    }
266
267
    public function getViewPrefix(): string
268
    {
269
        return "{$this->getModule()}.resources.views.admin";
270
    }
271
272
    public function getRoutesFile(): string
273
    {
274
        return $this->getPsr4Path() . '/routes/admin.php';
275
    }
276
277
    public function routesFileExists(): bool
278
    {
279
        return file_exists($this->getRoutesFile());
280
    }
281
282
    public function getModel(): string
283
    {
284
        return $this->getModelNamespace() . '\\' . $this->getSingular();
285
    }
286
287
    public function getTranslationModel(): string
288
    {
289
        return $this->getModelNamespace() . '\\' . $this->getSingular() . 'Translation';
290
    }
291
292
    public function getSlugModel(): string
293
    {
294
        return $this->getModelNamespace() . '\\' . $this->getSingular() . 'Slug';
295
    }
296
297
    public function getRevisionModel(): string
298
    {
299
        return $this->getModelNamespace() . '\\' . $this->getSingular() . 'Revision';
300
    }
301
302
    public function getRepositoryClass(): string
303
    {
304
        return $this->getRepositoriesNamespace() . '\\' . $this->getSingular() . 'Repository';
305
    }
306
307
    public function getControllerClass(): string
308
    {
309
        return $this->getControllersNamespace() . '\\' . $this->getSingular() . 'Controller';
310
    }
311
312
    public function getFormRequestClass(): string
313
    {
314
        return $this->getRequestsNamespace() . '\\' . $this->getSingular() . 'Request';
315
    }
316
317
    public function getConfigFile(): string
318
    {
319
        return $this->path . '/config.php';
320
    }
321
322
    public function getConfig(): array
323
    {
324
        if (file_exists($this->getConfigFile())) {
325
            return require $this->getConfigFile();
326
        }
327
        return [];
328
    }
329
330
    public function registerConfig(): void
331
    {
332
        $config = Config::get('twill-navigation', []);
333
334
        $config[$this->name] = [
335
            'title' => $this->name,
336
            'module' => true,
337
        ];
338
339
        Config::set('twill-navigation', $config);
340
    }
341
342
    public function getType(): string {
343
        return '';
344
    }
345
}
346