From 7b1d14ebb5e5aa2f9aad03aaad629d012c914e92 Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Mon, 30 Oct 2023 23:47:52 +0700 Subject: [PATCH] fix: phpstan test errors --- src/Interfaces/Structures/AppInterface.php | 4 ++-- src/Structures/App.php | 2 +- src/Structures/Notification.php | 21 ++++++++++++++++++--- src/Trait/EventSettingTrait.php | 4 ++++ 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/Interfaces/Structures/AppInterface.php b/src/Interfaces/Structures/AppInterface.php index 83f3767..2bc4867 100644 --- a/src/Interfaces/Structures/AppInterface.php +++ b/src/Interfaces/Structures/AppInterface.php @@ -10,14 +10,14 @@ interface AppInterface /** * Send a message to telegram * - * @param string $message + * @param string|null $message * @param array $options * * @return void * @throws MessageIsEmptyException * @see App::sendMessage() */ - public function sendMessage(string $message = '', array $options = []): void; + public function sendMessage(?string $message = '', array $options = []): void; /** * Send a photo to telegram diff --git a/src/Structures/App.php b/src/Structures/App.php index 907df5d..f3465d3 100644 --- a/src/Structures/App.php +++ b/src/Structures/App.php @@ -27,7 +27,7 @@ private function createTelegramBaseContent(): array ]; } - public function sendMessage(string $message = '', array $options = []): void + public function sendMessage(?string $message = '', array $options = []): void { if (empty($message)) { throw MessageIsEmptyException::create(); diff --git a/src/Structures/Notification.php b/src/Structures/Notification.php index 2a04d26..9426436 100644 --- a/src/Structures/Notification.php +++ b/src/Structures/Notification.php @@ -4,6 +4,7 @@ use GuzzleHttp\Exception\GuzzleException; use LbilTech\TelegramGitNotifier\Constants\EventConstant; +use LbilTech\TelegramGitNotifier\Exceptions\MessageIsEmptyException; use LbilTech\TelegramGitNotifier\Exceptions\SendNotificationException; use Symfony\Component\HttpFoundation\Request; @@ -30,11 +31,18 @@ public function accessDenied( public function setPayload(Request $request, string $event) { + $content = null; + if ($this->event->platform === 'gitlab') { - $this->payload = json_decode($request->getContent()); + $content = $request->getContent(); } elseif ($this->event->platform === EventConstant::DEFAULT_PLATFORM) { - $this->payload = json_decode($request->request->get('payload')); + $content = $request->request->get('payload'); + } + + if (is_string($content)) { + $this->payload = json_decode($content); } + $this->setMessage($event); return $this->payload; @@ -46,6 +54,7 @@ public function setPayload(Request $request, string $event) * @param string $typeEvent * * @return void + * @throws MessageIsEmptyException */ private function setMessage(string $typeEvent): void { @@ -56,10 +65,16 @@ private function setMessage(string $typeEvent): void ? "events.{$this->event->platform}.{$event}.default" : "events.{$this->event->platform}.{$event}.{$action}"; - $this->message = view($viewTemplate, [ + $viewResult = view($viewTemplate, [ 'payload' => $this->payload, 'event' => tgn_convert_event_name($typeEvent), ]); + + if ($viewResult === null) { + throw MessageIsEmptyException::create(); + } + + $this->message = $viewResult; } public function sendNotify(string $message = null, array $options = []): bool diff --git a/src/Trait/EventSettingTrait.php b/src/Trait/EventSettingTrait.php index 6f5d925..51f98ad 100644 --- a/src/Trait/EventSettingTrait.php +++ b/src/Trait/EventSettingTrait.php @@ -157,6 +157,10 @@ public function sendSettingEventMessage( public function getEventFromCallback(?string $callback): string { + if (!$callback) { + return ''; + } + return str_replace([ EventConstant::EVENT_PREFIX, EventConstant::GITHUB_EVENT_SEPARATOR,