Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 8 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| NotifyUser | |
0.00% |
0 / 8 |
|
0.00% |
0 / 2 |
20 | |
0.00% |
0 / 1 |
| notify | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| admins | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\Shared\Services; |
| 4 | |
| 5 | use App\Models\User; |
| 6 | use Domain\Shared\Enums\SectorEnum; |
| 7 | use Domain\User\Enums\AccessLevel; |
| 8 | use Illuminate\Notifications\Notification; |
| 9 | |
| 10 | class NotifyUser |
| 11 | { |
| 12 | public function notify(Notification $notification, AccessLevel $accessLevel = null, SectorEnum $sectorEnum = null): void |
| 13 | { |
| 14 | $query = User::query(); |
| 15 | |
| 16 | if ($sectorEnum && $accessLevel) { |
| 17 | $query->whereRaw("EXISTS ( |
| 18 | SELECT 1 |
| 19 | FROM jsonb_each(users.access_level) AS s(key, value) |
| 20 | JOIN jsonb_array_elements_text(value) AS level ON TRUE |
| 21 | WHERE level::int = ? |
| 22 | AND key::int = ? |
| 23 | )", [$accessLevel->value, $sectorEnum->value]); |
| 24 | } |
| 25 | |
| 26 | $query->get()->each(function (User $admin) use ($notification) { |
| 27 | $admin->notify($notification); |
| 28 | }); |
| 29 | } |
| 30 | |
| 31 | public static function admins(Notification $notification, SectorEnum $sectorEnum = null): void |
| 32 | { |
| 33 | (new self())->notify($notification, AccessLevel::ADMIN, $sectorEnum); |
| 34 | } |
| 35 | } |