Completed
Pull Request — master (#1088)
by
unknown
09:38
created

GeneratorCommand::beforeHandle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
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
     * Method to apply necessary functionality
34
     * before console command gets executed
35 126
     *
36
     * @return void
37 126
     */
38
    public function beforeHandle()
39 126
    {
40 20
        // ...
41
    }
42
43 126
    /**
44
     * Execute the console command.
45
     */
46 126
    public function handle(): int
47 126
    {
48
        $this->beforeHandle();
49 126
50 2
        $path = str_replace('\\', '/', $this->getDestinationFilePath());
51 2
52 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...
53 2
            $this->laravel['files']->makeDirectory($dir, 0777, true);
54
        }
55
56 126
        $contents = $this->getTemplateContents();
57
58
        try {
59
60
            $overwriteFile = $this->hasOption('force') ? $this->option('force') : false;
61
            (new FileGenerator($path, $contents))->withFileOverwrite($overwriteFile)->generate();
62
            $this->info("Created " . basename($path) . " file");
63
        } catch (FileAlreadyExistException $e) {
64 126
65
            $this->critical("File [ {$path} ] already exists.");
66 126
            return E_ERROR;
67
        }
68
69
        return 0;
70
    }
71
72
    /**
73
     * Get class name.
74
     *
75
     * @return string
76
     */
77
    public function getClass()
78
    {
79
        return class_basename($this->argument($this->argumentName));
0 ignored issues
show
Bug introduced by
It seems like $this->argument($this->argumentName) targeting Illuminate\Console\Conce...ractsWithIO::argument() can also be of type array or null; 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...
80
    }
81
82
    /**
83
     * Get default namespace.
84
     *
85
     * @return string
86 126
     */
87
    public function getDefaultNamespace(): string
88 126
    {
89
        return '';
90 126
    }
91
92 126
    /**
93
     * Get class namespace.
94 126
     *
95
     * @param \Nwidart\Modules\Module $module
96 126
     *
97
     * @return string
98 126
     */
99
    public function getClassNamespace($module)
100 126
    {
101
        $extra = str_replace($this->getClass(), '', $this->argument($this->argumentName));
102 126
103
        $extra = str_replace('/', '\\', $extra);
104
105
        $namespace = $this->laravel['modules']->config('namespace');
106
107
        $namespace .= '\\' . $module->getStudlyName();
108
109
        $namespace .= '\\' . $this->getDefaultNamespace();
110
111
        $namespace .= '\\' . $extra;
112
113
        $namespace = str_replace('/', '\\', $namespace);
114
115
        return trim($namespace, '\\');
116
    }
117
}
118