Completed
Push — master ( 4fafa1...e65f27 )
by Nicolas
12:12
created

src/Commands/ResourceMakeCommand.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 5
    public function getDefaultNamespace() : string
21
    {
22 5
        $module = $this->laravel['modules'];
23
24 5
        return $module->config('paths.generator.resource.namespace') ?: $module->config('paths.generator.resource.path', 'Transformers');
25
    }
26
27
    /**
28
     * Get the console command arguments.
29
     *
30
     * @return array
31
     */
32 120
    protected function getArguments()
33
    {
34
        return [
35 120
            ['name', InputArgument::REQUIRED, 'The name of the resource class.'],
36
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
37
        ];
38
    }
39
40 120
    protected function getOptions()
41
    {
42
        return [
43 120
            ['collection', 'c', InputOption::VALUE_NONE, 'Create a resource collection.'],
44
        ];
45
    }
46
47
    /**
48
     * @return mixed
49
     */
50 5
    protected function getTemplateContents()
51
    {
52 5
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
53
54 5
        return (new Stub($this->getStubName(), [
55 5
            'NAMESPACE' => $this->getClassNamespace($module),
56 5
            'CLASS'     => $this->getClass(),
57 5
        ]))->render();
58
    }
59
60
    /**
61
     * @return mixed
62
     */
63 5
    protected function getDestinationFilePath()
64
    {
65 5
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
66
67 5
        $resourcePath = GenerateConfigReader::read('resource');
68
69 5
        return $path . $resourcePath->getPath() . '/' . $this->getFileName() . '.php';
70
    }
71
72
    /**
73
     * @return string
74
     */
75 5
    private function getFileName()
76
    {
77 5
        return Str::studly($this->argument('name'));
0 ignored issues
show
It seems like $this->argument('name') targeting Illuminate\Console\Command::argument() can also be of type array or null; 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...
78
    }
79
80
    /**
81
     * Determine if the command is generating a resource collection.
82
     *
83
     * @return bool
84
     */
85 5
    protected function collection() : bool
86
    {
87 5
        return $this->option('collection') ||
88 5
            Str::endsWith($this->argument('name'), 'Collection');
0 ignored issues
show
It seems like $this->argument('name') targeting Illuminate\Console\Command::argument() can also be of type array or null; 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...
89
    }
90
91
    /**
92
     * @return string
93
     */
94 5
    protected function getStubName(): string
95
    {
96 5
        if ($this->collection()) {
97 3
            return '/resource-collection.stub';
98
        }
99
100 2
        return '/resource.stub';
101
    }
102
}
103