Skip to content

Commit 0698f1a

Browse files
committed
Major code cleanup, formatting fixes and upgrades like typed properties and params
This also adjusts the test according to the latest changes.
1 parent bee31a7 commit 0698f1a

18 files changed

+233
-204
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
strategy:
1414
fail-fast: true
1515
matrix:
16-
php: [8.2, 8.1, 8.0]
16+
php: [8.3, 8.2, 8.1]
1717
laravel: ['8.*', '9.*', '10.*', '11.*']
1818
include:
1919
- laravel: 10.*
@@ -25,16 +25,8 @@ jobs:
2525
- laravel: 11.*
2626
testbench: 9.*
2727
exclude:
28-
- laravel: 10.*
29-
php: 8.0
30-
- laravel: 10.*
31-
php: 7.4
32-
- laravel: 9.*
33-
php: 7.4
3428
- laravel: 11.*
3529
php: 8.1
36-
- laravel: 11.*
37-
php: 8.0
3830

3931
steps:
4032
- name: Checkout code

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/vendor
22
build
3+
.phpunit.result.cache
34
composer.phar
45
composer.lock

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
}
1313
],
1414
"require": {
15-
"php": "^8.0",
15+
"php": "^8.1",
1616
"guzzlehttp/guzzle": "^7.0.1",
1717
"illuminate/notifications": "^8.0 || ^9.0 || ^10.0 || ^11.0",
1818
"illuminate/support": "^8.0 || ^9.0 || ^10.0 || ^11.0"

src/Exceptions/CouldNotSendNotification.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
namespace NotificationChannels\Pushover\Exceptions;
44

55
use Exception;
6+
use Illuminate\Notifications\AnonymousNotifiable;
67
use Psr\Http\Message\ResponseInterface;
78

89
class CouldNotSendNotification extends Exception
910
{
10-
public static function serviceRespondedWithAnError(ResponseInterface $response, $notifiable)
11+
public static function serviceRespondedWithAnError(ResponseInterface $response, $notifiable): static
1112
{
1213
$statusCode = $response->getStatusCode();
1314

14-
$result = json_decode($response->getBody());
15+
$result = json_decode($response->getBody()->getContents());
1516

1617
$exceptionMessage = sprintf(
1718
"Pushover responded with an error (%s) for notifiable '%s' with id '%s'",
@@ -30,8 +31,12 @@ public static function serviceRespondedWithAnError(ResponseInterface $response,
3031
return new static($exceptionMessage, $statusCode);
3132
}
3233

33-
public static function pushoverKeyHasWrongLength($notifiable)
34+
public static function pushoverKeyHasWrongLength($notifiable): static
3435
{
36+
if ($notifiable instanceof AnonymousNotifiable) {
37+
return new static('Pushover key has wrong length. It needs to be 30 characters long.');
38+
}
39+
3540
$exceptionMessage = sprintf(
3641
"Pushover key has wrong length for notifiable '%s' with id '%s'. It needs to be 30 characters long.",
3742
get_class($notifiable),

src/Exceptions/ServiceCommunicationError.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class ServiceCommunicationError extends Exception
88
{
9-
public static function communicationFailed(Exception $exception)
9+
public static function communicationFailed(Exception $exception): static
1010
{
1111
return new static("The communication with Pushover failed because `{$exception->getCode()} - {$exception->getMessage()}`");
1212
}

src/Pushover.php

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
use Exception;
66
use GuzzleHttp\Client as HttpClient;
7+
use GuzzleHttp\Exception\GuzzleException;
78
use GuzzleHttp\Exception\RequestException;
89
use NotificationChannels\Pushover\Exceptions\CouldNotSendNotification;
910
use NotificationChannels\Pushover\Exceptions\ServiceCommunicationError;
11+
use Psr\Http\Message\ResponseInterface;
1012

1113
class Pushover
1214
{
@@ -22,27 +24,27 @@ class Pushover
2224
*
2325
* @var string
2426
*/
25-
protected $pushoverApiUrl = 'https://api.pushover.net/1/messages.json';
27+
protected string $pushoverApiUrl = 'https://api.pushover.net/1/messages.json';
2628

2729
/**
2830
* The HTTP client instance.
2931
*
30-
* @var \GuzzleHttp\Client
32+
* @var HttpClient
3133
*/
32-
protected $http;
34+
protected HttpClient $http;
3335

3436
/**
3537
* Pushover App Token.
3638
*
3739
* @var string
3840
*/
39-
protected $token;
41+
protected string $token;
4042

4143
/**
42-
* @param HttpClient $http
43-
* @param string $token
44+
* @param HttpClient $http
45+
* @param string $token
4446
*/
45-
public function __construct(HttpClient $http, $token)
47+
public function __construct(HttpClient $http, string $token)
4648
{
4749
$this->http = $http;
4850

@@ -54,21 +56,23 @@ public function __construct(HttpClient $http, $token)
5456
*
5557
* @link https://pushover.net/api
5658
*
57-
* @param array $params
59+
* @param array $params
5860
* @param mixed $notifiable
59-
* @return \Psr\Http\Message\ResponseInterface
61+
* @return ResponseInterface
6062
*
6163
* @throws CouldNotSendNotification
64+
* @throws ServiceCommunicationError
65+
* @throws GuzzleException
6266
*/
63-
public function send($params, $notifiable)
67+
public function send(array $params, mixed $notifiable): ResponseInterface
6468
{
6569
try {
6670
$multipart = [];
6771

6872
foreach ($this->paramsWithToken($params) as $name => $contents) {
6973
if ($name !== 'image') {
7074
$multipart[] = [
71-
'name' => $name,
75+
'name' => $name,
7276
'contents' => $contents,
7377
];
7478
} else {
@@ -80,6 +84,7 @@ public function send($params, $notifiable)
8084
}
8185
}
8286

87+
//dd($multipart);
8388
return $this->http->post(
8489
$this->pushoverApiUrl,
8590
[
@@ -101,10 +106,10 @@ public function send($params, $notifiable)
101106
/**
102107
* Merge token into parameters array, unless it has been set on the PushoverReceiver.
103108
*
104-
* @param array $params
109+
* @param array $params
105110
* @return array
106111
*/
107-
protected function paramsWithToken($params)
112+
protected function paramsWithToken(array $params): array
108113
{
109114
return array_merge([
110115
'token' => $this->token,
@@ -119,9 +124,14 @@ protected function paramsWithToken($params)
119124
*
120125
* @param $file
121126
* @return array|null
127+
* @throws GuzzleException
122128
*/
123129
private function getImageData($file): ?array
124130
{
131+
if (empty($file)) {
132+
return null;
133+
}
134+
125135
try {
126136
// check if $file is not too big
127137
if (is_file($file) && is_readable($file)) {
@@ -141,7 +151,7 @@ private function getImageData($file): ?array
141151
}
142152

143153
// some servers may not return the "Content-Length" header
144-
$fileSizeChecked = (bool) $contentLength;
154+
$fileSizeChecked = (bool)$contentLength;
145155
}
146156

147157
// check if $file is an image
@@ -153,7 +163,7 @@ private function getImageData($file): ?array
153163

154164
$contents = file_get_contents($file);
155165
// if not checked before, finally check the file size after reading it
156-
if (! $fileSizeChecked && strlen($contents) > self::IMAGE_SIZE_LIMIT) {
166+
if (!$fileSizeChecked && strlen($contents) > self::IMAGE_SIZE_LIMIT) {
157167
return null;
158168
}
159169
} catch (Exception $exception) {
@@ -162,10 +172,10 @@ private function getImageData($file): ?array
162172

163173
return [
164174
// name of the field holding the image must be 'attachment' (https://pushover.net/api#attachments)
165-
'name' => 'attachment',
175+
'name' => 'attachment',
166176
'contents' => $contents,
167177
'filename' => basename($file),
168-
'headers' => [
178+
'headers' => [
169179
'Content-Type' => $contentType,
170180
],
171181
];

src/PushoverChannel.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace NotificationChannels\Pushover;
44

5+
use GuzzleHttp\Exception\GuzzleException;
56
use Illuminate\Contracts\Events\Dispatcher;
67
use Illuminate\Notifications\Events\NotificationFailed;
78
use Illuminate\Notifications\Notification;
@@ -10,16 +11,15 @@
1011

1112
class PushoverChannel
1213
{
13-
/** @var Pushover */
14-
protected $pushover;
14+
protected Pushover $pushover;
1515

16-
/** @var Dispatcher */
17-
protected $events;
16+
protected Dispatcher $events;
1817

1918
/**
2019
* Create a new Pushover channel instance.
2120
*
22-
* @param Pushover $pushover
21+
* @param Pushover $pushover
22+
* @param Dispatcher $events
2323
*/
2424
public function __construct(Pushover $pushover, Dispatcher $events)
2525
{
@@ -30,12 +30,13 @@ public function __construct(Pushover $pushover, Dispatcher $events)
3030
/**
3131
* Send the given notification.
3232
*
33-
* @param mixed $notifiable
34-
* @param \Illuminate\Notifications\Notification $notification
33+
* @param mixed $notifiable
34+
* @param Notification $notification
3535
*
36-
* @throws \NotificationChannels\Pushover\Exceptions\CouldNotSendNotification
36+
* @throws CouldNotSendNotification
37+
* @throws GuzzleException
3738
*/
38-
public function send($notifiable, Notification $notification)
39+
public function send(mixed $notifiable, Notification $notification): void
3940
{
4041
if (! $pushoverReceiver = $notifiable->routeNotificationFor('pushover')) {
4142
return;
@@ -63,7 +64,7 @@ public function send($notifiable, Notification $notification)
6364
}
6465
}
6566

66-
protected function fireFailedEvent($notifiable, $notification, $message)
67+
protected function fireFailedEvent($notifiable, $notification, $message): void
6768
{
6869
$this->events->dispatch(
6970
new NotificationFailed($notifiable, $notification, 'pushover', [$message])

0 commit comments

Comments
 (0)