Passed
Push — feature/lg ( 3c06af...b2e7fb )
by Richard
03:07
created

RollbackGeneratorCommand::handle()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 75
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 47
c 2
b 0
f 0
dl 0
loc 75
rs 8.8452
cc 5
nc 10
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PWWEB\Artomator\Commands;
4
5
use Illuminate\Console\Command;
6
use InfyOm\Generator\Generators\API\APIControllerGenerator;
7
use InfyOm\Generator\Generators\API\APIRequestGenerator;
8
use InfyOm\Generator\Generators\API\APIRoutesGenerator;
9
use InfyOm\Generator\Generators\API\APITestGenerator;
10
use InfyOm\Generator\Generators\MigrationGenerator;
11
use InfyOm\Generator\Generators\ModelGenerator;
12
use InfyOm\Generator\Generators\RepositoryGenerator;
13
use InfyOm\Generator\Generators\RepositoryTestGenerator;
14
use InfyOm\Generator\Generators\Scaffold\ControllerGenerator;
15
use InfyOm\Generator\Generators\Scaffold\MenuGenerator;
16
use InfyOm\Generator\Generators\Scaffold\RequestGenerator;
17
use InfyOm\Generator\Generators\Scaffold\RoutesGenerator;
18
use InfyOm\Generator\Generators\Scaffold\ViewGenerator;
19
use PWWEB\Artomator\Common\CommandData;
20
use Symfony\Component\Console\Input\InputArgument;
21
use Symfony\Component\Console\Input\InputOption;
22
23
class RollbackGeneratorCommand extends Command
24
{
25
    /**
26
     * The command Data.
27
     *
28
     * @var CommandData
29
     */
30
    public $commandData;
31
    /**
32
     * The console command name.
33
     *
34
     * @var string
35
     */
36
    protected $name = 'artomator:rollback';
37
    /**
38
     * The console command description.
39
     *
40
     * @var string
41
     */
42
    protected $description = 'Rollback a full CRUD API and Scaffold for given model';
43
44
    /**
45
     * @var Composer
0 ignored issues
show
Bug introduced by
The type PWWEB\Artomator\Commands\Composer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
46
     */
47
    public $composer;
48
49
    /**
50
     * Create a new command instance.
51
     */
52
    public function __construct()
53
    {
54
        parent::__construct();
55
56
        $this->composer = app()['composer'];
0 ignored issues
show
Documentation Bug introduced by
It seems like app()['composer'] of type Illuminate\Support\Composer is incompatible with the declared type PWWEB\Artomator\Commands\Composer of property $composer.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
57
    }
58
59
    /**
60
     * Execute the command.
61
     *
62
     * @return void
63
     */
64
    public function handle()
65
    {
66
        if (! in_array($this->argument('type'), [
67
            CommandData::$COMMAND_TYPE_API,
68
            CommandData::$COMMAND_TYPE_SCAFFOLD,
69
            CommandData::$COMMAND_TYPE_API_SCAFFOLD,
70
            CommandData::$COMMAND_TYPE_GRAPHQL,
71
        ])) {
72
            $this->error('invalid rollback type');
73
        }
74
75
        $this->commandData = new CommandData($this, $this->argument('type'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('type') can also be of type string[]; however, parameter $commandType of PWWEB\Artomator\Common\CommandData::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
        $this->commandData = new CommandData($this, /** @scrutinizer ignore-type */ $this->argument('type'));
Loading history...
76
        $this->commandData->config->mName = $this->commandData->modelName = $this->argument('model');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->argument('model') can also be of type string[]. However, the property $modelName is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
77
78
        $this->commandData->config->init($this->commandData, ['tableName', 'prefix', 'plural', 'views']);
79
80
        $views = $this->commandData->getOption('views');
81
        if (! empty($views)) {
82
            $views = explode(',', $views);
83
            $viewGenerator = new ViewGenerator($this->commandData);
84
            $viewGenerator->rollback($views);
85
86
            $this->info('Generating autoload files');
87
            $this->composer->dumpOptimized();
88
89
            return;
90
        }
91
92
        $migrationGenerator = new MigrationGenerator($this->commandData);
93
        $migrationGenerator->rollback();
94
95
        $modelGenerator = new ModelGenerator($this->commandData);
96
        $modelGenerator->rollback();
97
98
        $repositoryGenerator = new RepositoryGenerator($this->commandData);
99
        $repositoryGenerator->rollback();
100
101
        $requestGenerator = new APIRequestGenerator($this->commandData);
102
        $requestGenerator->rollback();
103
104
        $controllerGenerator = new APIControllerGenerator($this->commandData);
105
        $controllerGenerator->rollback();
106
107
        $routesGenerator = new APIRoutesGenerator($this->commandData);
108
        $routesGenerator->rollback();
109
110
        $requestGenerator = new RequestGenerator($this->commandData);
111
        $requestGenerator->rollback();
112
113
        $controllerGenerator = new ControllerGenerator($this->commandData);
114
        $controllerGenerator->rollback();
115
116
        $viewGenerator = new ViewGenerator($this->commandData);
117
        $viewGenerator->rollback();
118
119
        $routeGenerator = new RoutesGenerator($this->commandData);
120
        $routeGenerator->rollback();
121
122
        if ($this->commandData->getAddOn('tests')) {
123
            $repositoryTestGenerator = new RepositoryTestGenerator($this->commandData);
124
            $repositoryTestGenerator->rollback();
125
126
            $apiTestGenerator = new APITestGenerator($this->commandData);
127
            $apiTestGenerator->rollback();
128
        }
129
130
        if ($this->commandData->config->getAddOn('menu.enabled')) {
131
            $menuGenerator = new MenuGenerator($this->commandData);
132
            $menuGenerator->rollback();
133
        }
134
135
        // TODO: Add rollback for the graphql files too.
136
137
        $this->info('Generating autoload files');
138
        $this->composer->dumpOptimized();
139
    }
140
141
    /**
142
     * Get the console command options.
143
     *
144
     * @return array
145
     */
146
    public function getOptions()
147
    {
148
        return [
149
            ['tableName', null, InputOption::VALUE_REQUIRED, 'Table Name'],
150
            ['prefix', null, InputOption::VALUE_REQUIRED, 'Prefix for all files'],
151
            ['plural', null, InputOption::VALUE_REQUIRED, 'Plural Model name'],
152
            ['views', null, InputOption::VALUE_REQUIRED, 'Views to rollback'],
153
        ];
154
    }
155
156
    /**
157
     * Get the console command arguments.
158
     *
159
     * @return array
160
     */
161
    protected function getArguments()
162
    {
163
        return [
164
            ['model', InputArgument::REQUIRED, 'Singular Model name'],
165
            ['type', InputArgument::REQUIRED, 'Rollback type: (api / scaffold / api_scaffold)'],
166
        ];
167
    }
168
}
169