Passed
Pull Request — 2.x (#729)
by Antonio Carlos
05:59
created

HasCapsules::makeCapsule()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 89
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 55
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 89
rs 8.9818

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace A17\Twill\Services\Capsules;
4
5
use Illuminate\Support\Str;
6
7
trait HasCapsules
8
{
9
    protected function getAutoloader()
10
    {
11
        return app()->bound('autoloader')
12
            ? app('autoloader')
13
            : require base_path('vendor/autoload.php');
14
    }
15
16
    public function getCapsuleList()
17
    {
18
        $path = config('twill.capsules.path');
19
20
        $list = collect(config('twill.capsules.list'));
21
22
        if (config('twill.capsules.loaded')) {
23
            return $list;
24
        }
25
26
        return $list
27
            ->where('enabled', true)
28
            ->map(function ($capsule) use ($path) {
29
                return $this->makeCapsule($capsule, $path);
30
            });
31
    }
32
33
    public function getCapsuleByModel($model)
34
    {
35
        return $this->getCapsuleList()
36
            ->where('singular', $model)
37
            ->first();
38
    }
39
40
    public function getCapsuleByModule($module)
41
    {
42
        return $this->getCapsuleList()
43
            ->filter(function ($capsule) use ($module) {
44
                return Str::lower($capsule['plural']) == Str::lower($module);
45
            })
46
            ->first();
47
    }
48
49
    public function makeCapsule($capsule, $basePath)
50
    {
51
        $capsule['name'] = Str::studly($capsule['name']);
52
53
        $capsule['module'] = Str::camel($capsule['name']);
54
55
        $capsule['plural'] = $name = $capsule['name'];
56
57
        $capsule['singular'] = $singular =
58
            $capsule['singular'] ?? Str::singular($name);
59
60
        $twillNamespace = config('twill.namespace');
0 ignored issues
show
Unused Code introduced by
The assignment to $twillNamespace is dead and can be removed.
Loading history...
61
62
        $capsule[
63
            'namespace'
64
        ] = $capsuleNamespace = $this->getManager()->capsuleNamespace(
0 ignored issues
show
Bug introduced by
The method capsuleNamespace() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
        ] = $capsuleNamespace = $this->getManager()->/** @scrutinizer ignore-call */ capsuleNamespace(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65
            $capsule['name']
66
        );
67
68
        $capsule[
69
            'database_namespace'
70
        ] = "$capsuleNamespace\Database";
71
72
        $capsule[
73
            'seeds_namespace'
74
        ] = "{$capsule['database_namespace']}\Seeds";
75
76
        $capsule['model'] = $capsule['models'] = $models =
77
            "{$capsuleNamespace}\\" .
78
            config('twill.capsules.namespaces.models');
79
        $capsule['repositories'] = $repositories =
80
            "{$capsuleNamespace}\\" .
81
            config('twill.capsules.namespaces.repositories');
82
        $capsule['controllers'] = $controllers =
83
            "{$capsuleNamespace}\\" .
84
            config('twill.capsules.namespaces.controllers');
85
        $capsule['requests'] = $requests =
86
            "{$capsuleNamespace}\\" .
87
            config('twill.capsules.namespaces.requests');
88
89
        $capsule['psr4_path'] = "$basePath/{$name}/app";
90
91
        $capsule['database_psr4_path'] = "$basePath/{$name}/database";
92
93
        $capsule['seeds_psr4_path'] = "{$capsule['database_psr4_path']}/seeds";
94
95
        $capsule['root_path'] = $root = $this->capsuleRootPath($capsule);
0 ignored issues
show
Unused Code introduced by
The assignment to $root is dead and can be removed.
Loading history...
96
97
        $capsule[
98
            'migrations_dir'
99
        ] = "{$capsule['root_path']}/database/migrations";
100
101
        $capsule['views_dir'] = "{$capsule['root_path']}/resources/views";
102
103
        $capsule['view_prefix'] = "{$name}.resources.views.admin";
104
105
        $capsule['routes_file'] = "{$capsule['root_path']}/routes/admin.php";
106
107
        $capsule['model'] = "{$models}\\{$singular}";
108
109
        $capsule['models_dir'] = $this->namespaceToPath($capsule, $models);
110
111
        $capsule['translation'] = "{$models}\\{$singular}Translation";
112
113
        $capsule['slug'] = "{$models}\\{$singular}Slug";
114
115
        $capsule['revision'] = "{$models}\\{$singular}Revision";
116
117
        $capsule['repository'] = "{$repositories}\\{$singular}Repository";
118
119
        $capsule['repositories_dir'] = $this->namespaceToPath(
120
            $capsule,
121
            $repositories
122
        );
123
124
        $capsule['controller'] = "{$controllers}\\{$singular}Controller";
125
126
        $capsule['controllers_dir'] = $this->namespaceToPath(
127
            $capsule,
128
            $controllers
129
        );
130
131
        $capsule['formRequest'] = "{$requests}\\{$singular}Request";
132
133
        $capsule['requests_dir'] = $this->namespaceToPath($capsule, $requests);
134
135
        $this->registerPsr4Autoloader($capsule);
136
137
        return $capsule;
138
    }
139
140
    public function registerPsr4Autoloader($capsule)
141
    {
142
        $this->getAutoloader()->setPsr4(
0 ignored issues
show
Bug introduced by
The method setPsr4() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

142
        $this->getAutoloader()->/** @scrutinizer ignore-call */ setPsr4(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
143
            $capsule['namespace'] . '\\',
144
            $capsule['psr4_path']
145
        );
146
147
        $this->getAutoloader()->setPsr4(
148
            $capsule['database_namespace'] . '\\',
149
            $capsule['database_psr4_path']
150
        );
151
152
        $this->getAutoloader()->setPsr4(
153
            $capsule['database_namespace'] . '\\Seeds\\',
154
            $capsule['database_psr4_path'] . '/seeds'
155
        );
156
    }
157
158
    public function capsuleRootPath($capsule)
159
    {
160
        return config('twill.capsules.path') . '/' . $capsule['name'] ?? null;
161
    }
162
163
    public function getCapsuleRepositoryClass($model)
164
    {
165
        return $this->getCapsuleByModel($model)['repository'] ?? null;
166
    }
167
168
    public function getCapsuleTranslationClass($model)
169
    {
170
        return $this->getCapsuleByModel($model)['translation'] ?? null;
171
    }
172
173
    public function getCapsuleSlugClass($model)
174
    {
175
        return $this->getCapsuleByModel($model)['slug'] ?? null;
176
    }
177
178
    public function getCapsuleRevisionClass($model)
179
    {
180
        return $this->getCapsuleByModel($model)['revision'] ?? null;
181
    }
182
183
    public function getCapsuleFormRequestClass($model)
184
    {
185
        return $this->getCapsuleByModel($model)['formRequest'] ?? null;
186
    }
187
188
    public function getCapsuleViewPrefix($capsule)
189
    {
190
        return $this->getCapsuleByModule(Str::studly($capsule))[
191
            'view_prefix'
192
        ] ?? null;
193
    }
194
195
    public function namespaceToPath($capsule, $namespace)
196
    {
197
        return $this->capsuleNamespaceToPath(
198
            $namespace,
199
            $capsule['namespace'],
200
            $capsule['root_path']
201
        );
202
    }
203
204
    public function capsuleNamespaceToPath(
205
        $namespace,
206
        $capsuleNamespace,
207
        $rootPath
208
    ) {
209
        $namespace = Str::after($namespace, $capsuleNamespace . '\\');
210
211
        $subdir = config('twill.capsules.namespaces.subdir');
212
213
        $subdir = filled($subdir) ? "{$subdir}/" : '';
214
215
        return "{$rootPath}/{$subdir}" . str_replace('\\', '/', $namespace);
216
    }
217
218
    public function getManager()
219
    {
220
        return $this->manager = $this->manager ?? app('twill.capsules.manager');
0 ignored issues
show
Bug Best Practice introduced by
The property manager does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
221
    }
222
223
    public function seedCapsules()
224
    {
225
        $this->getCapsuleList()->each(function ($capsule) {
226
            if (filled($seeder = $this->makeCapsuleSeeder($capsule))) {
227
                $seeder->__invoke();
0 ignored issues
show
Bug introduced by
The method __invoke() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

227
                $seeder->/** @scrutinizer ignore-call */ 
228
                         __invoke();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
228
            }
229
        });
230
    }
231
232
    public function makeCapsuleSeeder($capsule)
233
    {
234
        $seeder = "{$capsule['database_namespace']}\\Seeds\DatabaseSeeder";
235
236
        if (class_exists($seeder)) {
237
            return app($seeder);
238
        }
239
    }
240
}
241