Skip to content

Commit 6f8676e

Browse files
authored
Merge pull request #43 from laravel-notification-channels/improvements
Code improvements
2 parents 318e534 + 01e102d commit 6f8676e

13 files changed

+134
-58
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ build
33
composer.phar
44
composer.lock
55
.phpunit.result.cache
6+
build/

phpunit.xml.dist

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@
1111
stopOnFailure="false">
1212
<testsuites>
1313
<testsuite name="Pushbullet Notification Channel Test Suite">
14-
<directory>tests</directory>
14+
<directory suffix="Test.php">tests</directory>
1515
</testsuite>
1616
</testsuites>
17+
1718
<filter>
18-
<whitelist>
19-
<directory suffix=".php">src/</directory>
19+
<whitelist processUncoveredFilesFromWhitelist="true">
20+
<directory suffix=".php">src</directory>
2021
</whitelist>
2122
</filter>
23+
2224
<logging>
2325
<log type="tap" target="build/report.tap"/>
2426
<log type="junit" target="build/report.junit.xml"/>
Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,30 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace NotificationChannels\Pushbullet\Exceptions;
46

5-
use GuzzleHttp\Exception\ClientException;
7+
use Psr\Http\Message\ResponseInterface;
68
use RuntimeException;
79

810
class CouldNotSendNotification extends RuntimeException
911
{
10-
public static function pushbulletRespondedWithAnError(ClientException $exception)
12+
public static function pushbulletRespondedWithAnError(ResponseInterface $response): self
1113
{
12-
$code = $exception->getResponse()->getStatusCode();
13-
14-
$message = $exception->getResponse()->getBody();
14+
$code = $response->getStatusCode();
1515

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

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

24-
public static function couldNotSendNotificationWithoutRecipient()
21+
public static function providedEmailIsInvalid(string $email): self
2522
{
26-
return new static('Neither device id nor email of recipient was not specified');
23+
return new self("Provided email `{$email}` of `notifiable` is not valid.");
2724
}
2825

29-
public static function couldNotCommunicateWithPushbullet()
26+
public static function couldNotCommunicateWithPushbullet(): self
3027
{
31-
return new static("Couldn't connect to Pushbullet API.");
28+
return new self('Could not connect to Pushbullet API.');
3229
}
3330
}

src/Pushbullet.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace NotificationChannels\Pushbullet;
46

57
use Exception;
68
use GuzzleHttp\Client as HttpClient;
79
use GuzzleHttp\Exception\ClientException;
810
use NotificationChannels\Pushbullet\Exceptions\CouldNotSendNotification;
11+
use Psr\Http\Message\ResponseInterface;
912

1013
class Pushbullet
1114
{
1215
/** @var string */
13-
protected $token;
16+
private $token;
1417

1518
/** @var \GuzzleHttp\Client */
16-
protected $httpClient;
19+
private $httpClient;
1720

1821
/**
1922
* Create small Pushbullet client.
2023
*
2124
* @param string $token
2225
* @param \GuzzleHttp\Client $httpClient
2326
*/
24-
public function __construct($token, HttpClient $httpClient)
27+
public function __construct(string $token, HttpClient $httpClient)
2528
{
2629
$this->token = $token;
27-
2830
$this->httpClient = $httpClient;
2931
}
3032

@@ -33,7 +35,7 @@ public function __construct($token, HttpClient $httpClient)
3335
*
3436
* @return string
3537
*/
36-
protected function getPushbulletUrl()
38+
private function getPushbulletUrl(): string
3739
{
3840
return 'https://api.pushbullet.com/v2/pushes';
3941
}
@@ -43,7 +45,7 @@ protected function getPushbulletUrl()
4345
*
4446
* @return array
4547
*/
46-
protected function getHeaders()
48+
private function getHeaders(): array
4749
{
4850
return [
4951
'Access-Token' => $this->token,
@@ -54,9 +56,10 @@ protected function getHeaders()
5456
* Send request to Pushbullet API.
5557
*
5658
* @param array $params
59+
*
5760
* @return \Psr\Http\Message\ResponseInterface
5861
*/
59-
public function send($params)
62+
public function send($params): ResponseInterface
6063
{
6164
$url = $this->getPushbulletUrl();
6265

@@ -66,7 +69,7 @@ public function send($params)
6669
'headers' => $this->getHeaders(),
6770
]);
6871
} catch (ClientException $exception) {
69-
throw CouldNotSendNotification::pushbulletRespondedWithAnError($exception);
72+
throw CouldNotSendNotification::pushbulletRespondedWithAnError($exception->getResponse());
7073
} catch (Exception $exception) {
7174
throw CouldNotSendNotification::couldNotCommunicateWithPushbullet();
7275
}

src/PushbulletChannel.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace NotificationChannels\Pushbullet;
46

57
use Illuminate\Notifications\Notification;
@@ -9,13 +11,10 @@
911

1012
class PushbulletChannel
1113
{
12-
/**
13-
* @var \NotificationChannels\Pushbullet\Pushbullet
14-
*/
15-
protected $pushbullet;
14+
/** @var \NotificationChannels\Pushbullet\Pushbullet */
15+
private $pushbullet;
1616

1717
/**
18-
* Create pushbullet notification channel.
1918
* @param \NotificationChannels\Pushbullet\Pushbullet $pushbullet
2019
*/
2120
public function __construct(Pushbullet $pushbullet)
@@ -31,7 +30,7 @@ public function __construct(Pushbullet $pushbullet)
3130
*
3231
* @throws \NotificationChannels\Pushbullet\Exceptions\CouldNotSendNotification
3332
*/
34-
public function send($notifiable, Notification $notification)
33+
public function send($notifiable, Notification $notification): void
3534
{
3635
if (! $target = $this->getTarget($notifiable)) {
3736
return;
@@ -45,12 +44,13 @@ public function send($notifiable, Notification $notification)
4544

4645
/**
4746
* @param $notifiable
47+
*
4848
* @return \NotificationChannels\Pushbullet\Targets\Targetable|void
4949
*/
50-
protected function getTarget($notifiable)
50+
private function getTarget($notifiable): ?Targetable
5151
{
5252
if (! $target = $notifiable->routeNotificationFor('pushbullet')) {
53-
return;
53+
return null;
5454
}
5555

5656
if ($target instanceof Targetable) {

src/PushbulletMessage.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace NotificationChannels\Pushbullet;
46

57
use NotificationChannels\Pushbullet\Targets\Targetable;
68

79
class PushbulletMessage
810
{
9-
const TYPE_NOTE = 'note';
10-
const TYPE_LINK = 'link';
11+
private const TYPE_NOTE = 'note';
12+
private const TYPE_LINK = 'link';
1113

1214
/**
1315
* Type of message (currently: note or link).
@@ -45,7 +47,7 @@ class PushbulletMessage
4547
*
4648
* @return static
4749
*/
48-
public static function create($message)
50+
public static function create($message): self
4951
{
5052
return new static($message);
5153
}
@@ -63,7 +65,7 @@ public function __construct($message)
6365
*
6466
* @return $this
6567
*/
66-
public function target(Targetable $targetable)
68+
public function target(Targetable $targetable): self
6769
{
6870
$this->target = $targetable;
6971

@@ -75,9 +77,9 @@ public function target(Targetable $targetable)
7577
*
7678
* @return $this
7779
*/
78-
public function note()
80+
public function note(): self
7981
{
80-
$this->type = static::TYPE_NOTE;
82+
$this->type = self::TYPE_NOTE;
8183

8284
return $this;
8385
}
@@ -87,9 +89,9 @@ public function note()
8789
*
8890
* @return $this
8991
*/
90-
public function link()
92+
public function link(): self
9193
{
92-
$this->type = static::TYPE_LINK;
94+
$this->type = self::TYPE_LINK;
9395

9496
return $this;
9597
}
@@ -101,7 +103,7 @@ public function link()
101103
*
102104
* @return $this
103105
*/
104-
public function title($title)
106+
public function title($title): self
105107
{
106108
$this->title = $title;
107109

@@ -115,7 +117,7 @@ public function title($title)
115117
*
116118
* @return $this
117119
*/
118-
public function message($message)
120+
public function message($message): self
119121
{
120122
$this->message = $message;
121123

@@ -129,7 +131,7 @@ public function message($message)
129131
*
130132
* @return $this
131133
*/
132-
public function url($url)
134+
public function url($url): self
133135
{
134136
$this->url = $url;
135137

@@ -141,7 +143,7 @@ public function url($url)
141143
*
142144
* @return array
143145
*/
144-
public function toArray()
146+
public function toArray(): array
145147
{
146148
$payload = [
147149
'type' => $this->type,
@@ -159,15 +161,15 @@ public function toArray()
159161
/**
160162
* @return bool
161163
*/
162-
private function isLink()
164+
private function isLink(): bool
163165
{
164-
return $this->type === static::TYPE_LINK;
166+
return $this->type === self::TYPE_LINK;
165167
}
166168

167169
/**
168170
* @return array
169171
*/
170-
private function getUrlParameter()
172+
private function getUrlParameter(): array
171173
{
172174
return $this->isLink() ? ['url' => $this->url] : [];
173175
}

src/PushbulletServiceProvider.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace NotificationChannels\Pushbullet;
46

57
use GuzzleHttp\Client as HttpClient;
68
use Illuminate\Support\ServiceProvider;
79

810
class PushbulletServiceProvider extends ServiceProvider
911
{
10-
/**
11-
* Bootstrap the application services.
12-
*/
13-
public function boot()
12+
public function boot(): void
1413
{
15-
// Bootstrap code here.
1614
$this->app->when(PushbulletChannel::class)
1715
->needs(Pushbullet::class)
18-
->give(function () {
16+
->give(static function (): Pushbullet {
1917
$config = config('services.pushbullet');
2018

21-
return new Pushbullet($config['access_token'], new HttpClient);
19+
return new Pushbullet($config['access_token'], new HttpClient());
2220
});
2321
}
2422
}

src/Targets/Channel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace NotificationChannels\Pushbullet\Targets;
46

57
class Channel implements Targetable

src/Targets/Device.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace NotificationChannels\Pushbullet\Targets;
46

57
class Device implements Targetable

src/Targets/Email.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace NotificationChannels\Pushbullet\Targets;
46

57
use NotificationChannels\Pushbullet\Exceptions\CouldNotSendNotification;

0 commit comments

Comments
 (0)