SetAutoIncrementCommand::handle()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 9.0488
c 0
b 0
f 0
cc 5
nc 6
nop 0
1
<?php
2
3
namespace Sausin\DBSetAutoIncrement\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Facades\Config;
8
use Sausin\DBSetAutoIncrement\DatabaseInfo;
9
use Sausin\DBSetAutoIncrement\GetAttribute;
10
use Sausin\DBSetAutoIncrement\UpdateAttribute;
11
12
class SetAutoIncrementCommand extends Command
13
{
14
    use DatabaseInfo;
15
    use GetAttribute;
16
    use UpdateAttribute;
17
18
    /**
19
     * The name and signature of the console command.
20
     *
21
     * @var string
22
     */
23
    protected $signature = 'db:set-auto-increment
24
                            {--tables=* : The table(s) for which auto increment should be set}
25
                            {--value= : The auto increment value to be set}';
26
27
    /**
28
     * The console command description.
29
     *
30
     * @var string
31
     */
32
    protected $description = 'Set auto increment for database table(s)';
33
34
    /** @var array */
35
    protected $skipTables;
36
37
    /** @var array */
38
    protected $onlyTables;
39
40
    /** @var string */
41
    protected $mode;
42
43
    /** @var string */
44
    protected $driver;
45
46
    /** @var int */
47
    protected $autoIncrement;
48
49
    /** @var array */
50
    protected $supportedDrivers = ['mysql', 'sqlite'];
51
52
    /**
53
     * The name of the queue the job should be sent to.
54
     *
55
     * @var string|null
56
     */
57
    public $queue = 'monitoring';
58
59
    /**
60
     * Create the event listener.
61
     *
62
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
63
     */
64
    public function __construct()
65
    {
66
        parent::__construct();
67
68
        $this->mode = Config::get('auto-increment.mode', 'skip');
69
        $this->skipTables = Config::get('auto-increment.skipTables', ['migrations']);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Illuminate\Support\Faca...', array('migrations')) of type * is incompatible with the declared type array of property $skipTables.

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...
70
        $this->onlyTables = Config::get('auto-increment.onlyTables', []);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Illuminate\Support\Faca...t.onlyTables', array()) of type * is incompatible with the declared type array of property $onlyTables.

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...
71
    }
72
73
    /**
74
     * Execute the console command.
75
     *
76
     * @return mixed
77
     */
78
    public function handle()
79
    {
80
        $this->autoIncrement = $this->option('value') ?? Config::get('auto-increment.autoIncrement', 100001);
81
82
        if (! $this->isDatabaseCompatible()) {
83
            $this->info("Database {$this->driver} not supported");
84
85
            return;
86
        }
87
88
        $driver = ucfirst($this->driver);
89
90
        if ($this->option('tables')) {
91
            $this->{"update{$driver}Tables"}(collect($this->option('tables')));
92
93
            $this->info('Specified tables have been updated');
94
95
            return;
96
        }
97
98
        $tables = collect([]);
99
100
        if ($this->mode === 'only') {
101
            $tables = collect($this->onlyTables);
102
        }
103
104
        if ($this->mode === 'skip') {
105
            $tables = collect($this->getTableList())->reject(function ($value) {
106
                return in_array($value, $this->skipTables, true);
107
            });
108
        }
109
110
        $this->{"update{$driver}Tables"}($tables);
111
        $this->info('Tables have been updated as per config');
112
    }
113
114
    /**
115
     * Update AUTO INCREMENT value in mysql tables.
116
     *
117
     * @param  Collection $tables
118
     * @return void
119
     */
120 View Code Duplication
    protected function updateMysqlTables(Collection $tables): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
    {
122
        $tables->filter(function ($table) {
123
            return $this->getAutoIncrement('Mysql', $table) < $this->autoIncrement;
124
        })->map(function ($table) {
125
            $this->updateAutoIncrement('Mysql', $table);
126
        });
127
    }
128
129
    /**
130
     * Update AUTO INCREMENT value in sqlite tables.
131
     *
132
     * @param  Collection $tables
133
     * @return void
134
     */
135 View Code Duplication
    protected function updateSqliteTables(Collection $tables): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
    {
137
        // the auto increment value is reduced by 1 as SQLITE uses it in this way
138
        $this->autoIncrement--;
139
140
        $tables->filter(function ($table) {
141
            return $this->getAutoIncrement('Sqlite', $table) < $this->autoIncrement;
142
        })->map(function ($table) {
143
            $this->updateAutoIncrement('Sqlite', $table);
144
        });
145
    }
146
}
147