ResourceMakeCommand   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A collection() 0 4 2
A getDefaultNamespace() 0 6 2
A getStubName() 0 7 2
A getTemplateContents() 0 9 1
A getDestinationFilePath() 0 7 1
1
<?php
2
3
namespace Rawilk\LaravelModules\Commands\Generators;
4
5
use Illuminate\Support\Str;
6
use Rawilk\LaravelModules\Support\Config\GenerateConfigReader;
7
use Rawilk\LaravelModules\Support\Stub;
8
use Rawilk\LaravelModules\Traits\ModuleCommands;
9
10
class ResourceMakeCommand extends GeneratorCommand
11
{
12
    use ModuleCommands;
13
14
    /** @var string */
15
    protected $argumentName = 'name';
16
17
    /** @var string */
18
    protected $signature = 'module:make-resource
19
                            {name : The name of the resource class}
20
                            {module? : The name of the module to create the resource for}
21
                            {--c|collection : Indicates a resource collection should be created}';
22
23
    /** @var string */
24
    protected $description = 'Create a new resource class for the specified module.';
25
26
    protected function getDefaultNamespace(): string
27
    {
28
        /** @var \Rawilk\LaravelModules\Contracts\Repository $module */
29
        $module = $this->laravel['modules'];
30
31
        return $module->config('paths.generator.resource.namespace') ?: $module->config('paths.generator.resource.path', 'Transformers');
32
    }
33
34
    protected function getDestinationFilePath(): string
35
    {
36
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
37
38
        $resourcePath = GenerateConfigReader::read('resource');
39
40
        return $path . $resourcePath->getPath() . '/' . $this->getFileName() . '.php';
41
    }
42
43
    protected function getTemplateContents(): string
44
    {
45
        /** @var \Rawilk\LaravelModules\Module $module */
46
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
47
48
        return (new Stub($this->getStubName(), [
49
            'NAMESPACE' => $this->getClassNamespace($module),
50
            'CLASS'     => $this->getClass(),
51
        ]))->render();
52
    }
53
54
    private function collection(): bool
55
    {
56
        return $this->option('collection')
57
            || Str::endsWith($this->argument('name'), 'Collection');
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') can also be of type string[]; however, parameter $haystack of Illuminate\Support\Str::endsWith() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
            || Str::endsWith(/** @scrutinizer ignore-type */ $this->argument('name'), 'Collection');
Loading history...
58
    }
59
60
    private function getStubName(): string
61
    {
62
        if ($this->collection()) {
63
            return '/resource-collection.stub';
64
        }
65
66
        return '/resource.stub';
67
    }
68
}
69