Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
8 / 8 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| AddressRestoreService | |
100.00% |
8 / 8 |
|
100.00% |
2 / 2 |
6 | |
100.00% |
1 / 1 |
| restore | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| restoreChildren | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\Address\Services; |
| 4 | |
| 5 | use App\Events\ServiceProcessed; |
| 6 | use App\Models\Address\Address; |
| 7 | use App\Models\User; |
| 8 | use Exception; |
| 9 | |
| 10 | readonly class AddressRestoreService |
| 11 | { |
| 12 | /** |
| 13 | * @param Address $address |
| 14 | * @param User $user |
| 15 | * @return void |
| 16 | * @throws Exception |
| 17 | */ |
| 18 | public function restore(Address $address, User $user, bool $restoreChildren): void |
| 19 | { |
| 20 | $address->restore(); |
| 21 | |
| 22 | if($address->childrenWithTrashed && $restoreChildren){ |
| 23 | $this->restoreChildren($address); |
| 24 | } |
| 25 | |
| 26 | ServiceProcessed::dispatch(self::class, $user); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * @param $address |
| 31 | */ |
| 32 | private function restoreChildren($address) |
| 33 | { |
| 34 | foreach ($address->childrenWithTrashed as $child) { |
| 35 | $child->restore(); |
| 36 | |
| 37 | if($child->childrenWithTrashed){ |
| 38 | $this->restoreChildren($child); |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | } |