Completed
Pull Request — master (#1088)
by
unknown
02:55
created

ProviderMakeCommand::getFileName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Nwidart\Modules\Commands;
4
5
use Nwidart\Modules\Json;
6
use Illuminate\Support\Str;
7
use Nwidart\Modules\Module;
8
use Nwidart\Modules\Support\Stub;
9
use Nwidart\Modules\Traits\ModuleCommandTrait;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Nwidart\Modules\Support\Config\GenerateConfigReader;
13
14
class ProviderMakeCommand extends GeneratorCommand
15
{
16
    use ModuleCommandTrait;
17
18
    /**
19
     * The name of argument name.
20
     *
21
     * @var string
22
     */
23
    protected $argumentName = 'name';
24
25
    /**
26
     * The console command name.
27
     *
28
     * @var string
29
     */
30
    protected $name = 'module:make-provider';
31
32
    /**
33
     * Execute the console command.
34
     */
35
    public function handle(): int
36
    {
37
        if (parent::handle() != E_ERROR) {
38
39
            /** Provider cannot be type of abstract or once registered will break the application */
40
            if (!$this->option('master')) {
41
42
                /** Confirmation */
43
                if ($this->confirm('Do you want to register this provider?', false)) {
44
45
                    $json = new Json(module_path($this->getModule()->getStudlyName(), 'module.json'));
0 ignored issues
show
Bug introduced by
The method getModule() does not exist on Nwidart\Modules\Commands\ProviderMakeCommand. Did you maybe mean getModuleName()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
46
                    $providers = $json->get('providers', []);
47
48
                    $provider = $this->getClassNamespace($this->getModule()) . "\\" . $this->getFileName();
0 ignored issues
show
Bug introduced by
The method getModule() does not exist on Nwidart\Modules\Commands\ProviderMakeCommand. Did you maybe mean getModuleName()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
49
50
                    $json->set('providers', array_unique(array_merge($providers, [
51
                        $provider
52
                    ])))->save();
53
54
                    $this->info("Provider [{$this->getFileName()}] for module [{$this->getModule()->getStudlyName()}] has been registered");
0 ignored issues
show
Bug introduced by
The method getModule() does not exist on Nwidart\Modules\Commands\ProviderMakeCommand. Did you maybe mean getModuleName()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
55
                }
56
            }
57
        };
58
59
        return 0;
60
    }
61
62
63
    /**
64
     * The console command description.
65
     *
66
     * @var string
67
     */
68
    protected $description = 'Create a new service provider class for the specified module.';
69
70
    public function getDefaultNamespace(): string
71
    {
72
        $module = $this->laravel['modules'];
73
74
        return $module->config('paths.generator.provider.namespace') ?: $module->config('paths.generator.provider.path', 'Providers');
75
    }
76
77
    /**
78
     * Get the console command arguments.
79
     *
80
     * @return array
81
     */
82 4
    protected function getArguments()
83
    {
84
        return [
85 4
            ['name', InputArgument::REQUIRED, 'The service provider name.'],
86
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
87
        ];
88
    }
89
90
    /**
91
     * Get the console command options.
92
     *
93
     * @return array
94
     */
95 4
    protected function getOptions()
96
    {
97
        return [
98 4
            ['master', null, InputOption::VALUE_NONE, 'Indicates the master service provider', null],
99
        ];
100
    }
101
102
    /**
103
     * @return mixed
104
     */
105
    protected function getTemplateContents()
106
    {
107
        $stub = $this->option('master') ? 'scaffold/provider' : 'provider';
108
109
        /** @var Module $module */
110
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
111
112
        return (new Stub('/' . $stub . '.stub', [
113
            'NAMESPACE'         => $this->getClassNamespace($module),
114
            'CLASS'             => $this->getClass(),
115
            'LOWER_NAME'        => $module->getLowerName(),
116
            'MODULE'            => $this->getModuleName(),
117
            'NAME'              => $this->getFileName(),
118
            'STUDLY_NAME'       => $module->getStudlyName(),
119
            'MODULE_NAMESPACE'  => $this->laravel['modules']->config('namespace'),
120
            'PATH_VIEWS'        => GenerateConfigReader::read('views')->getPath(),
121
            'PATH_LANG'         => GenerateConfigReader::read('lang')->getPath(),
122
            'PATH_CONFIG'       => GenerateConfigReader::read('config')->getPath(),
123
            'MIGRATIONS_PATH'   => GenerateConfigReader::read('migration')->getPath(),
124
            'FACTORIES_PATH'    => GenerateConfigReader::read('factory')->getPath(),
125
        ]))->render();
126
    }
127
128
    /**
129
     * @return mixed
130
     */
131
    protected function getDestinationFilePath()
132
    {
133
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
134
135
        $generatorPath = GenerateConfigReader::read('provider');
136
137
        return $path . $generatorPath->getPath() . '/' . $this->getFileName() . '.php';
138
    }
139
140
    /**
141
     * @return string
142
     */
143
    private function getFileName()
144
    {
145
        return Str::studly($this->argument('name'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') targeting Illuminate\Console\Conce...ractsWithIO::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...
146
    }
147
}
148