領収証発行サービス
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.1KB

  1. <?php
  2. namespace App\Services\Slack;
  3. use Illuminate\Notifications\Notifiable;
  4. use App\Notifications\SlackNotification;
  5. use Illuminate\Support\Collection;
  6. class SlackService
  7. {
  8. use Notifiable;
  9. private bool $enable = false;
  10. /**
  11. * @var Collection<int,SlackNotification>
  12. */
  13. private Collection $messages;
  14. public function __construct()
  15. {
  16. $this->messages = new Collection();
  17. $this->enable = config('slack.enable', false);
  18. }
  19. public function send($message = null)
  20. {
  21. if ($this->enable) {
  22. $this->messages->push(new SlackNotification($message));
  23. }
  24. }
  25. public function hasMessage(): bool
  26. {
  27. return $this->messages->isNotEmpty();
  28. }
  29. public function commit()
  30. {
  31. if (!$this->hasMessage()) return;
  32. foreach ($this->messages as $message) {
  33. $this->notify($message);
  34. logger('Slack通知登録');
  35. }
  36. $this->messages->empty();
  37. }
  38. public function routeNotificationForSlack()
  39. {
  40. return config('slack.channel');
  41. }
  42. }