GenerateMiddlewareCommand::getTemplateContents()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
3
namespace Consigliere\Components\Commands;
4
5
use Illuminate\Support\Str;
6
use Consigliere\Components\Support\Stub;
7
use Consigliere\Components\Traits\ComponentCommandTrait;
8
use Symfony\Component\Console\Input\InputArgument;
9
10 View Code Duplication
class GenerateMiddlewareCommand extends Command
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...
11
{
12
    use ComponentCommandTrait;
13
14
    /**
15
     * The name of argument name.
16
     *
17
     * @var string
18
     */
19
    protected $argumentName = 'name';
20
21
    /**
22
     * The console command name.
23
     *
24
     * @var string
25
     */
26
    protected $name = 'component:make-middleware';
27
28
    /**
29
     * The console command description.
30
     *
31
     * @var string
32
     */
33
    protected $description = 'Generate new middleware class for the specified component.';
34
35
    /**
36
     * Get the console command arguments.
37
     *
38
     * @return array
39
     */
40
    protected function getArguments()
41
    {
42
        return [
43
            ['name', InputArgument::REQUIRED, 'The name of the command.'],
44
            ['component', InputArgument::OPTIONAL, 'The name of component will be used.'],
45
        ];
46
    }
47
48
    /**
49
     * @return mixed
50
     */
51
    protected function getTemplateContents()
52
    {
53
        $component = $this->laravel['components']->findOrFail($this->getComponentName());
54
55
        return (new Stub('/middleware.stub', [
56
            'NAMESPACE'           => $this->getClassNamespace($component),
57
            'CLASS'               => $this->getClass(),
58
            'LOWER_NAME'          => $component->getLowerName(),
59
            'COMPONENT'           => $this->getComponentName(),
60
            'NAME'                => $this->getFileName(),
61
            'STUDLY_NAME'         => $this->getFileName(),
62
            'COMPONENT_NAMESPACE' => $this->laravel['components']->config('namespace'),
63
        ]))->render();
64
    }
65
66
    /**
67
     * @return mixed
68
     */
69
    protected function getDestinationFilePath()
70
    {
71
        $path = $this->laravel['components']->getComponentPath($this->getComponentName());
72
73
        $seederPath = $this->laravel['components']->config('paths.generator.middleware');
74
75
        return $path . $seederPath . '/' . $this->getFileName() . '.php';
76
    }
77
78
    /**
79
     * @return string
80
     */
81
    private function getFileName()
82
    {
83
        return Str::studly($this->argument('name'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') targeting Illuminate\Console\Command::argument() can also be of type array; 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...
84
    }
85
86
    /**
87
     * Get default namespace.
88
     *
89
     * @return string
90
     */
91
    public function getDefaultNamespace()
92
    {
93
        return 'Http\Middleware';
94
    }
95
}
96