ApiMakeCommand::interfaceHandle()   A
last analyzed

Complexity

Conditions 3
Paths 6

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 16
rs 9.9666
1
<?php
2
3
namespace Modules\Core\Console;
4
5
use File;
6
use Illuminate\Contracts\Filesystem\FileNotFoundException;
7
use Illuminate\Support\Str;
8
use Nwidart\Modules\Commands\GeneratorCommand;
9
use Nwidart\Modules\Exceptions\FileAlreadyExistException;
10
use Nwidart\Modules\Generators\FileGenerator;
11
use Nwidart\Modules\Support\Config\GenerateConfigReader;
12
use Nwidart\Modules\Support\Stub;
13
use Nwidart\Modules\Traits\ModuleCommandTrait;
14
use Symfony\Component\Console\Input\InputArgument;
15
16
class ApiMakeCommand extends GeneratorCommand
17
{
18
    use ModuleCommandTrait;
19
20
    /**
21
     * The name of argument name.
22
     *
23
     * @var string
24
     */
25
    protected $argumentName = 'name';
26
27
    /**
28
     * The console command name.
29
     *
30
     * @var string
31
     */
32
    protected $name = 'module:make-api';
33
34
    /**
35
     * The console command description.
36
     *
37
     * @var string
38
     */
39
    protected $description = 'Generate new Api for the specified module.';
40
41
    public function getDefaultNamespace(): string
42
    {
43
        /** @var \Nwidart\Modules\Laravel\LaravelFileRepository $laravelFileRepository */
44
        $laravelFileRepository = $this->laravel['modules'];
45
46
        return $laravelFileRepository->config('paths.generator.api.path', 'Http/Controllers/Api/V1');
47
    }
48
49
    /**
50
     * Get the console command arguments.
51
     *
52
     * @return array
53
     */
54
    protected function getArguments()
55
    {
56
        return [
57
            ['name', InputArgument::REQUIRED, 'The name of the api.'],
58
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
59
        ];
60
    }
61
62
    /**
63
     * @param $path
64
     *
65
     * @throws \Nwidart\Modules\Exceptions\ModuleNotFoundException
66
     */
67
    private function bindingsHandle($path)
68
    {
69
        $contents = $this->getBindingsTemplateContents();
70
71
        try {
72
            $contents = str_replace('//', $contents, File::get($path));
73
            File::replace($path, $contents);
74
75
            $this->info("Update : {$path}");
76
        } catch (FileNotFoundException $e) {
77
            $this->error("File : {$path} not found.");
78
        }
79
    }
80
81
    /**
82
     * Get bindings template contents.
83
     *
84
     * @throws \Nwidart\Modules\Exceptions\ModuleNotFoundException
85
     *
86
     * @return string
87
     */
88
    protected function getBindingsTemplateContents()
89
    {
90
        /** @var \Nwidart\Modules\Laravel\LaravelFileRepository $laravelFileRepository */
91
        $laravelFileRepository = $this->laravel['modules'];
92
        $module = $laravelFileRepository->findOrFail($this->getModuleName());
93
94
        return (new Stub('/bindings.stub', [
95
            'NAMESPACE'       => $this->getClassNamespace($module),
96
            'INTERFACE_CLASS' => $this->getClass(),
97
            'IMPLEMENT_CLASS' => $this->getClass().'I',
98
            'PLACEHOLDER'     => '//',
99
        ]))->render();
100
    }
101
102
    /**
103
     * Get implementation template contents.
104
     *
105
     * @throws \Nwidart\Modules\Exceptions\ModuleNotFoundException
106
     *
107
     * @return string
108
     */
109
    protected function getImplementTemplateContents()
110
    {
111
        /** @var \Nwidart\Modules\Laravel\LaravelFileRepository $laravelFileRepository */
112
        $laravelFileRepository = $this->laravel['modules'];
113
        $module = $laravelFileRepository->findOrFail($this->getModuleName());
114
115
        return (new Stub('/api/implement.stub', [
116
            'NAMESPACE' => $this->getClassNamespace($module),
117
            'CLASS'     => $this->getClass(),
118
        ]))->render();
119
    }
120
121
    /**
122
     * Get interface template contents.
123
     *
124
     * @throws \Nwidart\Modules\Exceptions\ModuleNotFoundException
125
     *
126
     * @return string
127
     */
128
    protected function getInterfaceTemplateContents()
129
    {
130
        /** @var \Nwidart\Modules\Laravel\LaravelFileRepository $laravelFileRepository */
131
        $laravelFileRepository = $this->laravel['modules'];
132
        $module = $laravelFileRepository->findOrFail($this->getModuleName());
133
134
        return (new Stub('/api/interface.stub', [
135
            'NAMESPACE' => $this->getClassNamespace($module),
136
            'CLASS'     => $this->getClass(),
137
        ]))->render();
138
    }
139
140
    /**
141
     * @return mixed
142
     */
143
    protected function getDestinationFilePath()
144
    {
145
        /** @var \Nwidart\Modules\Laravel\LaravelFileRepository $laravelFileRepository */
146
        $laravelFileRepository = $this->laravel['modules'];
147
        $path = $laravelFileRepository->getModulePath($this->getModuleName());
148
        $apiPath = GenerateConfigReader::read('api');
149
150
        return $path.$apiPath->getPath().'/'.$this->getFileName().'.php';
151
    }
152
153
    /**
154
     * @return string
155
     */
156
    private function getFileName()
157
    {
158
        return Str::studly((string) $this->argument('name'));
159
    }
160
161
    /**
162
     * Execute the console command.
163
     *
164
     * @throws \Nwidart\Modules\Exceptions\ModuleNotFoundException
165
     */
166
    public function handle()
167
    {
168
        $path = str_replace('\\', '/', $this->getDestinationFilePath());
169
        $this->interfaceHandle($path);
170
171
        $path = Str::before($path, '.php').'I.php';
172
        $this->implementationHandle($path);
173
174
        $path = module_path($this->getModuleName()).'/Providers/ControllerServiceProvider.php';
175
        $this->bindingsHandle($path);
176
    }
177
178
    /**
179
     * Execute the console interface command.
180
     *
181
     * @param $path
182
     *
183
     * @throws \Nwidart\Modules\Exceptions\ModuleNotFoundException
184
     */
185
    protected function interfaceHandle($path)
186
    {
187
        /** @var \Illuminate\Filesystem\Filesystem $filesystem */
188
        $filesystem = $this->laravel['files'];
189
        if (!$filesystem->isDirectory($dir = dirname($path))) {
190
            $filesystem->makeDirectory($dir, 0777, true);
191
        }
192
193
        $contents = $this->getInterfaceTemplateContents();
194
195
        try {
196
            with(new FileGenerator($path, $contents))->generate();
197
198
            $this->info("Created : {$path}");
199
        } catch (FileAlreadyExistException $e) {
200
            $this->error("File : {$path} already exists.");
201
        }
202
    }
203
204
    /**
205
     * Execute the console implementation command.
206
     *
207
     * @param $path
208
     *
209
     * @throws \Nwidart\Modules\Exceptions\ModuleNotFoundException
210
     */
211
    protected function implementationHandle($path)
212
    {
213
        /** @var \Illuminate\Filesystem\Filesystem $filesystem */
214
        $filesystem = $this->laravel['files'];
215
        if (!$filesystem->isDirectory($dir = dirname($path))) {
216
            $filesystem->makeDirectory($dir, 0777, true);
217
        }
218
219
        $contents = $this->getImplementTemplateContents();
220
221
        try {
222
            with(new FileGenerator($path, $contents))->generate();
223
224
            $this->info("Created : {$path}");
225
        } catch (FileAlreadyExistException $e) {
226
            $this->error("File : {$path} already exists.");
227
        }
228
    }
229
230
    /**
231
     * Get template contents.
232
     *
233
     * @return string
234
     */
235
    protected function getTemplateContents()
236
    {
237
        return '';
238
    }
239
}
240