EnumMakeCommand::getDefaultNamespace()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 14
ccs 7
cts 7
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Cerbero\LaravelEnum\Console\Commands;
4
5
use Illuminate\Console\GeneratorCommand;
6
use Illuminate\Support\Str;
7
use Cerbero\LaravelEnum\StubAssembler;
8
use Cerbero\LaravelEnum\Parser;
9
10
/**
11
 * The Artisan command to generate Enum classes.
12
 *
13
 */
14
class EnumMakeCommand extends GeneratorCommand
15
{
16
    /**
17
     * The name and signature of the console command.
18
     *
19
     * @var string
20
     */
21
    protected $signature = 'make:enum
22
                            {name : The name of the class}
23
                            {enum : The definition of the enum}
24
                            {--p|path= : The path to generate enums in}
25
                            {--f|force : Create the class even if the enum already exists}';
26
27
    /**
28
     * The console command description.
29
     *
30
     * @var string
31
     */
32
    protected $description = 'Create a new enum class';
33
34
    /**
35
     * The type of class being generated.
36
     *
37
     * @var string
38
     */
39
    protected $type = 'Enum';
40
41
    /**
42
     * Get the stub file for the generator.
43
     *
44
     * @return string
45
     */
46 9
    protected function getStub()
47
    {
48 9
        return __DIR__ . '/../../../stubs/enum.stub';
49
    }
50
51
    /**
52
     * Get the default namespace for the class.
53
     *
54
     * @param  string  $rootNamespace
55
     * @return string
56
     */
57 9
    protected function getDefaultNamespace($rootNamespace)
58
    {
59 9
        if ($path = $this->option('path')) {
60
            // Ensure the path starts with "app/"
61 3
            $path = Str::start(ltrim($path, '/'), 'app/');
62
            // Remove "app/" from the beginning of the path
63 3
            $path = preg_replace('#^app\/#', '', $path);
64
            // Convert the path into namespace
65 3
            $namespace = implode('\\', array_map('ucfirst', explode('/', $path)));
66
            // Prepend the root namespace
67 3
            return $rootNamespace . '\\' . $namespace;
68
        }
69
70 6
        return $rootNamespace . '\Enums';
71
    }
72
73
    /**
74
     * Build the class with the given name.
75
     *
76
     * @param  string  $name
77
     * @return string
78
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
79
     */
80 9
    protected function buildClass($name)
81
    {
82 9
        $stub = parent::buildClass($name);
83 9
        $enums = $this->parseEnums();
84
85 9
        return (new StubAssembler($stub, $enums))
86 9
            ->replaceMethodTags()
87 9
            ->replaceConstants()
88 9
            ->replaceMap()
89 9
            ->getStub();
90
    }
91
92
    /**
93
     * Parse the command syntax and retrieve the enums
94
     *
95
     * @return array
96
     */
97 9
    private function parseEnums() : array
98
    {
99
        // Normalise definition as argument() may return an array
100 9
        $enum = (array) $this->argument('enum');
101 9
        $definition = trim($enum[0]);
102
103 9
        return (new Parser)->parseDefinition($definition);
104
    }
105
}
106