GeneratorCommand::getFileName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Rawilk\LaravelModules\Commands\Generators;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Str;
7
use Rawilk\LaravelModules\Exceptions\FileAlreadyExists;
8
use Rawilk\LaravelModules\Generators\FileGenerator;
9
use Rawilk\LaravelModules\Module;
10
11
abstract class GeneratorCommand extends Command
12
{
13
    /**
14
     * The name of the 'name' argument.
15
     *
16
     * @var string
17
     */
18
    protected $argumentName = '';
19
20
    abstract protected function getDestinationFilePath(): string;
21
22
    abstract protected function getTemplateContents(): string;
23
24
    public function handle(): void
25
    {
26
        $path = str_replace('\\', '/', $this->getDestinationFilePath());
27
28
        if (! $this->laravel['files']->isDirectory($dir = dirname($path))) {
29
            $this->laravel['files']->makeDirectory($dir, 0777, true);
30
        }
31
32
        $contents = $this->getTemplateContents();
33
34
        try {
35
            $overwriteFile = $this->hasOption('force') ? $this->option('force') : false;
36
            (new FileGenerator($path, $contents))->withFileOverwrite($overwriteFile)->generate();
37
38
            $this->info("Created: {$path}");
39
        } catch (FileAlreadyExists $e) {
40
            $this->error("File: {$path} already exists.");
41
        }
42
    }
43
44
    /**
45
     * Get the base class to extend for a given class type.
46
     *
47
     * @param string $classType
48
     * @param bool $returnBasename
49
     * @return string
50
     */
51
    protected function getBaseClass(string $classType, bool $returnBasename = false): string
52
    {
53
        $baseClass = $this->hasOption('base_class') && $this->option('base_class') !== null
54
            ? $this->option('base_class')
55
            : $this->laravel['modules']->config("base_classes.{$classType}");
56
57
        return $returnBasename ? class_basename($baseClass) : str_replace('/', '\\', $baseClass);
0 ignored issues
show
Bug introduced by
It seems like $baseClass can also be of type string[]; however, parameter $class of class_basename() does only seem to accept object|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
        return $returnBasename ? class_basename(/** @scrutinizer ignore-type */ $baseClass) : str_replace('/', '\\', $baseClass);
Loading history...
58
    }
59
60
    protected function getClass(): string
61
    {
62
        return class_basename($this->argument($this->argumentName));
0 ignored issues
show
Bug introduced by
It seems like $this->argument($this->argumentName) can also be of type string[]; however, parameter $class of class_basename() does only seem to accept object|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
        return class_basename(/** @scrutinizer ignore-type */ $this->argument($this->argumentName));
Loading history...
63
    }
64
65
    protected function getClassNamespace(Module $module): string
66
    {
67
        $namespace = $this->laravel['modules']->config('namespace');
68
69
        $namespace .= '\\' . $module->getStudlyName();
70
71
        $namespace .= '\\' . $this->getDefaultNamespace();
72
73
        $namespace .= '\\' . $this->getExtraNamespace($this->argument($this->argumentName));
0 ignored issues
show
Bug introduced by
It seems like $this->argument($this->argumentName) can also be of type null and string[]; however, parameter $path of Rawilk\LaravelModules\Co...nd::getExtraNamespace() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
        $namespace .= '\\' . $this->getExtraNamespace(/** @scrutinizer ignore-type */ $this->argument($this->argumentName));
Loading history...
74
75
        $namespace = str_replace('/', '\\', $namespace);
76
77
        return trim($namespace, '\\');
78
    }
79
80
    protected function getDefaultNamespace(): string
81
    {
82
        return '';
83
    }
84
85
    protected function getFileName(): string
86
    {
87
        return Str::studly($this->argument($this->argumentName));
0 ignored issues
show
Bug introduced by
It seems like $this->argument($this->argumentName) can also be of type string[]; however, parameter $value of Illuminate\Support\Str::studly() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
        return Str::studly(/** @scrutinizer ignore-type */ $this->argument($this->argumentName));
Loading history...
88
    }
89
90
    private function getExtraNamespace(string $path): string
91
    {
92
        $path = str_replace('/', '\\', $path);
93
        $pieces = explode("\\", $path);
94
95
        return implode("\\", array_slice($pieces, 0, count($pieces) - 1) );
96
    }
97
}
98