Completed
Pull Request — master (#1088)
by
unknown
02:55
created

GeneratorCommand::getClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Nwidart\Modules\Commands;
4
5
use Nwidart\Modules\Console\Command;
6
use Nwidart\Modules\Generators\FileGenerator;
7
use Nwidart\Modules\Exceptions\FileAlreadyExistException;
8
9
abstract class GeneratorCommand extends Command
10
{
11
    /**
12
     * The name of 'name' argument.
13
     *
14
     * @var string
15
     */
16
    protected $argumentName = 'name';
17
18
    /**
19
     * Get template contents.
20
     *
21
     * @return string
22
     */
23
    abstract protected function getTemplateContents();
24
25
    /**
26
     * Get the destination file path.
27
     *
28
     * @return string
29
     */
30
    abstract protected function getDestinationFilePath();
31
32
    /**
33
     * Execute the console command.
34
     */
35
    public function handle(): int
36
    {
37
        $path = str_replace('\\', '/', $this->getDestinationFilePath());
38
39 View Code Duplication
        if (!$this->laravel['files']->isDirectory($dir = dirname($path))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
40
            $this->laravel['files']->makeDirectory($dir, 0777, true);
41
        }
42
43
        $contents = $this->getTemplateContents();
44
45
        try {
46
47
            $overwriteFile = $this->hasOption('force') ? $this->option('force') : false;
48
            (new FileGenerator($path, $contents))->withFileOverwrite($overwriteFile)->generate();
49
            $this->info("Created " . basename($path) . " file");
50
        } catch (FileAlreadyExistException $e) {
51
52
            $this->critical("File [ {$path} ] already exists.");
53
            return E_ERROR;
54
        }
55
56
        return 0;
57
    }
58
59
    /**
60
     * Get class name.
61
     *
62
     * @return string
63
     */
64
    public function getClass()
65
    {
66
        return class_basename($this->getFileName());
0 ignored issues
show
Documentation Bug introduced by
The method getFileName does not exist on object<Nwidart\Modules\Commands\GeneratorCommand>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
67
    }
68
69
    /**
70
     * Get default namespace.
71
     *
72
     * @return string
73
     */
74
    public function getDefaultNamespace(): string
75
    {
76
        return '';
77
    }
78
79
    /**
80
     * Get class namespace.
81
     *
82
     * @param \Nwidart\Modules\Module $module
83
     *
84
     * @return string
85
     */
86
    public function getClassNamespace($module)
87
    {
88
        $extra = str_replace($this->getClass(), '', $this->argument($this->argumentName));
89
90
        $extra = str_replace('/', '\\', $extra);
91
92
        $namespace = $this->laravel['modules']->config('namespace');
93
94
        $namespace .= '\\' . $module->getStudlyName();
95
96
        $namespace .= '\\' . $this->getDefaultNamespace();
97
98
        $namespace .= '\\' . $extra;
99
100
        $namespace = str_replace('/', '\\', $namespace);
101
102
        return trim($namespace, '\\');
103
    }
104
}
105