| Conditions | 9 |
| Paths | 8 |
| Total Lines | 36 |
| Code Lines | 21 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 19 |
| CRAP Score | 9 |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 19 | 5 | public function handle() |
|
| 20 | { |
||
| 21 | 5 | $modelName = $this->argument('model'); |
|
| 22 | 5 | $timeoutInMinutes = (int)$this->option('timeout'); |
|
| 23 | 5 | $isForceDelete = (bool)$this->option('force'); |
|
| 24 | |||
| 25 | 5 | if (!is_subclass_of($modelName, Model::class, true)) { |
|
| 26 | 1 | $modelName = Relation::getMorphedModel($modelName) ?: $modelName; |
|
|
|
|||
| 27 | } |
||
| 28 | |||
| 29 | 5 | if (!is_subclass_of($modelName, Model::class, true)) { |
|
| 30 | 1 | $this->error("Model [{$modelName}] name should extends model class"); |
|
| 31 | |||
| 32 | 1 | return static::FAILURE; |
|
| 33 | } |
||
| 34 | |||
| 35 | 4 | if (!in_array(HasExpiration::class, class_uses_recursive($modelName))) { |
|
| 36 | 1 | $this->error("Model [{$modelName}] should use trait 'HasExpiration'"); |
|
| 37 | |||
| 38 | 1 | return static::FAILURE; |
|
| 39 | } |
||
| 40 | |||
| 41 | 3 | $query = $modelName::query()->willBeExpiredAt(Carbon::now()->addMinutes($timeoutInMinutes)); |
|
| 42 | |||
| 43 | 3 | if ($isForceDelete && in_array(SoftDeletes::class, class_uses_recursive($modelName))) { |
|
| 44 | do { |
||
| 45 | 1 | $deleted = $query->limit(100)->forceDelete(); |
|
| 46 | 1 | } while ($deleted > 0); |
|
| 47 | } else { |
||
| 48 | do { |
||
| 49 | 2 | $deleted = $query->limit(100)->delete(); |
|
| 50 | 2 | } while ($deleted > 0); |
|
| 51 | } |
||
| 52 | |||
| 53 | |||
| 54 | 3 | return static::SUCCESS; |
|
| 55 | } |
||
| 57 |