Completed
Push — master ( 7a4ed5...39fb48 )
by Nicolas
02:33
created

ResourceMakeCommand::collection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Nwidart\Modules\Commands;
4
5
use Illuminate\Support\Str;
6
use Nwidart\Modules\Support\Stub;
7
use Nwidart\Modules\Traits\ModuleCommandTrait;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputOption;
10
11
class ResourceMakeCommand extends GeneratorCommand
12
{
13
    use ModuleCommandTrait;
14
15
    protected $argumentName = 'name';
16
    protected $name = 'module:make-resource';
17
    protected $description = 'Create a new resource class for the specified module.';
18
19
    /**
20
     * Get default namespace.
21
     *
22
     * @return string
23
     */
24 3
    public function getDefaultNamespace()
25
    {
26 3
        return 'Transformers';
27
    }
28
29
    /**
30
     * Get the console command arguments.
31
     *
32
     * @return array
33
     */
34 65
    protected function getArguments()
35
    {
36
        return [
37 65
            ['name', InputArgument::REQUIRED, 'The name of the resource class.'],
38
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
39
        ];
40
    }
41
42 65
    protected function getOptions()
43
    {
44
        return [
45 65
            ['collection', 'c', InputOption::VALUE_NONE, 'Create a resource collection.'],
46
        ];
47
    }
48
49
    /**
50
     * @return mixed
51
     */
52 3
    protected function getTemplateContents()
53
    {
54 3
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
55
56 3
        return (new Stub($this->getStubName(), [
57 3
            'NAMESPACE' => $this->getClassNamespace($module),
58 3
            'CLASS'     => $this->getClass(),
59 3
        ]))->render();
60
    }
61
62
    /**
63
     * @return mixed
64
     */
65 3
    protected function getDestinationFilePath()
66
    {
67 3
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
68
69 3
        $seederPath = $this->laravel['modules']->config('paths.generator.resource');
70
71 3
        return $path . $seederPath . '/' . $this->getFileName() . '.php';
72
    }
73
74
    /**
75
     * @return string
76
     */
77 3
    private function getFileName()
78
    {
79 3
        return Str::studly($this->argument('name'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') targeting Illuminate\Console\Command::argument() can also be of type array; however, Illuminate\Support\Str::studly() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
80
    }
81
82
    /**
83
     * Determine if the command is generating a resource collection.
84
     *
85
     * @return bool
86
     */
87 3
    protected function collection() : bool
88
    {
89 3
        return $this->option('collection') ||
90 3
            Str::endsWith($this->argument('name'), 'Collection');
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') targeting Illuminate\Console\Command::argument() can also be of type array; however, Illuminate\Support\Str::endsWith() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
91
    }
92
93
    /**
94
     * @return string
95
     */
96 3
    protected function getStubName(): string
97
    {
98 3
        if ($this->collection()) {
99 1
            return '/resource-collection.stub';
100
        }
101
102 2
        return '/resource.stub';
103
    }
104
}
105