Completed
Pull Request — master (#3)
by
unknown
59s
created

MakeModel   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 10
dl 0
loc 67
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 12 3
A getStub() 0 4 1
A getDefaultNamespace() 0 4 1
A createMigration() 0 8 1
A createFactory() 0 8 1
A runCommand() 0 21 1
1
<?php
2
3
namespace BeyondCode\LaravelPackageTools\Commands;
4
5
use Illuminate\Support\Str;
6
use Symfony\Component\Console\Input\ArrayInput;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputDefinition;
11
use Symfony\Component\Console\Output\BufferedOutput;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
class MakeModel extends GeneratorCommand
15
{
16
    protected $type = 'Model';
17
18
    public function __invoke(InputInterface $input, OutputInterface $output)
19
    {
20
        parent::__invoke($input, $output);
21
22
        if ($this->input->getOption('migration')) {
23
            $this->createMigration();
24
        }
25
26
        if ($this->input->getOption('factory')) {
27
            $this->createFactory();
28
        }
29
    }
30
31
    protected function getStub()
32
    {
33
        return __DIR__.'/stubs/model.stub';
34
    }
35
36
    protected function getDefaultNamespace($rootNamespace): string
37
    {
38
        return $rootNamespace.'\Models';
39
    }
40
41
    private function createMigration()
42
    {
43
        $table = Str::snake(Str::plural(class_basename($this->getNameInput())));
44
45
        $this->runCommand("create_{$table}_table", ['--create' => "{$table}"], new MakeMigration);
46
47
        $this->info(PHP_EOL."Created migration: create_{$table}_table");
48
    }
49
50
    protected function createFactory()
51
    {
52
        $model = $this->getNameInput();
53
54
        $this->runCommand("{$model}Factory", ['--model' => "{$model}"], new MakeFactory);
55
56
        $this->info(PHP_EOL.'Factory created successfully.');
57
    }
58
59
    protected function runCommand($name, $options, GeneratorCommand $command)
60
    {
61
        $input = new ArrayInput(
62
            array_merge([
63
            'name'  => $name,
64
            '--force' => false,
65
            ], $options),
66
            new InputDefinition([
67
                new InputArgument('name'),
68
                new InputOption('create'),
69
                new InputOption('model'),
70
                new InputOption('force'),
71
            ])
72
        );
73
74
        $output = new BufferedOutput();
75
76
        $command->outputPath = ($this->outputPath);
77
78
        $command->__invoke($input, $output);
79
    }
80
}
81