Completed
Push — master ( 15e02a...758c52 )
by Nicolas
05:13
created

GeneratorCommand::getDestinationFilePath()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 1
ccs 0
cts 0
cp 0
nc 1
1
<?php
2
3
namespace Nwidart\Modules\Commands;
4
5
use Illuminate\Console\Command;
6
use Nwidart\Modules\Exceptions\FileAlreadyExistException;
7
use Nwidart\Modules\Generators\FileGenerator;
8
9
abstract class GeneratorCommand extends Command
10
{
11
    /**
12
     * The name of 'name' argument.
13
     *
14
     * @var string
15
     */
16
    protected $argumentName = '';
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 16
    public function fire()
36
    {
37 16
        $path = str_replace('\\', '/', $this->getDestinationFilePath());
38
39 16
        if (!$this->laravel['files']->isDirectory($dir = dirname($path))) {
40
            $this->laravel['files']->makeDirectory($dir, 0777, true);
41 16
        }
42
43 16
        $contents = $this->getTemplateContents();
44
45
        try {
46 16
            with(new FileGenerator($path, $contents))->generate();
47
48 16
            $this->info("Created : {$path}");
49 16
        } catch (FileAlreadyExistException $e) {
50
            $this->error("File : {$path} already exists.");
51
        }
52 16
    }
53
54
    /**
55
     * Get class name.
56
     *
57
     * @return string
58
     */
59 16
    public function getClass()
60
    {
61 16
        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...
62 4
    }
63
64
    /**
65
     * Get default namespace.
66
     *
67
     * @return string
68
     */
69 6
    public function getDefaultNamespace()
70
    {
71 6
        return '';
72
    }
73
74
    /**
75
     * Get class namespace.
76
     *
77
     * @param \Nwidart\Module\Module $module
78
     *
79
     * @return string
80
     */
81 16
    public function getClassNamespace($module)
82 3
    {
83 16
        $extra = str_replace($this->getClass(), '', $this->argument($this->argumentName));
84
85 16
        $extra = str_replace('/', '\\', $extra);
86
87 16
        $namespace = $this->laravel['modules']->config('namespace');
88
89 16
        $namespace .= '\\'.$module->getStudlyName();
90
91 16
        $namespace .= '\\'.$this->getDefaultNamespace();
92
93 16
        $namespace .= '\\'.$extra;
94
95 16
        return rtrim($namespace, '\\');
96
    }
97
}
98