Completed
Push — master ( 8b259f...9e5224 )
by Nicolas
25s queued 11s
created

RouteProviderMakeCommand::getArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Nwidart\Modules\Commands;
4
5
use Nwidart\Modules\Support\Config\GenerateConfigReader;
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 RouteProviderMakeCommand extends GeneratorCommand
12
{
13
    use ModuleCommandTrait;
14
15
    protected $argumentName = 'module';
16
17
    /**
18
     * The command name.
19
     *
20
     * @var string
21
     */
22
    protected $name = 'module:route-provider';
23
24
    /**
25
     * The command description.
26
     *
27
     * @var string
28
     */
29
    protected $description = 'Create a new route service provider for the specified module.';
30
31
    /**
32
     * The command arguments.
33
     *
34
     * @return array
35
     */
36 127
    protected function getArguments()
37
    {
38
        return [
39 127
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
40
        ];
41
    }
42
43 127
    protected function getOptions()
44
    {
45
        return [
46 127
            ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when the file already exists.'],
47
        ];
48
    }
49
50
    /**
51
     * Get template contents.
52
     *
53
     * @return string
54
     */
55 117 View Code Duplication
    protected function getTemplateContents()
56
    {
57 117
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
58
59 117
        return (new Stub('/route-provider.stub', [
60 117
            'NAMESPACE'            => $this->getClassNamespace($module),
61 117
            'CLASS'                => $this->getFileName(),
62 117
            'MODULE_NAMESPACE'     => $this->laravel['modules']->config('namespace'),
63 117
            'MODULE'               => $this->getModuleName(),
64 117
            'CONTROLLER_NAMESPACE' => $this->getControllerNameSpace(),
65 117
            'WEB_ROUTES_PATH'      => $this->getWebRoutesPath(),
66 117
            'API_ROUTES_PATH'      => $this->getApiRoutesPath(),
67 117
            'LOWER_NAME'           => $module->getLowerName(),
68 117
        ]))->render();
69
    }
70
71
    /**
72
     * @return string
73
     */
74 117
    private function getFileName()
75
    {
76 117
        return 'RouteServiceProvider';
77
    }
78
79
    /**
80
     * Get the destination file path.
81
     *
82
     * @return string
83
     */
84 117
    protected function getDestinationFilePath()
85
    {
86 117
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
87
88 117
        $generatorPath = GenerateConfigReader::read('provider');
89
90 117
        return $path . $generatorPath->getPath() . '/' . $this->getFileName() . '.php';
91
    }
92
93
    /**
94
     * @return mixed
95
     */
96 117
    protected function getWebRoutesPath()
97
    {
98 117
        return '/' . $this->laravel['modules']->config('stubs.files.routes/web', 'Routes/web.php');
99
    }
100
101
    /**
102
     * @return mixed
103
     */
104 117
    protected function getApiRoutesPath()
105
    {
106 117
        return '/' . $this->laravel['modules']->config('stubs.files.routes/api', 'Routes/api.php');
107
    }
108
109 117
    public function getDefaultNamespace() : string
110
    {
111 117
        $module = $this->laravel['modules'];
112
113 117
        return $module->config('paths.generator.provider.namespace') ?: $module->config('paths.generator.provider.path', 'Providers');
114
    }
115
116
    /**
117
     * @return string
118
     */
119 117
    private function getControllerNameSpace(): string
120
    {
121 117
        $module = $this->laravel['modules'];
122 117
        return str_replace('/', '\\', $module->config('paths.generator.controller.namespace') ?: $module->config('paths.generator.controller.path', 'Controller'));
123
    }
124
}
125