Passed
Push — master ( 40ce26...965fb6 )
by Arthur
22:04
created

ModelMakeCommand   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 106
rs 10
c 0
b 0
f 0
ccs 0
cts 54
cp 0
wmc 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
A handleMongoOption() 0 2 1
A afterGeneration() 0 13 3
A setOptions() 0 5 1
A stubOptions() 0 7 1
A needsMigration() 0 3 1
A isMongoModel() 0 3 1
A handleMigrationOption() 0 2 1
A stubName() 0 7 2
1
<?php
2
3
namespace Foundation\Generator\Commands;
4
5
use Foundation\Generator\Abstracts\AbstractGeneratorCommand;
6
use Foundation\Generator\Abstracts\ClassGeneratorCommand;
7
use Foundation\Generator\Events\ModelGeneratedEvent;
8
use Foundation\Generator\Managers\GeneratorManager;
9
use Illuminate\Support\Str;
10
use Symfony\Component\Console\Input\InputOption;
11
12
class ModelMakeCommand extends ClassGeneratorCommand
13
{
14
15
    /**
16
     * The console command name.
17
     *
18
     * @var string
19
     */
20
    protected $name = 'larapi:make:model';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Create a new model for the specified module.';
28
29
    /**
30
     * The name of the generated resource.
31
     *
32
     * @var string
33
     */
34
    protected $generatorName = 'model';
35
36
    /**
37
     * The file path.
38
     *
39
     * @var string
40
     */
41
    protected $filePath = '/Entities';
42
43
    /**
44
     * The event that will fire when the file is created.
45
     *
46
     * @var string
47
     */
48
    protected $event = ModelGeneratedEvent::class;
49
50
    protected function stubOptions(): array
51
    {
52
        return [
53
            'NAMESPACE' => $this->getClassNamespace(),
54
            'CLASS' => $this->getClassName(),
55
            'MONGO' => $this->isMongoModel(),
56
            'MIGRATION' => $this->needsMigration()
57
        ];
58
    }
59
60
    /**
61
     * Get the console command options.
62
     *
63
     * @return array
64
     */
65
    protected function setOptions(): array
66
    {
67
        return [
68
            ['mongo', null, InputOption::VALUE_OPTIONAL, 'Mongo model.', false],
69
            ['migration', null, InputOption::VALUE_OPTIONAL, 'Create migration for the model.', true],
70
        ];
71
    }
72
73
    protected function handleMongoOption($shortcut, $type, $question, $default){
0 ignored issues
show
Unused Code introduced by
The parameter $question is not used and could be removed. ( Ignorable by Annotation )

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

73
    protected function handleMongoOption($shortcut, $type, /** @scrutinizer ignore-unused */ $question, $default){

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

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

73
    protected function handleMongoOption($shortcut, /** @scrutinizer ignore-unused */ $type, $question, $default){

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $shortcut is not used and could be removed. ( Ignorable by Annotation )

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

73
    protected function handleMongoOption(/** @scrutinizer ignore-unused */ $shortcut, $type, $question, $default){

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
74
        return $this->confirm('Is this model for a mongodb database?', $default);
75
    }
76
77
    protected function isMongoModel(): bool
78
    {
79
        return $this->getOption('mongo');
80
    }
81
82
    protected function handleMigrationOption($shortcut, $type, $question, $default){
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

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

82
    protected function handleMigrationOption($shortcut, /** @scrutinizer ignore-unused */ $type, $question, $default){

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $question is not used and could be removed. ( Ignorable by Annotation )

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

82
    protected function handleMigrationOption($shortcut, $type, /** @scrutinizer ignore-unused */ $question, $default){

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $shortcut is not used and could be removed. ( Ignorable by Annotation )

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

82
    protected function handleMigrationOption(/** @scrutinizer ignore-unused */ $shortcut, $type, $question, $default){

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
83
        return $this->confirm('Do you want to create a migration for this model?', $default);
84
    }
85
86
    protected function needsMigration(): bool
87
    {
88
        return $this->getOption('migration');
89
    }
90
91
    public function afterGeneration(): void
92
    {
93
        if ($this->needsMigration()) {
94
            if ($this->isMongoModel()) {
95
                GeneratorManager::module($this->getModuleName(), $this->isOverwriteable())->createMigration(
96
                    "Create" . ucfirst($this->getClassName()) . "Collection",
97
                    strtolower(split_caps_to_underscore(Str::plural($this->getClassName()))),
98
                    true);
99
            } else {
100
                GeneratorManager::module($this->getModuleName(), $this->isOverwriteable())->createMigration(
101
                    "Create" . ucfirst($this->getClassName() . "Table"),
102
                    strtolower(split_caps_to_underscore(Str::plural($this->getClassName()))),
103
                    false);
104
            }
105
        }
106
    }
107
108
    /**
109
     * @return string
110
     */
111
    protected function stubName(): string
112
    {
113
        if ($this->isMongoModel()) {
114
            return 'model-mongo.stub';
115
        }
116
117
        return 'model.stub';
118
    }
119
}
120