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

PolicyMakeCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 76
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 76
loc 76
ccs 0
cts 29
cp 0
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultNamespace() 4 4 1
A getArguments() 7 7 1
A getTemplateContents() 9 9 1
A getDestinationFilePath() 8 8 1
A getFileName() 4 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 Illuminate\Support\Str;
6
use Nwidart\Modules\Support\Config\GenerateConfigReader;
7
use Nwidart\Modules\Support\Stub;
8
use Nwidart\Modules\Traits\ModuleCommandTrait;
9
use Symfony\Component\Console\Input\InputArgument;
10
11 View Code Duplication
class PolicyMakeCommand extends GeneratorCommand
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
12
{
13
    use ModuleCommandTrait;
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 = 'module:make-policy';
28
29
    /**
30
     * The console command description.
31
     *
32
     * @var string
33
     */
34
    protected $description = 'Create a new policy class for the specified module.';
35
36
    public function getDefaultNamespace() : string
37
    {
38
        return $this->laravel['modules']->config('paths.generator.policies.path', 'Policies');
39
    }
40
41
    /**
42
     * Get the console command arguments.
43
     *
44
     * @return array
45
     */
46
    protected function getArguments()
47
    {
48
        return [
49
            ['name', InputArgument::REQUIRED, 'The name of the policy class.'],
50
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
51
        ];
52
    }
53
54
    /**
55
     * @return mixed
56
     */
57
    protected function getTemplateContents()
58
    {
59
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
60
61
        return (new Stub('/policy.plain.stub', [
62
            'NAMESPACE' => $this->getClassNamespace($module),
63
            'CLASS'     => $this->getClass(),
64
        ]))->render();
65
    }
66
67
    /**
68
     * @return mixed
69
     */
70
    protected function getDestinationFilePath()
71
    {
72
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
73
74
        $policyPath = GenerateConfigReader::read('policies');
75
76
        return $path . $policyPath->getPath() . '/' . $this->getFileName() . '.php';
77
    }
78
79
    /**
80
     * @return string
81
     */
82
    private function getFileName()
83
    {
84
        return Str::studly($this->argument('name'));
85
    }
86
}
87