Completed
Pull Request — master (#666)
by reallyli
02:28
created

ResourceMakeCommand   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 89
ccs 0
cts 47
cp 0
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 5

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultNamespace() 0 4 1
A getArguments() 0 7 1
A getOptions() 0 6 1
A getTemplateContents() 0 9 1
A getDestinationFilePath() 0 8 1
A getFileName() 0 4 1
A collection() 0 5 2
A getStubName() 0 8 2
1
<?php
2
3
namespace Nwidart\Modules\Commands;
4
5
use Illuminate\Support\Str;
6
use Nwidart\Modules\Support\Config\GenerateConfigReader;
7
use Nwidart\Modules\Support\Stub;
8
use Nwidart\Modules\Traits\ModuleCommandTrait;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputOption;
11
12
class ResourceMakeCommand extends GeneratorCommand
13
{
14
    use ModuleCommandTrait;
15
16
    protected $argumentName = 'name';
17
    protected $name = 'module:make-resource';
18
    protected $description = 'Create a new resource class for the specified module.';
19
20
    public function getDefaultNamespace() : string
21
    {
22
        return $this->laravel['modules']->config('paths.generator.resource.path', 'Transformers');
23
    }
24
25
    /**
26
     * Get the console command arguments.
27
     *
28
     * @return array
29
     */
30
    protected function getArguments()
31
    {
32
        return [
33
            ['name', InputArgument::REQUIRED, 'The name of the resource class.'],
34
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
35
        ];
36
    }
37
38
    protected function getOptions()
39
    {
40
        return [
41
            ['collection', 'c', InputOption::VALUE_NONE, 'Create a resource collection.'],
42
        ];
43
    }
44
45
    /**
46
     * @return mixed
47
     */
48
    protected function getTemplateContents()
49
    {
50
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
51
52
        return (new Stub($this->getStubName(), [
53
            'NAMESPACE' => $this->getClassNamespace($module),
54
            'CLASS'     => $this->getClass(),
55
        ]))->render();
56
    }
57
58
    /**
59
     * @return mixed
60
     */
61
    protected function getDestinationFilePath()
62
    {
63
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
64
65
        $resourcePath = GenerateConfigReader::read('resource');
66
67
        return $path . $resourcePath->getPath() . '/' . $this->getFileName() . '.php';
68
    }
69
70
    /**
71
     * @return string
72
     */
73
    private function getFileName()
74
    {
75
        return Str::studly($this->argument('name'));
76
    }
77
78
    /**
79
     * Determine if the command is generating a resource collection.
80
     *
81
     * @return bool
82
     */
83
    protected function collection() : bool
84
    {
85
        return $this->option('collection') ||
86
            Str::endsWith($this->argument('name'), 'Collection');
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    protected function getStubName(): string
93
    {
94
        if ($this->collection()) {
95
            return '/resource-collection.stub';
96
        }
97
98
        return '/resource.stub';
99
    }
100
}
101