Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved PHPDocs, started providing previous exceptions where possible #58

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/Exceptions/CouldNotSendNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,26 @@

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
{
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);
}
}
4 changes: 2 additions & 2 deletions src/Pushbullet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
2 changes: 1 addition & 1 deletion src/PushbulletChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ 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
{
Expand Down
4 changes: 2 additions & 2 deletions src/PushbulletMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -162,7 +162,7 @@ private function isLink(): bool
}

/**
* @return array
* @return array{url?: string}
*/
private function getUrlParameter(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Targets/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct($channelTag)
}

/**
* {@inheritdoc}
* @return array{channel_tag: string}
*/
public function getTarget(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Targets/Device.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct($device)
}

/**
* {@inheritdoc}
* @return array{device_iden: string}
*/
public function getTarget(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Targets/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct($email)
}

/**
* {@inheritdoc}
* @return array{email: string}
*/
public function getTarget(): array
{
Expand Down
10 changes: 8 additions & 2 deletions tests/Exceptions/CouldNotSendNotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 */
Expand All @@ -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());
}
}