Skip to content

Commit

Permalink
Merge pull request #43 from laravel-notification-channels/improvements
Browse files Browse the repository at this point in the history
Code improvements
  • Loading branch information
alexsoft authored Mar 6, 2021
2 parents 318e534 + 01e102d commit 6f8676e
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 58 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ build
composer.phar
composer.lock
.phpunit.result.cache
build/
8 changes: 5 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
stopOnFailure="false">
<testsuites>
<testsuite name="Pushbullet Notification Channel Test Suite">
<directory>tests</directory>
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">src/</directory>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>

<logging>
<log type="tap" target="build/report.tap"/>
<log type="junit" target="build/report.junit.xml"/>
Expand Down
25 changes: 11 additions & 14 deletions src/Exceptions/CouldNotSendNotification.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
<?php

declare(strict_types=1);

namespace NotificationChannels\Pushbullet\Exceptions;

use GuzzleHttp\Exception\ClientException;
use Psr\Http\Message\ResponseInterface;
use RuntimeException;

class CouldNotSendNotification extends RuntimeException
{
public static function pushbulletRespondedWithAnError(ClientException $exception)
public static function pushbulletRespondedWithAnError(ResponseInterface $response): self
{
$code = $exception->getResponse()->getStatusCode();

$message = $exception->getResponse()->getBody();
$code = $response->getStatusCode();

return new static("Pushbullet responded with an error `{$code} - {$message}`");
}
$message = $response->getBody();

public static function providedEmailIsInvalid($email)
{
return new static("Provided email `{$email}` of `notifiable` is not valid");
return new self("Pushbullet responded with error: `{$code} - {$message}`.");
}

public static function couldNotSendNotificationWithoutRecipient()
public static function providedEmailIsInvalid(string $email): self
{
return new static('Neither device id nor email of recipient was not specified');
return new self("Provided email `{$email}` of `notifiable` is not valid.");
}

public static function couldNotCommunicateWithPushbullet()
public static function couldNotCommunicateWithPushbullet(): self
{
return new static("Couldn't connect to Pushbullet API.");
return new self('Could not connect to Pushbullet API.');
}
}
19 changes: 11 additions & 8 deletions src/Pushbullet.php
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
<?php

declare(strict_types=1);

namespace NotificationChannels\Pushbullet;

use Exception;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Exception\ClientException;
use NotificationChannels\Pushbullet\Exceptions\CouldNotSendNotification;
use Psr\Http\Message\ResponseInterface;

class Pushbullet
{
/** @var string */
protected $token;
private $token;

/** @var \GuzzleHttp\Client */
protected $httpClient;
private $httpClient;

/**
* Create small Pushbullet client.
*
* @param string $token
* @param \GuzzleHttp\Client $httpClient
*/
public function __construct($token, HttpClient $httpClient)
public function __construct(string $token, HttpClient $httpClient)
{
$this->token = $token;

$this->httpClient = $httpClient;
}

Expand All @@ -33,7 +35,7 @@ public function __construct($token, HttpClient $httpClient)
*
* @return string
*/
protected function getPushbulletUrl()
private function getPushbulletUrl(): string
{
return 'https://api.pushbullet.com/v2/pushes';
}
Expand All @@ -43,7 +45,7 @@ protected function getPushbulletUrl()
*
* @return array
*/
protected function getHeaders()
private function getHeaders(): array
{
return [
'Access-Token' => $this->token,
Expand All @@ -54,9 +56,10 @@ protected function getHeaders()
* Send request to Pushbullet API.
*
* @param array $params
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function send($params)
public function send($params): ResponseInterface
{
$url = $this->getPushbulletUrl();

Expand All @@ -66,7 +69,7 @@ public function send($params)
'headers' => $this->getHeaders(),
]);
} catch (ClientException $exception) {
throw CouldNotSendNotification::pushbulletRespondedWithAnError($exception);
throw CouldNotSendNotification::pushbulletRespondedWithAnError($exception->getResponse());
} catch (Exception $exception) {
throw CouldNotSendNotification::couldNotCommunicateWithPushbullet();
}
Expand Down
16 changes: 8 additions & 8 deletions src/PushbulletChannel.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace NotificationChannels\Pushbullet;

use Illuminate\Notifications\Notification;
Expand All @@ -9,13 +11,10 @@

class PushbulletChannel
{
/**
* @var \NotificationChannels\Pushbullet\Pushbullet
*/
protected $pushbullet;
/** @var \NotificationChannels\Pushbullet\Pushbullet */
private $pushbullet;

/**
* Create pushbullet notification channel.
* @param \NotificationChannels\Pushbullet\Pushbullet $pushbullet
*/
public function __construct(Pushbullet $pushbullet)
Expand All @@ -31,7 +30,7 @@ public function __construct(Pushbullet $pushbullet)
*
* @throws \NotificationChannels\Pushbullet\Exceptions\CouldNotSendNotification
*/
public function send($notifiable, Notification $notification)
public function send($notifiable, Notification $notification): void
{
if (! $target = $this->getTarget($notifiable)) {
return;
Expand All @@ -45,12 +44,13 @@ public function send($notifiable, Notification $notification)

/**
* @param $notifiable
*
* @return \NotificationChannels\Pushbullet\Targets\Targetable|void
*/
protected function getTarget($notifiable)
private function getTarget($notifiable): ?Targetable
{
if (! $target = $notifiable->routeNotificationFor('pushbullet')) {
return;
return null;
}

if ($target instanceof Targetable) {
Expand Down
32 changes: 17 additions & 15 deletions src/PushbulletMessage.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

declare(strict_types=1);

namespace NotificationChannels\Pushbullet;

use NotificationChannels\Pushbullet\Targets\Targetable;

class PushbulletMessage
{
const TYPE_NOTE = 'note';
const TYPE_LINK = 'link';
private const TYPE_NOTE = 'note';
private const TYPE_LINK = 'link';

/**
* Type of message (currently: note or link).
Expand Down Expand Up @@ -45,7 +47,7 @@ class PushbulletMessage
*
* @return static
*/
public static function create($message)
public static function create($message): self
{
return new static($message);
}
Expand All @@ -63,7 +65,7 @@ public function __construct($message)
*
* @return $this
*/
public function target(Targetable $targetable)
public function target(Targetable $targetable): self
{
$this->target = $targetable;

Expand All @@ -75,9 +77,9 @@ public function target(Targetable $targetable)
*
* @return $this
*/
public function note()
public function note(): self
{
$this->type = static::TYPE_NOTE;
$this->type = self::TYPE_NOTE;

return $this;
}
Expand All @@ -87,9 +89,9 @@ public function note()
*
* @return $this
*/
public function link()
public function link(): self
{
$this->type = static::TYPE_LINK;
$this->type = self::TYPE_LINK;

return $this;
}
Expand All @@ -101,7 +103,7 @@ public function link()
*
* @return $this
*/
public function title($title)
public function title($title): self
{
$this->title = $title;

Expand All @@ -115,7 +117,7 @@ public function title($title)
*
* @return $this
*/
public function message($message)
public function message($message): self
{
$this->message = $message;

Expand All @@ -129,7 +131,7 @@ public function message($message)
*
* @return $this
*/
public function url($url)
public function url($url): self
{
$this->url = $url;

Expand All @@ -141,7 +143,7 @@ public function url($url)
*
* @return array
*/
public function toArray()
public function toArray(): array
{
$payload = [
'type' => $this->type,
Expand All @@ -159,15 +161,15 @@ public function toArray()
/**
* @return bool
*/
private function isLink()
private function isLink(): bool
{
return $this->type === static::TYPE_LINK;
return $this->type === self::TYPE_LINK;
}

/**
* @return array
*/
private function getUrlParameter()
private function getUrlParameter(): array
{
return $this->isLink() ? ['url' => $this->url] : [];
}
Expand Down
12 changes: 5 additions & 7 deletions src/PushbulletServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
<?php

declare(strict_types=1);

namespace NotificationChannels\Pushbullet;

use GuzzleHttp\Client as HttpClient;
use Illuminate\Support\ServiceProvider;

class PushbulletServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*/
public function boot()
public function boot(): void
{
// Bootstrap code here.
$this->app->when(PushbulletChannel::class)
->needs(Pushbullet::class)
->give(function () {
->give(static function (): Pushbullet {
$config = config('services.pushbullet');

return new Pushbullet($config['access_token'], new HttpClient);
return new Pushbullet($config['access_token'], new HttpClient());
});
}
}
2 changes: 2 additions & 0 deletions src/Targets/Channel.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace NotificationChannels\Pushbullet\Targets;

class Channel implements Targetable
Expand Down
2 changes: 2 additions & 0 deletions src/Targets/Device.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace NotificationChannels\Pushbullet\Targets;

class Device implements Targetable
Expand Down
2 changes: 2 additions & 0 deletions src/Targets/Email.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace NotificationChannels\Pushbullet\Targets;

use NotificationChannels\Pushbullet\Exceptions\CouldNotSendNotification;
Expand Down
2 changes: 2 additions & 0 deletions src/Targets/Targetable.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace NotificationChannels\Pushbullet\Targets;

interface Targetable
Expand Down
Loading

0 comments on commit 6f8676e

Please sign in to comment.