Completed
Push — master ( eb3a14...ff6e8c )
by Nicolas
03:49
created

GeneratorCommand::getDefaultNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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\FileAlreadyExistException;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Cannot use Nwidart\Modules\Generators\FileAlreadyExistException as FileAlreadyExistException because the name is already in use
Loading history...
8
use Nwidart\Modules\Generators\FileGenerator;
9
10
abstract class GeneratorCommand extends Command
11
{
12
    /**
13
     * The name of 'name' argument.
14
     *
15
     * @var string
16
     */
17
    protected $argumentName = '';
18
19
    /**
20
     * Get template contents.
21
     *
22
     * @return string
23
     */
24
    abstract protected function getTemplateContents();
25
26
    /**
27
     * Get the destination file path.
28
     *
29
     * @return string
30
     */
31
    abstract protected function getDestinationFilePath();
32
33
    /**
34
     * Execute the console command.
35
     */
36
    public function fire()
37
    {
38
        $path = str_replace('\\', '/', $this->getDestinationFilePath());
39
40
        if (!$this->laravel['files']->isDirectory($dir = dirname($path))) {
41
            $this->laravel['files']->makeDirectory($dir, 0777, true);
42
        }
43
44
        $contents = $this->getTemplateContents();
45
46
        try {
47
            with(new FileGenerator($path, $contents))->generate();
48
49
            $this->info("Created : {$path}");
50
        } catch (FileAlreadyExistException $e) {
51
            $this->error("File : {$path} already exists.");
52
        }
53
    }
54
55
    /**
56
     * Get class name.
57
     *
58
     * @return string
59
     */
60
    public function getClass()
61
    {
62
        return class_basename($this->argument($this->argumentName));
63
    }
64
65
    /**
66
     * Get default namespace.
67
     *
68
     * @return string
69
     */
70
    public function getDefaultNamespace()
71
    {
72
        return '';
73
    }
74
75
    /**
76
     * Get class namespace.
77
     *
78
     * @param \Nwidart\Module\Module $module
79
     *
80
     * @return string
81
     */
82
    public function getClassNamespace($module)
83
    {
84
        $extra = str_replace($this->getClass(), '', $this->argument($this->argumentName));
85
86
        $extra = str_replace('/', '\\', $extra);
87
88
        $namespace = $this->laravel['modules']->config('namespace');
89
90
        $namespace .= '\\'.$module->getStudlyName();
91
92
        $namespace .= '\\'.$this->getDefaultNamespace();
93
94
        $namespace .= '\\'.$extra;
95
96
        return rtrim($namespace, '\\');
97
    }
98
}
99