cerbero90 /
notifiable-exception
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Cerbero\NotifiableException\Notifications; |
||
| 4 | |||
| 5 | use BadMethodCallException; |
||
| 6 | use Cerbero\NotifiableException\Notifiable; |
||
| 7 | use Illuminate\Bus\Queueable; |
||
| 8 | use Illuminate\Notifications\Notification; |
||
| 9 | use Illuminate\Contracts\Queue\ShouldQueue; |
||
| 10 | use RuntimeException; |
||
| 11 | |||
| 12 | class ErrorOccurred extends Notification implements ShouldQueue |
||
| 13 | { |
||
| 14 | use Queueable; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * The notification messages keyed by the channel alias. |
||
| 18 | * |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | protected $messages; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * The custom channel class names keyed by the channel alias. |
||
| 25 | * |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | protected $customChannels; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Set the dependencies. |
||
| 32 | * |
||
| 33 | * @param \Cerbero\NotifiableException\Notifiable $exception |
||
| 34 | */ |
||
| 35 | 24 | public function __construct(Notifiable $exception) |
|
| 36 | { |
||
| 37 | 24 | $this->messages = $exception->getMessages(); |
|
| 38 | 24 | $this->customChannels = $exception->getCustomChannels(); |
|
| 39 | 24 | } |
|
| 40 | |||
| 41 | /** |
||
| 42 | * Get the notification's delivery channels. |
||
| 43 | * |
||
| 44 | * @param mixed $notifiable |
||
| 45 | * @return array |
||
| 46 | */ |
||
| 47 | 3 | public function via($notifiable) |
|
|
0 ignored issues
–
show
|
|||
| 48 | { |
||
| 49 | 3 | $channels = []; |
|
| 50 | 3 | $aliases = array_keys($this->messages); |
|
| 51 | |||
| 52 | // if there is a custom channel associated with the channel alias |
||
| 53 | // add custom channel to delivery channels, otherwise add the alias directly |
||
| 54 | 3 | foreach ($aliases as $alias) { |
|
| 55 | 3 | $channels[] = $this->customChannels[$alias] ?? $alias; |
|
| 56 | } |
||
| 57 | |||
| 58 | 3 | return $channels; |
|
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Retrieve the notification message dynamically |
||
| 63 | * |
||
| 64 | * @param string $name |
||
| 65 | * @param array $parameters |
||
| 66 | * @return mixed |
||
| 67 | * @throws \BadMethodCallException |
||
| 68 | * @throws \RuntimeException |
||
| 69 | */ |
||
| 70 | 9 | public function __call($name, $parameters) |
|
| 71 | { |
||
| 72 | 9 | if (substr($name, 0, 2) !== 'to') { |
|
| 73 | 3 | throw new BadMethodCallException(sprintf('Call to undefined method %s::%s()', static::class, $name)); |
|
| 74 | } |
||
| 75 | |||
| 76 | 6 | $channel = strtolower(substr($name, 2)); |
|
| 77 | |||
| 78 | 6 | if (isset($this->messages[$channel])) { |
|
| 79 | 3 | return $this->messages[$channel]; |
|
| 80 | } |
||
| 81 | |||
| 82 | 3 | throw new RuntimeException("No message to send to the channel [$channel]."); |
|
| 83 | } |
||
| 84 | } |
||
| 85 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.