Passed
Push — main ( 2890e8...0489e5 )
by ikechukwu
14:13
created

InitCommands::handleDatabasePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Ikechukwukalu\Magicmake\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Filesystem\Filesystem;
7
use Illuminate\Support\Str;
8
use Symfony\Component\Finder\SplFileInfo;
9
10
class InitCommands extends Command
11
{
12
13
    protected function handleAppActionPath()
14
    {
15
        $this->handlePath('Actions',
16
            __DIR__.'/stubs/init/app/Actions',
17
            'Action');
18
    }
19
20
    protected function handleAppContractsPath()
21
    {
22
        if (! is_dir($directory = app_path('Contracts'))) {
23
            mkdir($directory, 0755, true);
24
        }
25
26
        $filesystem = new Filesystem;
27
28
        collect($filesystem->allFiles(__DIR__.'/stubs/init/app/Contracts'))
0 ignored issues
show
Bug introduced by
$filesystem->allFiles(__...bs/init/app/Contracts') of type Symfony\Component\Finder\SplFileInfo[] is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

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

28
        collect(/** @scrutinizer ignore-type */ $filesystem->allFiles(__DIR__.'/stubs/init/app/Contracts'))
Loading history...
29
            ->each(function (SplFileInfo $file) use ($filesystem) {
30
                $filesystem->copy(
31
                    $file->getPathname(),
32
                    app_path('Contracts/'.Str::replaceLast('.stub', '.php', $file->getFilename()))
33
                );
34
            });
35
36
        $this->components->info('Contract classes scaffolding generated successfully.');
37
    }
38
39
    protected function handleAppEnumsPath()
40
    {
41
        $this->handlePath('Enums',
42
            __DIR__.'/stubs/init/app/Enums',
43
            'Enum');
44
    }
45
46
    protected function handleAppEventsPath()
47
    {
48
        $this->handlePath('Events',
49
            __DIR__.'/stubs/init/app/Events',
50
            'Event');
51
    }
52
53
    protected function handleAppExceptionsPath()
54
    {
55
        $this->handlePath('Exceptions',
56
            __DIR__.'/stubs/init/app/Exceptions',
57
            'Exception');
58
    }
59
60
    protected function handleAppHttpPath()
61
    {
62
        $this->handlePath('Http',
63
            __DIR__.'/stubs/init/app/Http',
64
            'Http');
65
    }
66
67
    protected function handleAppHttpControllersPath()
68
    {
69
        $this->handlePath('Http/Controllers',
70
            __DIR__.'/stubs/init/app/Controllers',
71
            'Http/Controllers');
72
    }
73
74
    protected function handleAppHttpControllersAuthPath()
75
    {
76
        $this->handlePath('Http/Controllers/Auth',
77
            __DIR__.'/stubs/init/app/AuthControllers',
78
            'Http/Controllers/Auth');
79
    }
80
81
    protected function handleAppHttpMiddlewarePath()
82
    {
83
        $this->handlePath('Http/Middleware',
84
            __DIR__.'/stubs/init/app/Middleware',
85
            'Http/Middleware');
86
    }
87
88
    protected function handleAppHttpRequestsPath()
89
    {
90
        $this->handlePath('Http/Requests',
91
            __DIR__.'/stubs/init/app/Requests',
92
            'Http/Requests');
93
    }
94
95
    protected function handleAppHttpRequestsAuthPath()
96
    {
97
        $this->handlePath('Http/Requests/Auth',
98
            __DIR__.'/stubs/init/app/AuthRequests',
99
            'Http/Requests/Auth');
100
    }
101
102
    protected function handleAppListenersPath()
103
    {
104
        $this->handlePath('Listeners',
105
            __DIR__.'/stubs/init/app/Listeners',
106
            'Listener');
107
    }
108
109
    protected function handleAppModelsPath()
110
    {
111
        $this->handlePath('Models',
112
            __DIR__.'/stubs/init/app/Models',
113
            'Model');
114
    }
115
116
    protected function handleAppModelsScopesPath()
117
    {
118
        $this->handlePath('Models/Scopes',
119
            __DIR__.'/stubs/init/app/ModelsScopes',
120
            'Models/Scopes');
121
    }
122
123
    protected function handleAppNotificationsPath()
124
    {
125
        $this->handlePath('Notifications',
126
            __DIR__.'/stubs/init/app/Notifications',
127
            'Notification');
128
    }
129
130
    protected function handleAppProvidersPath()
131
    {
132
        $this->handlePath('Providers',
133
            __DIR__.'/stubs/init/app/Providers',
134
            'Provider');
135
    }
136
137
    protected function handleAppRepositoriesPath()
138
    {
139
        $this->handlePath('Repositories',
140
            __DIR__.'/stubs/init/app/Repositories',
141
            'Repository');
142
    }
143
144
    protected function handleAppRulesPath()
145
    {
146
        $this->handlePath('Rules',
147
            __DIR__.'/stubs/init/app/Rules',
148
            'Rule');
149
    }
150
151
    protected function handleAppServicesPath()
152
    {
153
        $this->handlePath('Services',
154
            __DIR__.'/stubs/init/app/Services',
155
            'Service');
156
    }
157
158
    protected function handleAppServicesAuthPath()
159
    {
160
        $this->handlePath('Services/Auth',
161
            __DIR__.'/stubs/init/app/AuthServices',
162
            'Services/Auth');
163
    }
164
165
    protected function handleAppTraitsPath()
166
    {
167
        $this->handlePath('Traits',
168
            __DIR__.'/stubs/init/app/Traits',
169
            'Trait');
170
    }
171
172
    protected function handleConfigPath()
173
    {
174
        $this->handlePath('config',
175
            __DIR__.'/stubs/init/config',
176
            'config');
177
    }
178
179
    protected function handleDatabasePath()
180
    {
181
        $this->handlePath('database/factories',
182
            __DIR__.'/stubs/init/database/factories',
183
            'database/factories');
184
185
        $this->handlePath('database/migrations',
186
            __DIR__.'/stubs/init/database/migrations',
187
            'database/migrations');
188
    }
189
190
    protected function handleLangPath()
191
    {
192
        $this->handlePath('lang/en',
193
            __DIR__.'/stubs/init/lang/en',
194
            'lang/en');
195
    }
196
197
    protected function handleRoutesPath()
198
    {
199
        $this->handlePath('routes/app',
200
            __DIR__.'/stubs/init/routes/app',
201
            'routes/app');
202
203
        file_put_contents(
204
            base_path('routes/web.php'),
205
            file_get_contents(__DIR__.'/stubs/init/routes/web.stub'),
206
            FILE_APPEND
207
        );
208
209
        file_put_contents(
210
            base_path('routes/api.php'),
211
            file_get_contents(__DIR__.'/stubs/init/routes/api.stub'),
212
            FILE_APPEND
213
        );
214
215
        $this->components->info('routes scaffolding generated successfully.');
216
    }
217
218
    protected function handleTestsPath()
219
    {
220
        $this->handlePath('tests/Feature',
221
            __DIR__.'/stubs/init/tests/Feature',
222
            'tests/Feature');
223
    }
224
225
    protected function handleResourcesPath()
226
    {
227
        $this->handlePath('resources/views/layouts',
228
            __DIR__.'/stubs/init/resources/views/layouts',
229
            'resources/views/layouts');
230
231
        $this->handlePath('resources/views/passwords',
232
            __DIR__.'/stubs/init/resources/views/passwords',
233
            'resources/views/passwords');
234
235
        $this->handlePath('resources/views/socialite',
236
            __DIR__.'/stubs/init/resources/views/socialite',
237
            'resources/views/socialite');
238
239
        $this->handlePath('resources/views/twofactor',
240
            __DIR__.'/stubs/init/resources/views/twofactor',
241
            'resources/views/twofactor');
242
    }
243
244
    private function handlePath(string $appPath, string $stubPath, string $name)
245
    {
246
        if (! is_dir($directory = app_path($appPath))) {
247
            mkdir($directory, 0755, true);
248
        }
249
250
        $filesystem = new Filesystem;
251
252
        collect($filesystem->allFiles($stubPath))
0 ignored issues
show
Bug introduced by
$filesystem->allFiles($stubPath) of type Symfony\Component\Finder\SplFileInfo[] is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

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

252
        collect(/** @scrutinizer ignore-type */ $filesystem->allFiles($stubPath))
Loading history...
253
            ->each(function (SplFileInfo $file) use ($filesystem, $appPath) {
254
                $filesystem->copy(
255
                    $file->getPathname(),
256
                    app_path("{$appPath}/".Str::replaceLast('.stub', '.php', $file->getFilename()))
257
                );
258
            });
259
260
        $this->components->info("{$name} classes scaffolding generated successfully.");
261
    }
262
}
263