From ee26b9551504b1f4a8668ea50c63b13fea059a55 Mon Sep 17 00:00:00 2001 From: Choraimy Kroonstuiver Date: Thu, 3 Oct 2019 20:21:26 +0200 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=92=A5=20Add=20UnknownDeviceException?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Exceptions/PushwooshException.php | 35 +++++++++++++++ src/Exceptions/UnknownDeviceException.php | 40 +++++++++++++++++ src/Pushwoosh.php | 4 +- src/PushwooshException.php | 52 ----------------------- tests/Unit/PushwooshTest.php | 7 +-- 5 files changed, 82 insertions(+), 56 deletions(-) create mode 100644 src/Exceptions/PushwooshException.php create mode 100644 src/Exceptions/UnknownDeviceException.php delete mode 100644 src/PushwooshException.php diff --git a/src/Exceptions/PushwooshException.php b/src/Exceptions/PushwooshException.php new file mode 100644 index 0000000..f7a31a0 --- /dev/null +++ b/src/Exceptions/PushwooshException.php @@ -0,0 +1,35 @@ +status_message), 0, $previous); + } + + /** + * Create a new exception for failed transmission. + * + * @param \Throwable|null $previous + * @return \NotificationChannels\Pushwoosh\Exceptions\PushwooshException + */ + public static function failedTransmission(Throwable $previous = null) + { + return new static('Failed to create message(s)', 0, $previous); + } +} diff --git a/src/Exceptions/UnknownDeviceException.php b/src/Exceptions/UnknownDeviceException.php new file mode 100644 index 0000000..618401f --- /dev/null +++ b/src/Exceptions/UnknownDeviceException.php @@ -0,0 +1,40 @@ +devices = (array)$devices; + + parent::__construct( + sprintf('Unknown device(s) referenced: %s', implode(', ', Arr::flatten($this->devices))), + $code, + $previous + ); + } + + /** + * Get the unknown devices per message. + * + * @return string[][] + */ + public function getDevices() + { + return $this->devices; + } +} diff --git a/src/Pushwoosh.php b/src/Pushwoosh.php index 9633515..292d142 100644 --- a/src/Pushwoosh.php +++ b/src/Pushwoosh.php @@ -5,6 +5,8 @@ use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Psr7\Request; +use NotificationChannels\Pushwoosh\Exceptions\PushwooshException; +use NotificationChannels\Pushwoosh\Exceptions\UnknownDeviceException; class Pushwoosh { @@ -52,7 +54,7 @@ public function createMessage(PushwooshPendingMessage $message) } if (isset($response->response->UnknownDevices)) { - throw PushwooshException::unknownDevices($response); + throw new UnknownDeviceException($response->response->UnknownDevices); } $message->wasSent(); diff --git a/src/PushwooshException.php b/src/PushwooshException.php deleted file mode 100644 index e33f46b..0000000 --- a/src/PushwooshException.php +++ /dev/null @@ -1,52 +0,0 @@ -status_message), 0, $previous); - } - - /** - * Create a new exception for failed transmission. - * - * @param \Throwable|null $previous - * @return \NotificationChannels\Pushwoosh\PushwooshException - */ - public static function failedTransmission(Throwable $previous = null) - { - return new static('Failed to create message(s)', 0, $previous); - } - - /** - * Create a new exception for unknown devices. - * - * @param object $payload - * @param \Throwable|null $previous - * @return \NotificationChannels\Pushwoosh\PushwooshException - */ - public static function unknownDevices($payload, Throwable $previous = null) - { - $devices = Collection::make($payload->response->UnknownDevices)->reduce(function ($carry, $devices) { - return array_merge((array)$carry, $devices); - }, []); - - return new static(sprintf('Unknown device(s) mentioned: %s', implode(', ', $devices)), 0, $previous); - } -} diff --git a/tests/Unit/PushwooshTest.php b/tests/Unit/PushwooshTest.php index 0cb8ed2..e952da6 100644 --- a/tests/Unit/PushwooshTest.php +++ b/tests/Unit/PushwooshTest.php @@ -6,8 +6,9 @@ use GuzzleHttp\Psr7\Response; use Illuminate\Support\Str; use Mockery; +use NotificationChannels\Pushwoosh\Exceptions\PushwooshException; +use NotificationChannels\Pushwoosh\Exceptions\UnknownDeviceException; use NotificationChannels\Pushwoosh\Pushwoosh; -use NotificationChannels\Pushwoosh\PushwooshException; use NotificationChannels\Pushwoosh\PushwooshPendingMessage; use PHPUnit\Framework\TestCase; @@ -80,8 +81,8 @@ public function testUnknownDevices() new Response(200, [], file_get_contents(__DIR__ . '/../Fixtures/unknown-devices.json')) ); - $this->expectException(PushwooshException::class); - $this->expectExceptionMessage('Unknown device(s) mentioned: foo, bar'); + $this->expectException(UnknownDeviceException::class); + $this->expectExceptionMessage('Unknown device(s) referenced: foo, bar'); $this->pushwoosh->createMessage( new PushwooshPendingMessage($this->pushwoosh) From c976836d5ce79814d619535ac48c601e89f42377 Mon Sep 17 00:00:00 2001 From: Choraimy Kroonstuiver Date: Thu, 3 Oct 2019 20:24:46 +0200 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=94=A7=20Simplify=20PushwooshExceptio?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Exceptions/PushwooshException.php | 24 +----------------------- src/Pushwoosh.php | 4 ++-- tests/Unit/PushwooshTest.php | 2 +- 3 files changed, 4 insertions(+), 26 deletions(-) diff --git a/src/Exceptions/PushwooshException.php b/src/Exceptions/PushwooshException.php index f7a31a0..5b912fe 100644 --- a/src/Exceptions/PushwooshException.php +++ b/src/Exceptions/PushwooshException.php @@ -3,33 +3,11 @@ namespace NotificationChannels\Pushwoosh\Exceptions; use RuntimeException; -use Throwable; /** * Exception thrown following communication failure with the Pushwoosh API. */ class PushwooshException extends RuntimeException { - /** - * Create a new exception for an API error. - * - * @param object $payload - * @param \Throwable|null $previous - * @return \NotificationChannels\Pushwoosh\Exceptions\PushwooshException - */ - public static function apiError($payload, Throwable $previous = null) - { - return new static(sprintf('Pushwoosh API error: %s', $payload->status_message), 0, $previous); - } - - /** - * Create a new exception for failed transmission. - * - * @param \Throwable|null $previous - * @return \NotificationChannels\Pushwoosh\Exceptions\PushwooshException - */ - public static function failedTransmission(Throwable $previous = null) - { - return new static('Failed to create message(s)', 0, $previous); - } + // } diff --git a/src/Pushwoosh.php b/src/Pushwoosh.php index 292d142..6505265 100644 --- a/src/Pushwoosh.php +++ b/src/Pushwoosh.php @@ -44,13 +44,13 @@ public function createMessage(PushwooshPendingMessage $message) try { $response = $this->client->send($request); } catch (GuzzleException $exception) { - throw PushwooshException::failedTransmission($exception); + throw new PushwooshException('Failed to create message(s)', 0, $exception); } $response = \GuzzleHttp\json_decode($response->getBody()->getContents()); if (isset($response->status_code) && $response->status_code !== 200) { - throw PushwooshException::apiError($response); + throw new PushwooshException($response->status_message); } if (isset($response->response->UnknownDevices)) { diff --git a/tests/Unit/PushwooshTest.php b/tests/Unit/PushwooshTest.php index e952da6..db282c4 100644 --- a/tests/Unit/PushwooshTest.php +++ b/tests/Unit/PushwooshTest.php @@ -45,7 +45,7 @@ public function testApiError() ); $this->expectException(PushwooshException::class); - $this->expectExceptionMessage('Pushwoosh API error: Access denied or application not found'); + $this->expectExceptionMessage('Access denied or application not found'); $this->pushwoosh->createMessage( new PushwooshPendingMessage($this->pushwoosh)