PresenterMakeCommand   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 109
rs 10
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getResourceName() 0 4 2
A getDestinationFilePath() 0 9 1
A getArguments() 0 5 1
A getDefaultNamespace() 0 6 1
A getTemplateContents() 0 15 1
A getOptions() 0 4 1
A getFileName() 0 3 1
1
<?php
2
3
namespace Modules\Core\Console;
4
5
use Illuminate\Support\Str;
6
use Nwidart\Modules\Commands\GeneratorCommand;
7
use Nwidart\Modules\Support\Config\GenerateConfigReader;
8
use Nwidart\Modules\Support\Stub;
9
use Nwidart\Modules\Traits\ModuleCommandTrait;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputOption;
12
13
class PresenterMakeCommand extends GeneratorCommand
14
{
15
    use ModuleCommandTrait;
16
17
    /**
18
     * The name of argument name.
19
     *
20
     * @var string
21
     */
22
    protected $argumentName = 'name';
23
24
    /**
25
     * The console command name.
26
     *
27
     * @var string
28
     */
29
    protected $name = 'module:make-presenter';
30
31
    /**
32
     * The console command description.
33
     *
34
     * @var string
35
     */
36
    protected $description = 'Generate new Presenter for the specified module.';
37
38
    public function getDefaultNamespace(): string
39
    {
40
        /** @var \Nwidart\Modules\Laravel\LaravelFileRepository $laravelFileRepository */
41
        $laravelFileRepository = $this->laravel['modules'];
42
43
        return $laravelFileRepository->config('paths.generator.presenters.path', 'Presenters');
44
    }
45
46
    /**
47
     * Get the console command arguments.
48
     *
49
     * @return array
50
     */
51
    protected function getArguments()
52
    {
53
        return [
54
            ['name', InputArgument::REQUIRED, 'The name of the presenter.'],
55
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
56
        ];
57
    }
58
59
    /**
60
     * Get the console command options.
61
     *
62
     * @return array
63
     */
64
    protected function getOptions()
65
    {
66
        return [
67
            ['resource', null, InputOption::VALUE_OPTIONAL, 'The resource that should be assigned.', null],
68
        ];
69
    }
70
71
    /**
72
     * @throws \Nwidart\Modules\Exceptions\ModuleNotFoundException
73
     *
74
     * @return mixed
75
     */
76
    protected function getTemplateContents()
77
    {
78
        /** @var \Nwidart\Modules\Laravel\LaravelFileRepository $laravelFileRepository */
79
        $laravelFileRepository = $this->laravel['modules'];
80
        $module = $laravelFileRepository->findOrFail($this->getModuleName());
81
82
        $root_namespace = $laravelFileRepository->config('namespace');
83
        $root_namespace .= '\\'.$module->getStudlyName();
84
85
        return (new Stub('/presenter.stub', [
86
            'RESOURCE_NAME'  => $this->getResourceName(),
87
            'NAMESPACE'      => $this->getClassNamespace($module),
88
            'CLASS'          => $this->getClass(),
89
            'ROOT_NAMESPACE' => $root_namespace,
90
        ]))->render();
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    private function getResourceName()
97
    {
98
        return (string) $this->option('resource')
99
            ?: Str::before(class_basename((string) $this->argument($this->argumentName)), 'Presenter').'Transformer';
100
    }
101
102
    /**
103
     * @return mixed
104
     */
105
    protected function getDestinationFilePath()
106
    {
107
        /** @var \Nwidart\Modules\Laravel\LaravelFileRepository $laravelFileRepository */
108
        $laravelFileRepository = $this->laravel['modules'];
109
        $path = $laravelFileRepository->getModulePath($this->getModuleName());
110
111
        $presenterPath = GenerateConfigReader::read('presenters');
112
113
        return $path.$presenterPath->getPath().'/'.$this->getFileName().'.php';
114
    }
115
116
    /**
117
     * @return string
118
     */
119
    private function getFileName()
120
    {
121
        return Str::studly((string) $this->argument('name'));
122
    }
123
}
124