Completed
Pull Request — master (#633)
by
unknown
03:18
created

RouteProviderMakeCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 95
Duplicated Lines 14.74 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 14
loc 95
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 5

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getArguments() 0 6 1
A getTemplateContents() 14 14 1
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 94
    protected function getArguments()
36
    {
37
        return [
38 94
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
39
        ];
40
    }
41
42
    /**
43
     * Get template contents.
44
     *
45
     * @return string
46
     */
47 86 View Code Duplication
    protected function getTemplateContents()
48
    {
49 86
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
50
51 86
        return (new Stub('/route-provider.stub', [
52 86
            'NAMESPACE'        => $this->getClassNamespace($module),
53 86
            'CLASS'            => $this->getFileName(),
54 86
            'MODULE_NAMESPACE' => $this->laravel['modules']->config('namespace'),
55 86
            'MODULE'           => $this->getModuleName(),
56 86
            'WEB_ROUTES_PATH'  => $this->getWebRoutesPath(),
57 86
            'API_ROUTES_PATH'  => $this->getApiRoutesPath(),
58 86
            'LOWER_NAME'       => $module->getLowerName(),
59 86
        ]))->render();
60
    }
61
62
    /**
63
     * @return string
64
     */
65 86
    private function getFileName()
66
    {
67 86
        return 'RouteServiceProvider';
68
    }
69
70
    /**
71
     * Get the destination file path.
72
     *
73
     * @return string
74
     */
75 86
    protected function getDestinationFilePath()
76
    {
77 86
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
78
79 86
        $generatorPath = GenerateConfigReader::read('provider');
80
81 86
        return $path . $generatorPath->getPath() . '/' . $this->getFileName() . '.php';
82
    }
83
84
    /**
85
     * @return mixed
86
     */
87 86
    protected function getWebRoutesPath()
88
    {
89 86
        return '/' . $this->laravel['config']->get('stubs.files.routes', 'Routes/web.php');
90
    }
91
92
    /**
93
     * @return mixed
94
     */
95 86
    protected function getApiRoutesPath()
96
    {
97 86
        return '/' . $this->laravel['config']->get('stubs.files.routes', 'Routes/api.php');
98
    }
99
100 86
    public function getDefaultNamespace() : string
101
    {
102 86
        return $this->laravel['modules']->config('paths.generator.provider.path', 'Providers');
103
    }
104
}
105