Skip to content

Commit 31ff8c1

Browse files
authored
Merge pull request #63 from laravel-notification-channels/refactoring
Refactoring
2 parents 7355bc4 + 26ab126 commit 31ff8c1

18 files changed

+300
-232
lines changed

.github/workflows/test.yml

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,47 @@
11
name: Tests
22

3-
on: [push, pull_request]
3+
on:
4+
- push
5+
- pull_request
46

57
jobs:
6-
test:
7-
name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }}
8-
runs-on: ubuntu-latest
9-
strategy:
10-
fail-fast: true
11-
matrix:
12-
php: [ 8.2, 8.1, 8.0 ]
13-
laravel: [ 10.*, 9.*, 8.* ]
14-
include:
15-
- laravel: 10.*
16-
testbench: 8.*
17-
- laravel: 9.*
18-
testbench: 7.*
19-
- laravel: 8.*
20-
testbench: 6.*
21-
exclude:
22-
- laravel: 10.*
23-
php: 8.0
24-
- laravel: 10.*
25-
php: 7.4
26-
- laravel: 9.*
27-
php: 7.4
28-
29-
steps:
30-
- name: Checkout code
31-
uses: actions/checkout@v2
32-
33-
- name: Set correct PHP version
34-
uses: shivammathur/setup-php@v2
35-
with:
36-
php-version: ${{ matrix.php }}
37-
coverage: pcov
38-
39-
- name: Install dependencies
40-
run: |
41-
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
42-
composer update --prefer-stable --prefer-dist --no-interaction --no-suggest
43-
44-
- name: Execute tests
45-
run: vendor/bin/phpunit
46-
47-
- name: Upload coverage to Scrutinizer
48-
run: |
49-
wget https://scrutinizer-ci.com/ocular.phar
50-
php ocular.phar code-coverage:upload --format=php-clover coverage.clover
8+
test:
9+
name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }}
10+
11+
runs-on: ubuntu-latest
12+
13+
strategy:
14+
fail-fast: true
15+
matrix:
16+
php: [8.3, 8.2, 8.1]
17+
laravel: ['8.*', '9.*', '10.*', '11.*']
18+
include:
19+
- laravel: 10.*
20+
testbench: 8.*
21+
- laravel: 9.*
22+
testbench: 7.*
23+
- laravel: 8.*
24+
testbench: 6.*
25+
- laravel: 11.*
26+
testbench: 9.*
27+
exclude:
28+
- laravel: 11.*
29+
php: 8.1
30+
31+
steps:
32+
- name: Checkout code
33+
uses: actions/checkout@v2
34+
35+
- name: Set correct PHP version
36+
uses: shivammathur/setup-php@v2
37+
with:
38+
php-version: ${{ matrix.php }}
39+
coverage: pcov
40+
41+
- name: Install dependencies
42+
run: |
43+
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
44+
composer update --prefer-stable --prefer-dist --no-interaction --no-suggest
45+
46+
- name: Execute tests
47+
run: vendor/bin/phpunit

.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: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,20 @@
1212
}
1313
],
1414
"require": {
15-
"php": "^8.0",
15+
"php": "^8.1",
1616
"guzzlehttp/guzzle": "^7.0.1",
17-
"illuminate/notifications": "^8.0 || ^9.0 || ^10.0",
18-
"illuminate/support": "^8.0 || ^9.0 || ^10.0"
17+
"illuminate/notifications": "^8.0 || ^9.0 || ^10.0 || ^11.0",
18+
"illuminate/support": "^8.0 || ^9.0 || ^10.0 || ^11.0"
1919
},
2020
"require-dev": {
2121
"mockery/mockery": "^1.3.1",
22-
"phpunit/phpunit": "^9.3",
23-
"orchestra/testbench": "^8.0",
22+
"phpunit/phpunit": "^9.3 || ^10.5",
23+
"orchestra/testbench": "^8.0 || ^9.0",
2424
"dms/phpunit-arraysubset-asserts": ">=0.1.0"
2525
},
26+
"suggest": {
27+
"ext-exif": "Required for image attachment support"
28+
},
2629
"autoload": {
2730
"psr-4": {
2831
"NotificationChannels\\Pushover\\": "src"

src/Exceptions/CouldNotSendNotification.php

Lines changed: 18 additions & 2 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'",
@@ -29,4 +30,19 @@ public static function serviceRespondedWithAnError(ResponseInterface $response,
2930

3031
return new static($exceptionMessage, $statusCode);
3132
}
33+
34+
public static function pushoverKeyHasWrongLength($notifiable): static
35+
{
36+
if ($notifiable instanceof AnonymousNotifiable) {
37+
return new static('Pushover key has wrong length. It needs to be 30 characters long.');
38+
}
39+
40+
$exceptionMessage = sprintf(
41+
"Pushover key has wrong length for notifiable '%s' with id '%s'. It needs to be 30 characters long.",
42+
get_class($notifiable),
43+
$notifiable->getKey()
44+
);
45+
46+
return new static($exceptionMessage);
47+
}
3248
}

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: 24 additions & 12 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
/**
4244
* @param HttpClient $http
4345
* @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

@@ -55,19 +57,22 @@ public function __construct(HttpClient $http, $token)
5557
* @link https://pushover.net/api
5658
*
5759
* @param array $params
58-
* @return \Psr\Http\Message\ResponseInterface
60+
* @param mixed $notifiable
61+
* @return ResponseInterface
5962
*
6063
* @throws CouldNotSendNotification
64+
* @throws ServiceCommunicationError
65+
* @throws GuzzleException
6166
*/
62-
public function send($params)
67+
public function send(array $params, mixed $notifiable): ResponseInterface
6368
{
6469
try {
6570
$multipart = [];
6671

6772
foreach ($this->paramsWithToken($params) as $name => $contents) {
6873
if ($name !== 'image') {
6974
$multipart[] = [
70-
'name' => $name,
75+
'name' => $name,
7176
'contents' => $contents,
7277
];
7378
} else {
@@ -79,6 +84,7 @@ public function send($params)
7984
}
8085
}
8186

87+
//dd($multipart);
8288
return $this->http->post(
8389
$this->pushoverApiUrl,
8490
[
@@ -103,7 +109,7 @@ public function send($params)
103109
* @param array $params
104110
* @return array
105111
*/
106-
protected function paramsWithToken($params)
112+
protected function paramsWithToken(array $params): array
107113
{
108114
return array_merge([
109115
'token' => $this->token,
@@ -116,11 +122,17 @@ protected function paramsWithToken($params)
116122
* If there is any error (problem with reading the file, file size exceeds the limit, the file is not an image),
117123
* silently returns null and sends the message without image attachment.
118124
*
119-
* @param $file
125+
* @param $file
120126
* @return array|null
127+
*
128+
* @throws GuzzleException
121129
*/
122130
private function getImageData($file): ?array
123131
{
132+
if (empty($file)) {
133+
return null;
134+
}
135+
124136
try {
125137
// check if $file is not too big
126138
if (is_file($file) && is_readable($file)) {
@@ -161,10 +173,10 @@ private function getImageData($file): ?array
161173

162174
return [
163175
// name of the field holding the image must be 'attachment' (https://pushover.net/api#attachments)
164-
'name' => 'attachment',
176+
'name' => 'attachment',
165177
'contents' => $contents,
166178
'filename' => basename($file),
167-
'headers' => [
179+
'headers' => [
168180
'Content-Type' => $contentType,
169181
],
170182
];

src/PushoverChannel.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,24 @@
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;
9+
use NotificationChannels\Pushover\Exceptions\CouldNotSendNotification;
810
use NotificationChannels\Pushover\Exceptions\ServiceCommunicationError;
911

1012
class PushoverChannel
1113
{
12-
/** @var Pushover */
13-
protected $pushover;
14+
protected Pushover $pushover;
1415

15-
/** @var Dispatcher */
16-
protected $events;
16+
protected Dispatcher $events;
1717

1818
/**
1919
* Create a new Pushover channel instance.
2020
*
2121
* @param Pushover $pushover
22+
* @param Dispatcher $events
2223
*/
2324
public function __construct(Pushover $pushover, Dispatcher $events)
2425
{
@@ -30,32 +31,42 @@ public function __construct(Pushover $pushover, Dispatcher $events)
3031
* Send the given notification.
3132
*
3233
* @param mixed $notifiable
33-
* @param \Illuminate\Notifications\Notification $notification
34+
* @param Notification $notification
3435
*
35-
* @throws \NotificationChannels\Pushover\Exceptions\CouldNotSendNotification
36+
* @throws CouldNotSendNotification
37+
* @throws GuzzleException
3638
*/
37-
public function send($notifiable, Notification $notification)
39+
public function send(mixed $notifiable, Notification $notification): void
3840
{
3941
if (! $pushoverReceiver = $notifiable->routeNotificationFor('pushover')) {
4042
return;
4143
}
4244

4345
if (is_string($pushoverReceiver)) {
46+
// From https://pushover.net/api:
47+
// "User and group identifiers are 30 characters long, ..."
48+
if (strlen($pushoverReceiver) !== 30) {
49+
throw CouldNotSendNotification::pushoverKeyHasWrongLength($notifiable);
50+
}
51+
4452
$pushoverReceiver = PushoverReceiver::withUserKey($pushoverReceiver);
4553
}
4654

4755
$message = $notification->toPushover($notifiable);
4856

4957
try {
50-
$this->pushover->send(array_merge($message->toArray(), $pushoverReceiver->toArray()));
58+
$this->pushover->send(
59+
array_merge($message->toArray(), $pushoverReceiver->toArray()),
60+
$notifiable
61+
);
5162
} catch (ServiceCommunicationError $serviceCommunicationError) {
5263
$this->fireFailedEvent($notifiable, $notification, $serviceCommunicationError->getMessage());
5364
}
5465
}
5566

56-
protected function fireFailedEvent($notifiable, $notification, $message)
67+
protected function fireFailedEvent($notifiable, $notification, $message): void
5768
{
58-
$this->events->fire(
69+
$this->events->dispatch(
5970
new NotificationFailed($notifiable, $notification, 'pushover', [$message])
6071
);
6172
}

0 commit comments

Comments
 (0)