Completed
Pull Request — master (#1109)
by
unknown
02:05
created

ModuleMakeCommand::getModuleType()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
nc 4
nop 0
dl 0
loc 18
ccs 0
cts 0
cp 0
crap 30
rs 9.3554
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\ModuleGenerator;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputOption;
10
11
class ModuleMakeCommand extends Command
12
{
13
    /**
14
     * The console command name.
15
     *
16
     * @var string
17
     */
18
    protected $name = 'module:make';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Create a new module.';
26
27
    /**
28
     * Execute the console command.
29
     */
30 130
    public function handle() : int
31
    {
32 130
        $names = $this->argument('name');
33 130
        $success = true;
34
35 130
        foreach ($names as $name) {
0 ignored issues
show
Bug introduced by
The expression $names of type string|array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
36 130
            $code = with(new ModuleGenerator($name))
37 130
                ->setFilesystem($this->laravel['files'])
38 130
                ->setModule($this->laravel['modules'])
39 130
                ->setConfig($this->laravel['config'])
40 130
                ->setActivator($this->laravel[ActivatorInterface::class])
41 130
                ->setConsole($this)
42 130
                ->setForce($this->option('force'))
43 130
                ->setType($this->getModuleType())
44 130
                ->setActive(!$this->option('disabled'))
45 130
                ->generate();
46
47 130
            if ($code === E_ERROR) {
48 1
                $success = false;
49
            }
50
        }
51
52 130
        return $success ? 0 : E_ERROR;
53
    }
54
55
    /**
56
     * Get the console command arguments.
57
     *
58
     * @return array
59
     */
60 130
    protected function getArguments()
61
    {
62
        return [
63 130
            ['name', InputArgument::IS_ARRAY, 'The names of modules will be created.'],
64
        ];
65
    }
66
67 130
    protected function getOptions()
68
    {
69
        return [
70 130
            ['plain', 'p', InputOption::VALUE_NONE, 'Generate a plain module (without some resources).'],
71
            ['api', null, InputOption::VALUE_NONE, 'Generate an api module.'],
72
            ['web', null, InputOption::VALUE_NONE, 'Generate a web module.'],
73
            ['disabled', 'd', InputOption::VALUE_NONE, 'Do not enable the module at creation.'],
74
            ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when the module already exists.'],
75
        ];
76
    }
77
78
     /**
79
     * Get module type .
80
     *
81
     * @return string
82
     */
83
    private function getModuleType(){
84
85
        $isPlain = $this->option('plain');
86
        $isApi = $this->option('api');
87
88
        if( $isPlain && $isApi ){
89
            return 'web';
90
        }
91
        if( $isPlain ){
92
            return 'plain';
93
        }
94
        elseif( $isApi ){
95
            return 'api';
96
        }
97
        else{
98
            return 'web';
99
        }
100
    }
101
}
102