Completed
Pull Request — master (#1163)
by
unknown
03:47 queued 10s
created

ModuleMakeCommand::handle()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
nc 11
nop 0
dl 0
loc 30
ccs 17
cts 17
cp 1
crap 7
rs 8.5066
c 0
b 0
f 0
1
<?php
2
3
namespace Nwidart\Modules\Commands;
4
5
use Illuminate\Console\Command;
6
use Nwidart\Modules\Contracts\ActivatorInterface;
7
use Nwidart\Modules\Generators\DatabaseModuleGenerator;
8
use Nwidart\Modules\Generators\ModuleGenerator;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputOption;
11
12
class ModuleMakeCommand extends Command
13
{
14
    /**
15
     * The console command name.
16
     *
17
     * @var string
18
     */
19
    protected $name = 'module:make';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Create a new module.';
27
28
    /**
29
     * Execute the console command.
30 130
     */
31
    public function handle() : int
32 130
    {
33 130
        $names = $this->argument('name');
34
        $success = true;
35 130
36 130
        if (empty($names) || !is_array($names)) {
37 130
            $this->error('The names of modules are required');
38 130
            return 1;
39 130
        }
40 130
41 130
        foreach ($names as $name) {
42 130
            $generator = config('modules.database_management.enabled') ? new DatabaseModuleGenerator($name) : new ModuleGenerator($name);
43 130
            $code = with($generator)
44 130
                ->setFilesystem($this->laravel['files'])
45 130
                ->setModule($this->laravel['modules'])
46
                ->setConfig($this->laravel['config'])
47 130
                ->setActivator($this->laravel[ActivatorInterface::class])
48 1
                ->setConsole($this)
49
                ->setForce($this->option('force'))
50
                ->setType($this->getModuleType())
51
                ->setActive(!$this->option('disabled'))
52 130
                ->generate();
53
54
            if ($code === E_ERROR) {
55
                $success = false;
56
            }
57
        }
58
59
        return $success ? 0 : E_ERROR;
60 130
    }
61
62
    /**
63 130
     * Get the console command arguments.
64
     *
65
     * @return array
66
     */
67 130
    protected function getArguments()
68
    {
69
        return [
70 130
            ['name', InputArgument::IS_ARRAY, 'The names of modules will be created.'],
71
        ];
72
    }
73
74
    protected function getOptions()
75
    {
76
        return [
77
            ['plain', 'p', InputOption::VALUE_NONE, 'Generate a plain module (without some resources).'],
78
            ['api', null, InputOption::VALUE_NONE, 'Generate an api module.'],
79
            ['web', null, InputOption::VALUE_NONE, 'Generate a web module.'],
80
            ['disabled', 'd', InputOption::VALUE_NONE, 'Do not enable the module at creation.'],
81
            ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when the module already exists.'],
82
        ];
83
    }
84
85
    /**
86
    * Get module type .
87
    *
88
    * @return string
89
    */
90
    private function getModuleType()
91
    {
92
        $isPlain = $this->option('plain');
93
        $isApi = $this->option('api');
94
95
        if ($isPlain && $isApi) {
96
            return 'web';
97
        }
98
        if ($isPlain) {
99
            return 'plain';
100
        } elseif ($isApi) {
101
            return 'api';
102
        } else {
103
            return 'web';
104
        }
105
    }
106
}
107