| Conditions | 7 | 
| Paths | 36 | 
| Total Lines | 38 | 
| Code Lines | 15 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 0 | ||
| 1 | <?php namespace Anomaly\Streams\Platform\Model\Command; | ||
| 37 | public function handle() | ||
| 38 |     { | ||
| 39 | $action = $this->model->isForceDeleting() ? 'forceDelete' : 'delete'; | ||
| 40 | |||
| 41 | /** | ||
| 42 | * If the model itself can not be trashed | ||
| 43 | * then we have no reason to keep any | ||
| 44 | * relations that cascade. | ||
| 45 | */ | ||
| 46 |         if (!method_exists($this->model, 'restore')) { | ||
| 47 | $action = 'forceDelete'; | ||
| 48 | } | ||
| 49 | |||
| 50 |         foreach ($this->model->getCascades() as $relation) { | ||
| 51 | |||
| 52 | /* @var Relation $relation */ | ||
| 53 |             $relation = $this->model->{$relation}(); | ||
| 54 | |||
| 55 |             if ($action == 'forceDelete') { | ||
| 56 | $relation = $relation->withTrashed(); | ||
| 57 | } | ||
| 58 | |||
| 59 | $relation = $relation->getResults(); | ||
| 60 | |||
| 61 |             if ($relation instanceof EloquentModel) { | ||
| 62 |                 $relation->{$action}(); | ||
| 63 | } | ||
| 64 | |||
| 65 |             if ($relation instanceof EloquentCollection) { | ||
| 66 | |||
| 67 | $relation->each( | ||
| 68 |                     function (EloquentModel $item) use ($action) { | ||
| 69 |                         $item->{$action}(); | ||
| 70 | } | ||
| 71 | ); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | } | ||
| 75 | } | ||
| 76 |