Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
75.00% |
12 / 16 |
|
50.00% |
3 / 6 |
CRAP | |
0.00% |
0 / 1 |
| NotificationManager | |
75.00% |
12 / 16 |
|
50.00% |
3 / 6 |
12.89 | |
0.00% |
0 / 1 |
| driver | |
66.67% |
4 / 6 |
|
0.00% |
0 / 1 |
3.33 | |||
| has | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| keys | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isOn | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
4.07 | |||
| config | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| send | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Services\Notification; |
| 4 | |
| 5 | use App\Services\Notification\Contracts\NotificationChannelContract; |
| 6 | |
| 7 | /** |
| 8 | * Resolves channels from config/notification_channels.php and their saved |
| 9 | * state from the `notification_channels` settings group — the exact same |
| 10 | * shape as App\Services\Payment\PaymentManager: |
| 11 | * notification_channels.<key>.enabled bool |
| 12 | * notification_channels.<key>.config array |
| 13 | */ |
| 14 | class NotificationManager |
| 15 | { |
| 16 | /** @var array<string, NotificationChannelContract> */ |
| 17 | private array $resolved = []; |
| 18 | |
| 19 | public function driver(string $key): NotificationChannelContract |
| 20 | { |
| 21 | if (isset($this->resolved[$key])) { |
| 22 | return $this->resolved[$key]; |
| 23 | } |
| 24 | |
| 25 | $entry = config("notification_channels.{$key}"); |
| 26 | |
| 27 | if (! $entry) { |
| 28 | throw new \InvalidArgumentException("Unknown notification channel \"{$key}\"."); |
| 29 | } |
| 30 | |
| 31 | return $this->resolved[$key] = app($entry['driver']); |
| 32 | } |
| 33 | |
| 34 | public function has(string $key): bool |
| 35 | { |
| 36 | return config("notification_channels.{$key}") !== null; |
| 37 | } |
| 38 | |
| 39 | /** @return string[] */ |
| 40 | public function keys(): array |
| 41 | { |
| 42 | return array_keys(config('notification_channels', [])); |
| 43 | } |
| 44 | |
| 45 | public function isOn(string $key): bool |
| 46 | { |
| 47 | $entry = config("notification_channels.{$key}"); |
| 48 | |
| 49 | if (! $entry) { |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | if ($entry['feature'] && ! feature($entry['feature'])) { |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | return (bool) settings("notification_channels.{$key}.enabled", $entry['default_enabled'] ?? false); |
| 58 | } |
| 59 | |
| 60 | /** @return array<string, mixed> */ |
| 61 | public function config(string $key): array |
| 62 | { |
| 63 | return (array) settings("notification_channels.{$key}.config", []); |
| 64 | } |
| 65 | |
| 66 | /** @return array{ok: bool, response: string} */ |
| 67 | public function send(string $key, string $to, string $subject, string $body): array |
| 68 | { |
| 69 | return $this->driver($key)->send($to, $subject, $body, $this->config($key)); |
| 70 | } |
| 71 | } |