Issues (197)

src/Commands/ComponentClassMakeCommand.php (2 issues)

Labels
Severity
1
<?php
2
3
namespace Salah3id\Domains\Commands;
4
5
use Illuminate\Support\Str;
6
use Salah3id\Domains\Support\Config\GenerateConfigReader;
7
use Salah3id\Domains\Support\Stub;
8
use Salah3id\Domains\Traits\DomainCommandTrait;
9
use Symfony\Component\Console\Input\InputArgument;
10
11
class ComponentClassMakeCommand extends GeneratorCommand
12
{
13
    use DomainCommandTrait;
14
15
    /**
16
     * The name of argument name.
17
     *
18
     * @var string
19
     */
20
    protected $argumentName = 'name';
21
22
    /**
23
     * The console command name.
24
     *
25
     * @var string
26
     */
27
    protected $name = 'domain:make-component';
28
29
    /**
30
     * The console command description.
31
     *
32
     * @var string
33
     */
34
    protected $description = 'Create a new component-class for the specified domain.';
35
36
    public function handle(): int
37
    {
38
        if (parent::handle() === E_ERROR) {
39
            return E_ERROR;
40
        }
41
        $this->writeComponentViewTemplate();
42
43
        return 0;
44
    }
45
    /**
46
     * Write the view template for the component.
47
     *
48
     * @return void
49
     */
50
    protected function writeComponentViewTemplate()
51
    {
52
        $this->call('domain:make-component-view', ['name' => $this->argument('name') , 'domain' => $this->argument('domain')]);
53
    }
54
55
    public function getDefaultNamespace(): string
56
    {
57
        $domain = $this->laravel['domains'];
58
59
        return $domain->config('paths.generator.component-class.namespace') ?: $domain->config('paths.generator.component-class.path', 'View/Component');
60
    }
61
62
    /**
63
     * Get the console command arguments.
64
     *
65
     * @return array
66
     */
67
    protected function getArguments()
68
    {
69
        return [
70
            ['name', InputArgument::REQUIRED, 'The name of the component.'],
71
            ['domain', InputArgument::OPTIONAL, 'The name of domain will be used.'],
72
        ];
73
    }
74
    /**
75
     * @return mixed
76
     */
77
    protected function getTemplateContents()
78
    {
79
        $domain = $this->laravel['domains']->findOrFail($this->getDomainName());
80
81
        return (new Stub('/component-class.stub', [
82
            'NAMESPACE'         => $this->getClassNamespace($domain),
83
            'CLASS'             => $this->getClass(),
84
            'LOWER_NAME'        => $domain->getLowerName(),
85
            'COMPONENT_NAME'    => 'components.' . Str::lower($this->argument('name')),
0 ignored issues
show
It seems like $this->argument('name') can also be of type array; however, parameter $value of Illuminate\Support\Str::lower() 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

85
            'COMPONENT_NAME'    => 'components.' . Str::lower(/** @scrutinizer ignore-type */ $this->argument('name')),
Loading history...
86
        ]))->render();
87
    }
88
89
    /**
90
     * @return mixed
91
     */
92
    protected function getDestinationFilePath()
93
    {
94
        $path = $this->laravel['domains']->getDomainPath($this->getDomainName());
95
        $factoryPath = GenerateConfigReader::read('component-class');
96
97
        return $path . $factoryPath->getPath() . '/' . $this->getFileName();
98
    }
99
100
    /**
101
     * @return string
102
     */
103
    private function getFileName()
104
    {
105
        return Str::studly($this->argument('name')) . '.php';
0 ignored issues
show
It seems like $this->argument('name') can also be of type array; however, parameter $value of Illuminate\Support\Str::studly() 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

105
        return Str::studly(/** @scrutinizer ignore-type */ $this->argument('name')) . '.php';
Loading history...
106
    }
107
}
108