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

Capsule   B

Complexity

Total Complexity 50

Size/Duplication

Total Lines 317
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 84
c 1
b 0
f 0
dl 0
loc 317
rs 8.4
wmc 50

44 Methods

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