ResourceMakeCommand::handle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Modules\Core\Console;
4
5
use Illuminate\Support\Str;
6
use Nwidart\Modules\Commands\ResourceMakeCommand as BaseResourceMakeCommand;
7
use Nwidart\Modules\Support\Stub;
8
use Symfony\Component\Console\Input\InputOption;
9
10
class ResourceMakeCommand extends BaseResourceMakeCommand
11
{
12
    /**
13
     * Execute the console command.
14
     */
15
    public function handle()
16
    {
17
        parent::handle();
18
19
        $this->handleOptionalPresenterOption();
20
    }
21
22
    private function handleOptionalPresenterOption()
23
    {
24
        if ($this->option('presenter') === true) {
25
            $presenterName = $this->getModelName().'Presenter';
26
27
            $this->call('module:make-presenter', [
28
                'name' => $presenterName, 'module' => $this->argument('module'),
29
            ]);
30
        }
31
    }
32
33
    /**
34
     * Get the console command options.
35
     *
36
     * @return array
37
     */
38
    protected function getOptions()
39
    {
40
        return [
41
            ['model', null, InputOption::VALUE_OPTIONAL, 'The model that should be assigned.', null],
42
            ['presenter', 'p', InputOption::VALUE_NONE, 'Flag to create associated presenter', null],
43
        ];
44
    }
45
46
    /**
47
     * @throws \Nwidart\Modules\Exceptions\ModuleNotFoundException
48
     *
49
     * @return mixed
50
     */
51
    protected function getTemplateContents()
52
    {
53
        /** @var \Nwidart\Modules\Laravel\LaravelFileRepository $laravelFileRepository */
54
        $laravelFileRepository = $this->laravel['modules'];
55
        $module = $laravelFileRepository->findOrFail($this->getModuleName());
56
57
        $root_namespace = $laravelFileRepository->config('namespace');
58
        $root_namespace .= '\\'.$module->getStudlyName();
59
60
        return (new Stub('/resource.stub', [
61
            'MODEL'          => $this->getModelName(),
62
            'NAMESPACE'      => $this->getClassNamespace($module),
63
            'CLASS'          => $this->getClass(),
64
            'ROOT_NAMESPACE' => $root_namespace,
65
        ]))->render();
66
    }
67
68
    /**
69
     * @return string
70
     */
71
    private function getModelName()
72
    {
73
        return (string) $this->option('model')
74
            ?: Str::before(class_basename((string) $this->argument($this->argumentName)), 'Transformer');
75
    }
76
}
77