|
- <?php
-
- namespace App\Services\Slack;
-
- use Illuminate\Notifications\Notifiable;
- use App\Notifications\SlackNotification;
- use Illuminate\Support\Collection;
-
- class SlackService
- {
- use Notifiable;
-
- private bool $enable = false;
-
- /**
- * @var Collection<int,SlackNotification>
- */
- private Collection $messages;
-
- public function __construct()
- {
- $this->messages = new Collection();
- $this->enable = config('slack.enable', false);
- }
-
- public function send($message = null)
- {
- if ($this->enable) {
- $this->messages->push(new SlackNotification($message));
- }
- }
-
- public function hasMessage(): bool
- {
- return $this->messages->isNotEmpty();
- }
-
- public function commit()
- {
- if (!$this->hasMessage()) return;
-
- foreach ($this->messages as $message) {
- $this->notify($message);
- logger('Slack通知登録');
- }
- $this->messages->empty();
- }
-
- public function routeNotificationForSlack()
- {
- return config('slack.channel');
- }
- }
|