FactoryMakeCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 15
c 1
b 0
f 0
dl 0
loc 41
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getDestinationFilePath() 0 7 1
A getTemplateContents() 0 5 1
A getModelName() 0 9 3
1
<?php
2
3
namespace Rawilk\LaravelModules\Commands\Generators;
4
5
use Illuminate\Support\Str;
6
use Rawilk\LaravelModules\Support\Config\GenerateConfigReader;
7
use Rawilk\LaravelModules\Support\Stub;
8
use Rawilk\LaravelModules\Traits\ModuleCommands;
9
10
class FactoryMakeCommand extends GeneratorCommand
11
{
12
    use ModuleCommands;
13
14
    /** @var string */
15
    protected $argumentName = 'name';
16
17
    /** @var string */
18
    protected $signature = 'module:make-factory
19
                            {name : The name of the factory}
20
                            {module? : The name of the module to create the factory for}
21
                            {--model= : The name of the model the factory is for}';
22
23
    /** @var string */
24
    protected $description = 'Create a new model factory for the specified module.';
25
26
    protected function getDestinationFilePath(): string
27
    {
28
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
29
30
        $factoryPath = GenerateConfigReader::read('factory');
31
32
        return $path . $factoryPath->getPath() . '/' . $this->getFileName() . '.php';
33
    }
34
35
    protected function getTemplateContents(): string
36
    {
37
        return (new Stub('/factory.stub', [
38
            'MODEL' => $this->getModelName()
39
        ]))->render();
40
    }
41
42
    private function getModelName(): string
43
    {
44
        $model = $this->option('model') ?: 'Model::class';
45
46
        if (! Str::contains(strtolower($model), '::class')) {
47
            $model .= '::class';
48
        }
49
50
        return $model;
51
    }
52
}
53