Completed
Pull Request — master (#666)
by reallyli
02:28
created

GeneratorCommand::getDestinationFilePath()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
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
    public function handle()
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
            with(new FileGenerator($path, $contents))->generate();
47
48
            $this->info("Created : {$path}");
49
        } catch (FileAlreadyExistException $e) {
50
            $this->error("File : {$path} already exists.");
51
        }
52
    }
53
54
    /**
55
     * Get class name.
56
     *
57
     * @return string
58
     */
59
    public function getClass()
60
    {
61
        return class_basename($this->argument($this->argumentName));
62
    }
63
64
    /**
65
     * Get default namespace.
66
     *
67
     * @return string
68
     */
69
    public function getDefaultNamespace() : string
70
    {
71
        return '';
72
    }
73
74
    /**
75
     * Get class namespace.
76
     *
77
     * @param \Nwidart\Modules\Module $module
78
     *
79
     * @return string
80
     */
81
    public function getClassNamespace($module)
82
    {
83
        $extra = str_replace($this->getClass(), '', $this->argument($this->argumentName));
84
85
        $extra = str_replace('/', '\\', $extra);
86
87
        $namespace = $this->laravel['modules']->config('namespace');
88
89
        $namespace .= '\\' . $module->getStudlyName();
90
91
        $namespace .= '\\' . $this->getDefaultNamespace();
92
93
        $namespace .= '\\' . $extra;
94
95
        $namespace = str_replace('/', '\\', $namespace);
96
97
        return trim($namespace, '\\');
98
    }
99
}
100