From 34cc882b635cff644ef0d850f9716d3c652e8bd8 Mon Sep 17 00:00:00 2001 From: Alex Plekhanov Date: Thu, 9 Dec 2021 20:34:39 +0100 Subject: [PATCH 1/3] refactor: started providing previous exceptions where possible --- src/Exceptions/CouldNotSendNotification.php | 9 +++++---- src/Pushbullet.php | 4 ++-- tests/Exceptions/CouldNotSendNotificationTest.php | 10 ++++++++-- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/Exceptions/CouldNotSendNotification.php b/src/Exceptions/CouldNotSendNotification.php index 27d8e0e..814f9c9 100644 --- a/src/Exceptions/CouldNotSendNotification.php +++ b/src/Exceptions/CouldNotSendNotification.php @@ -6,16 +6,17 @@ use Psr\Http\Message\ResponseInterface; use RuntimeException; +use Throwable; class CouldNotSendNotification extends RuntimeException { - public static function pushbulletRespondedWithAnError(ResponseInterface $response): self + public static function pushbulletRespondedWithAnError(ResponseInterface $response, Throwable $previous = null): self { $code = $response->getStatusCode(); $message = $response->getBody(); - return new self("Pushbullet responded with error: `{$code} - {$message}`."); + return new self("Pushbullet responded with error: `{$code} - {$message}`.", 0, $previous); } public static function providedEmailIsInvalid(string $email): self @@ -23,8 +24,8 @@ public static function providedEmailIsInvalid(string $email): self return new self("Provided email `{$email}` of `notifiable` is not valid."); } - public static function couldNotCommunicateWithPushbullet(): self + public static function couldNotCommunicateWithPushbullet(Throwable $previous = null): self { - return new self('Could not connect to Pushbullet API.'); + return new self('Could not connect to Pushbullet API.', 0, $previous); } } diff --git a/src/Pushbullet.php b/src/Pushbullet.php index 5f2ac09..98b1ec6 100644 --- a/src/Pushbullet.php +++ b/src/Pushbullet.php @@ -68,9 +68,9 @@ public function send($params): ResponseInterface 'headers' => $this->getHeaders(), ]); } catch (ClientException $exception) { - throw CouldNotSendNotification::pushbulletRespondedWithAnError($exception->getResponse()); + throw CouldNotSendNotification::pushbulletRespondedWithAnError($exception->getResponse(), $exception); } catch (Exception $exception) { - throw CouldNotSendNotification::couldNotCommunicateWithPushbullet(); + throw CouldNotSendNotification::couldNotCommunicateWithPushbullet($exception); } } } diff --git a/tests/Exceptions/CouldNotSendNotificationTest.php b/tests/Exceptions/CouldNotSendNotificationTest.php index 5fff4aa..cc656a1 100644 --- a/tests/Exceptions/CouldNotSendNotificationTest.php +++ b/tests/Exceptions/CouldNotSendNotificationTest.php @@ -4,6 +4,7 @@ namespace NotificationChannels\Pushbullet\Test\Exceptions; +use Exception; use NotificationChannels\Pushbullet\Exceptions\CouldNotSendNotification; use PHPUnit\Framework\TestCase; use Psr\Http\Message\ResponseInterface; @@ -24,12 +25,14 @@ public function invalid_email_exception_can_be_created(): void /** @test */ public function pushbullet_connection_exception_can_be_created(): void { - $exception = CouldNotSendNotification::couldNotCommunicateWithPushbullet(); + $previousException = new Exception(); + $exception = CouldNotSendNotification::couldNotCommunicateWithPushbullet($previousException); $this->assertEquals( 'Could not connect to Pushbullet API.', $exception->getMessage() ); + $this->assertSame($previousException, $exception->getPrevious()); } /** @test */ @@ -45,11 +48,14 @@ public function pushbullet_error_exception_can_be_created(): void ->method('getBody') ->willReturn('Oops'); - $exception = CouldNotSendNotification::pushbulletRespondedWithAnError($response); + $previousException = new Exception(); + + $exception = CouldNotSendNotification::pushbulletRespondedWithAnError($response, $previousException); $this->assertEquals( 'Pushbullet responded with error: `400 - Oops`.', $exception->getMessage() ); + $this->assertSame($previousException, $exception->getPrevious()); } } From b44311a60e917ce33302c4e7685e5c837e8aa890 Mon Sep 17 00:00:00 2001 From: Alex Plekhanov Date: Thu, 9 Dec 2021 20:42:52 +0100 Subject: [PATCH 2/3] docs: improved PHPDocs --- src/PushbulletChannel.php | 3 ++- src/PushbulletMessage.php | 4 ++-- src/Targets/Channel.php | 2 +- src/Targets/Device.php | 2 +- src/Targets/Email.php | 2 +- 5 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/PushbulletChannel.php b/src/PushbulletChannel.php index d9bfd66..a21e303 100644 --- a/src/PushbulletChannel.php +++ b/src/PushbulletChannel.php @@ -44,7 +44,8 @@ public function send($notifiable, Notification $notification): void /** * @param $notifiable - * @return \NotificationChannels\Pushbullet\Targets\Targetable|void + * + * @return \NotificationChannels\Pushbullet\Targets\Targetable|null */ private function getTarget($notifiable): ?Targetable { diff --git a/src/PushbulletMessage.php b/src/PushbulletMessage.php index 9db6b26..72aae54 100644 --- a/src/PushbulletMessage.php +++ b/src/PushbulletMessage.php @@ -136,7 +136,7 @@ public function url($url): self /** * Get array representation of message for Pushbullet client. * - * @return array + * @return array{type: string, title: string, body: string, url?: string, channel_tag?: string, device_iden?: string, email?: string} */ public function toArray(): array { @@ -162,7 +162,7 @@ private function isLink(): bool } /** - * @return array + * @return array{url?: string} */ private function getUrlParameter(): array { diff --git a/src/Targets/Channel.php b/src/Targets/Channel.php index 37afbd5..514e1e8 100644 --- a/src/Targets/Channel.php +++ b/src/Targets/Channel.php @@ -24,7 +24,7 @@ public function __construct($channelTag) } /** - * {@inheritdoc} + * @return array{channel_tag: string} */ public function getTarget(): array { diff --git a/src/Targets/Device.php b/src/Targets/Device.php index 8d589f0..cc36aee 100644 --- a/src/Targets/Device.php +++ b/src/Targets/Device.php @@ -24,7 +24,7 @@ public function __construct($device) } /** - * {@inheritdoc} + * @return array{device_iden: string} */ public function getTarget(): array { diff --git a/src/Targets/Email.php b/src/Targets/Email.php index 085352a..5b925e8 100644 --- a/src/Targets/Email.php +++ b/src/Targets/Email.php @@ -30,7 +30,7 @@ public function __construct($email) } /** - * {@inheritdoc} + * @return array{email: string} */ public function getTarget(): array { From 4777f1d6864648544d87e70691c971837c57b07f Mon Sep 17 00:00:00 2001 From: Alex Plekhanov Date: Thu, 9 Dec 2021 19:43:07 +0000 Subject: [PATCH 3/3] Apply fixes from StyleCI [ci skip] [skip ci] --- src/PushbulletChannel.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/PushbulletChannel.php b/src/PushbulletChannel.php index a21e303..a2ed1ac 100644 --- a/src/PushbulletChannel.php +++ b/src/PushbulletChannel.php @@ -44,7 +44,6 @@ public function send($notifiable, Notification $notification): void /** * @param $notifiable - * * @return \NotificationChannels\Pushbullet\Targets\Targetable|null */ private function getTarget($notifiable): ?Targetable