Conditions | 7 |
Paths | 4 |
Total Lines | 19 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
20 | public function handle(Entity $entity): void |
||
21 | { |
||
22 | // We will initially check if the model is using soft deletes. If so, |
||
23 | // the attribute values will remain untouched as they should sill |
||
24 | // be available till the entity is truly deleted from database. |
||
25 | if (in_array(SoftDeletes::class, class_uses_recursive(get_class($entity))) && ! $entity->isForceDeleting()) { |
||
26 | return; |
||
27 | } |
||
28 | |||
29 | foreach ($entity->getEntityAttributes() as $attribute) { |
||
30 | if ($entity->relationLoaded($relation = $attribute->getAttribute('slug')) |
||
31 | && ($values = $entity->getRelationValue($relation)) && ! $values->isEmpty()) { |
||
32 | // Calling the `destroy` method from the given $type model class name |
||
33 | // will finally delete the records from database if any was found. |
||
34 | // We'll just provide an array containing the ids to be deleted. |
||
35 | forward_static_call_array([Attribute::getTypeModel($attribute->getAttribute('type')), 'destroy'], [$values->pluck('id')->toArray()]); |
||
36 | } |
||
37 | } |
||
38 | } |
||
39 | } |
||
40 |