Completed
Pull Request — master (#666)
by reallyli
02:28
created

RouteProviderMakeCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 109
Duplicated Lines 14.68 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 16
loc 109
ccs 0
cts 28
cp 0
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 5

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getArguments() 0 6 1
A getOptions() 0 6 1
A getTemplateContents() 16 16 2
A getFileName() 0 4 1
A getDestinationFilePath() 0 8 1
A getWebRoutesPath() 0 4 1
A getApiRoutesPath() 0 4 1
A getDefaultNamespace() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
10
class RouteProviderMakeCommand extends GeneratorCommand
11
{
12
    use ModuleCommandTrait;
13
14
    protected $argumentName = 'module';
15
16
    /**
17
     * The command name.
18
     *
19
     * @var string
20
     */
21
    protected $name = 'module:route-provider';
22
23
    /**
24
     * The command description.
25
     *
26
     * @var string
27
     */
28
    protected $description = 'Create a new route service provider for the specified module.';
29
30
    /**
31
     * The command arguments.
32
     *
33
     * @return array
34
     */
35
    protected function getArguments()
36
    {
37
        return [
38
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
39
        ];
40
    }
41
42
    /**
43
     * Get the console command options.
44
     *
45
     * @return array
46
     */
47
    protected function getOptions()
48
    {
49
        return [
50
            ['api', null, InputOption::VALUE_NONE, 'Indicates the api route service provider', null],
51
        ];
52
    }
53
54
    /**
55
     * Get template contents.
56
     *
57
     * @return string
58
     */
59 View Code Duplication
    protected function getTemplateContents()
60
    {
61
        $stubFile = $this->option('api') ? '/api-route-provider.stub' : '/route-provider.stub';
62
63
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
64
65
        return (new Stub($stubFile, [
66
            'NAMESPACE'        => $this->getClassNamespace($module),
67
            'CLASS'            => $this->getFileName(),
68
            'MODULE_NAMESPACE' => $this->laravel['modules']->config('namespace'),
69
            'MODULE'           => $this->getModuleName(),
70
            'WEB_ROUTES_PATH'  => $this->getWebRoutesPath(),
71
            'API_ROUTES_PATH'  => $this->getApiRoutesPath(),
72
            'LOWER_NAME'       => $module->getLowerName(),
73
        ]))->render();
74
    }
75
76
    /**
77
     * @return string
78
     */
79
    private function getFileName()
80
    {
81
        return 'RouteServiceProvider';
82
    }
83
84
    /**
85
     * Get the destination file path.
86
     *
87
     * @return string
88
     */
89
    protected function getDestinationFilePath()
90
    {
91
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
92
93
        $generatorPath = GenerateConfigReader::read('provider');
94
95
        return $path . $generatorPath->getPath() . '/' . $this->getFileName() . '.php';
96
    }
97
98
    /**
99
     * @return mixed
100
     */
101
    protected function getWebRoutesPath()
102
    {
103
        return '/' . $this->laravel['config']->get('stubs.files.routes', 'Routes/web.php');
104
    }
105
106
    /**
107
     * @return mixed
108
     */
109
    protected function getApiRoutesPath()
110
    {
111
        return '/' . $this->laravel['config']->get('stubs.files.routes', 'Routes/api.php');
112
    }
113
114
    public function getDefaultNamespace() : string
115
    {
116
        return $this->laravel['modules']->config('paths.generator.provider.path', 'Providers');
117
    }
118
}
119