Command   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 89
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
getTemplateContents() 0 1 ?
getDestinationFilePath() 0 1 ?
A fire() 0 18 3
A getClass() 0 4 1
A getDefaultNamespace() 0 4 1
A getClassNamespace() 0 16 1
1
<?php
2
3
namespace Consigliere\Components\Commands;
4
5
use Consigliere\Components\Exceptions\FileAlreadyExistException;
6
use Consigliere\Components\Generators\FileGenerator;
7
8
abstract class Command extends \Illuminate\Console\Command
9
{
10
    /**
11
     * The name of 'name' argument.
12
     *
13
     * @var string
14
     */
15
    protected $argumentName = '';
16
17
    /**
18
     * Get template contents.
19
     *
20
     * @return string
21
     */
22
    abstract protected function getTemplateContents();
23
24
    /**
25
     * Get the destination file path.
26
     *
27
     * @return string
28
     */
29
    abstract protected function getDestinationFilePath();
30
31
    /**
32
     * Execute the console command.
33
     */
34
    public function fire()
35
    {
36
        $path = str_replace('\\', '/', $this->getDestinationFilePath());
37
38
        if (!$this->laravel['files']->isDirectory($dir = dirname($path))) {
39
            $this->laravel['files']->makeDirectory($dir, 0777, true);
40
        }
41
42
        $contents = $this->getTemplateContents();
43
44
        try {
45
            with(new FileGenerator($path, $contents))->generate();
46
47
            $this->info("Created : {$path}");
48
        } catch (FileAlreadyExistException $e) {
49
            $this->error("File : {$path} already exists.");
50
        }
51
    }
52
53
    /**
54
     * Get class name.
55
     *
56
     * @return string
57
     */
58
    public function getClass()
59
    {
60
        return class_basename($this->argument($this->argumentName));
0 ignored issues
show
Bug introduced by
It seems like $this->argument($this->argumentName) targeting Illuminate\Console\Command::argument() can also be of type array; however, class_basename() does only seem to accept string|object, 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...
61
    }
62
63
    /**
64
     * Get default namespace.
65
     *
66
     * @return string
67
     */
68
    public function getDefaultNamespace()
69
    {
70
        return '';
71
    }
72
73
    /**
74
     * Get class namespace.
75
     *
76
     * @param \Consigliere\Components\Component $component
77
     *
78
     * @return string
79
     */
80
    public function getClassNamespace($component)
81
    {
82
        $extra = str_replace($this->getClass(), '', $this->argument($this->argumentName));
83
84
        $extra = str_replace('/', '\\', $extra);
85
86
        $namespace = $this->laravel['components']->config('namespace');
87
88
        $namespace .= '\\' . $component->getStudlyName();
89
90
        $namespace .= '\\' . $this->getDefaultNamespace();
91
92
        $namespace .= '\\' . $extra;
93
94
        return trim($namespace, '\\');
95
    }
96
}
97