Passed
Push — master ( ff4e9f...624a9a )
by Arthur
35:46
created

ModelMakeCommand   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Test Coverage

Coverage 88.57%

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 112
ccs 31
cts 35
cp 0.8857
rs 10
c 0
b 0
f 0
wmc 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
A handleMongoOption() 0 3 1
A setOptions() 0 5 1
A stubOptions() 0 7 1
A needsMigration() 0 3 1
A isMongoModel() 0 3 1
A handleMigrationOption() 0 3 1
A stubName() 0 7 2
A afterGeneration() 0 19 3
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 2
    protected function stubOptions(): array
51
    {
52
        return [
53 2
            'NAMESPACE' => $this->getClassNamespace(),
54 2
            'CLASS' => $this->getClassName(),
55 2
            'MONGO' => $this->isMongoModel(),
56 2
            'MIGRATION' => $this->needsMigration()
57
        ];
58
    }
59
60
    /**
61
     * Get the console command options.
62
     *
63
     * @return array
64
     */
65 112
    protected function setOptions(): array
66
    {
67
        return [
68 112
            ['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
    {
75
        return $this->confirm('Is this model for a mongodb database?', $default);
76
    }
77
78 2
    protected function isMongoModel(): bool
79
    {
80 2
        return $this->getOption('mongo');
81
    }
82
83
    protected function handleMigrationOption($shortcut, $type, $question, $default)
0 ignored issues
show
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

83
    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...
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

83
    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 $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

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