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

HasCapsules::namespaceToPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace A17\Twill\Services\Capsules;
4
5
use Illuminate\Support\Str;
6
7
trait HasCapsules
8
{
9 8
    protected function getAutoloader()
10
    {
11 8
        return app()->bound('autoloader')
12 8
            ? app('autoloader')
13 8
            : require base_path('vendor/autoload.php');
14
    }
15
16 76
    public function getCapsuleList()
17
    {
18 76
        $path = config('twill.capsules.path');
19
20 76
        $list = collect(config('twill.capsules.list'));
21
22 76
        if (config('twill.capsules.loaded')) {
23 76
            return $list;
24
        }
25
26
        return $list
27 76
            ->where('enabled', true)
28 76
            ->map(function ($capsule) use ($path) {
29 7
                return $this->makeCapsule($capsule, $path);
30 76
            });
31
    }
32
33 4
    public function getCapsuleByModel($model)
34
    {
35 4
        return $this->getCapsuleList()
36 4
            ->where('singular', $model)
37 4
            ->first();
38
    }
39
40 56
    public function getCapsuleByModule($module)
41
    {
42 56
        return $this->getCapsuleList()
43 56
            ->filter(function ($capsule) use ($module) {
44 7
                return Str::lower($capsule['plural']) == Str::lower($module);
45 56
            })
46 56
            ->first();
47
    }
48
49 8
    public function makeCapsule($capsule, $basePath)
50
    {
51 8
        $capsule['name'] = Str::studly($capsule['name']);
52
53 8
        $capsule['module'] = Str::camel($capsule['name']);
54
55 8
        $capsule['plural'] = $name = $capsule['name'];
56
57 8
        $capsule['singular'] = $singular =
58 8
            $capsule['singular'] ?? Str::singular($name);
59
60 8
        $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 8
            'namespace'
64 8
        ] = $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 8
            $capsule['name']
66
        );
67
68
        $capsule[
69 8
            'database_namespace'
70 8
        ] = "$capsuleNamespace\Database";
71
72
        $capsule[
73 8
            'seeds_namespace'
74 8
        ] = "{$capsule['database_namespace']}\Seeds";
75
76 8
        $capsule['model'] = $capsule['models'] = $models =
77 8
            "{$capsuleNamespace}\\" .
78 8
            config('twill.capsules.namespaces.models');
79 8
        $capsule['repositories'] = $repositories =
80 8
            "{$capsuleNamespace}\\" .
81 8
            config('twill.capsules.namespaces.repositories');
82 8
        $capsule['controllers'] = $controllers =
83 8
            "{$capsuleNamespace}\\" .
84 8
            config('twill.capsules.namespaces.controllers');
85 8
        $capsule['requests'] = $requests =
86 8
            "{$capsuleNamespace}\\" .
87 8
            config('twill.capsules.namespaces.requests');
88
89 8
        $capsule['psr4_path'] = "$basePath/{$name}/app";
90
91 8
        $capsule['database_psr4_path'] = "$basePath/{$name}/database";
92
93 8
        $capsule['seeds_psr4_path'] = "{$capsule['database_psr4_path']}/seeds";
94
95 8
        $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 8
            'migrations_dir'
99 8
        ] = "{$capsule['root_path']}/database/migrations";
100
101 8
        $capsule['views_dir'] = "{$capsule['root_path']}/resources/views";
102
103 8
        $capsule['view_prefix'] = "{$name}.resources.views.admin";
104
105 8
        $capsule['routes_file'] = "{$capsule['root_path']}/routes/admin.php";
106
107 8
        $capsule['model'] = "{$models}\\{$singular}";
108
109 8
        $capsule['models_dir'] = $this->namespaceToPath($capsule, $models);
110
111 8
        $capsule['translation'] = "{$models}\\{$singular}Translation";
112
113 8
        $capsule['slug'] = "{$models}\\{$singular}Slug";
114
115 8
        $capsule['revision'] = "{$models}\\{$singular}Revision";
116
117 8
        $capsule['repository'] = "{$repositories}\\{$singular}Repository";
118
119 8
        $capsule['repositories_dir'] = $this->namespaceToPath(
120 8
            $capsule,
121
            $repositories
122
        );
123
124 8
        $capsule['controller'] = "{$controllers}\\{$singular}Controller";
125
126 8
        $capsule['controllers_dir'] = $this->namespaceToPath(
127 8
            $capsule,
128
            $controllers
129
        );
130
131 8
        $capsule['formRequest'] = "{$requests}\\{$singular}Request";
132
133 8
        $capsule['requests_dir'] = $this->namespaceToPath($capsule, $requests);
134
135 8
        $this->registerPsr4Autoloader($capsule);
136
137 8
        return $capsule;
138
    }
139
140 8
    public function registerPsr4Autoloader($capsule)
141
    {
142 8
        $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 8
            $capsule['namespace'] . '\\',
144 8
            $capsule['psr4_path']
145
        );
146
147 8
        $this->getAutoloader()->setPsr4(
148 8
            $capsule['database_namespace'] . '\\',
149 8
            $capsule['database_psr4_path']
150
        );
151
152 8
        $this->getAutoloader()->setPsr4(
153 8
            $capsule['database_namespace'] . '\\Seeds\\',
154 8
            $capsule['database_psr4_path'] . '/seeds'
155
        );
156 8
    }
157
158 8
    public function capsuleRootPath($capsule)
159
    {
160 8
        return config('twill.capsules.path') . '/' . $capsule['name'] ?? null;
161
    }
162
163 4
    public function getCapsuleRepositoryClass($model)
164
    {
165 4
        return $this->getCapsuleByModel($model)['repository'] ?? null;
166
    }
167
168 4
    public function getCapsuleTranslationClass($model)
169
    {
170 4
        return $this->getCapsuleByModel($model)['translation'] ?? null;
171
    }
172
173 3
    public function getCapsuleSlugClass($model)
174
    {
175 3
        return $this->getCapsuleByModel($model)['slug'] ?? null;
176
    }
177
178 4
    public function getCapsuleRevisionClass($model)
179
    {
180 4
        return $this->getCapsuleByModel($model)['revision'] ?? null;
181
    }
182
183 3
    public function getCapsuleFormRequestClass($model)
184
    {
185 3
        return $this->getCapsuleByModel($model)['formRequest'] ?? null;
186
    }
187
188 56
    public function getCapsuleViewPrefix($capsule)
189
    {
190 56
        return $this->getCapsuleByModule(Str::studly($capsule))[
191 56
            'view_prefix'
192 56
        ] ?? null;
193
    }
194
195 8
    public function namespaceToPath($capsule, $namespace)
196
    {
197 8
        return $this->capsuleNamespaceToPath(
198 8
            $namespace,
199 8
            $capsule['namespace'],
200 8
            $capsule['root_path']
201
        );
202
    }
203
204 8
    public function capsuleNamespaceToPath(
205
        $namespace,
206
        $capsuleNamespace,
207
        $rootPath
208
    ) {
209 8
        $namespace = Str::after($namespace, $capsuleNamespace . '\\');
210
211 8
        $subdir = config('twill.capsules.namespaces.subdir');
212
213 8
        $subdir = filled($subdir) ? "{$subdir}/" : '';
214
215 8
        return "{$rootPath}/{$subdir}" . str_replace('\\', '/', $namespace);
216
    }
217
218 8
    public function getManager()
219
    {
220 8
        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($illuminateSeeder)
224
    {
225
        $twillSeeder = app(CapsuleSeeder::class);
226
227
        $this->getCapsuleList()->each(function ($capsule) use ($twillSeeder, $illuminateSeeder) {
228
            if (filled($capsuleSeeder = $this->makeCapsuleSeeder($capsule))) {
229
                $twillSeeder->setCommand($illuminateSeeder->command);
230
231
                $twillSeeder->call($capsuleSeeder);
232
            }
233
        });
234
    }
235
236
    public function makeCapsuleSeeder($capsule)
237
    {
238
        $seeder = "{$capsule['database_namespace']}\\Seeds\DatabaseSeeder";
239
240
        if (class_exists($seeder)) {
241
            return $seeder;
242
        }
243
244
        return null;
245
    }
246
}
247