Skip to content

Commit 5961641

Browse files
authored
Fire an event when unknown devices are referenced (#15)
1 parent b7990e7 commit 5961641

File tree

10 files changed

+105
-61
lines changed

10 files changed

+105
-61
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
All notable changes to `laravel-notification-channels/pushwoosh` will be documented in this file
44

5+
## 3.0.0
6+
- Removed `UnknownDevicesException`, a `UnknownDevices` event is now dispatched instead
7+
58
## 2.3.0 - (2020-09-17)
69
- Allow pushwoosh to be disabled on local environments via `config('services.pushwoosh.enabled')`
710

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,21 @@ Or to multiple users:
122122
Notification::send($customers, new WishlistItemOnSale($product));
123123
```
124124

125+
#### Unknown devices
126+
When you reference devices that do not exist (anymore), a `NotificationChannels\Pushwoosh\Events\UnknownDevices` event
127+
will be dispatched.
128+
129+
You can easily hook into this event like so:
130+
131+
```php
132+
Event::listen(
133+
NotificationChannels\Pushwoosh\Events\UnknownDevices::class,
134+
function ($event) {
135+
// Handle the event
136+
}
137+
);
138+
```
139+
125140
### Available methods
126141
This section details the public API of this package.
127142

composer.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@
1414
"ext-json": "*",
1515
"guzzlehttp/guzzle": "^6.2 || ^7.0",
1616
"guzzlehttp/psr7": "^1.0",
17-
"illuminate/notifications": "^5.5 || ^6.0 || ^7.0 || ^8.0",
18-
"illuminate/support": "^5.5 || ^6.0 || ^7.0 || ^8.0"
17+
"laravel/framework": "^5.5 || ^6.0 || ^7.0 || ^8.0"
1918
},
2019
"require-dev": {
21-
"illuminate/database": "^5.5 || ^6.0 || ^7.0 || ^8.0",
22-
"illuminate/queue": "^5.5 || ^6.0 || ^7.0 || ^8.0",
2320
"mockery/mockery": "^1.3.1",
24-
"phpunit/phpunit": "^7.5"
21+
"orchestra/testbench": "^3.5 || ^4.0 || ^5.0 || ^6.0",
22+
"phpunit/phpunit": "^7.5 || ^8.0 || ^9.0"
2523
},
2624
"autoload": {
2725
"psr-4": {

phpunit.xml.dist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
</include>
1717
</coverage>
1818
<testsuites>
19-
<testsuite name="Unit">
20-
<directory suffix="Test.php">./tests/Unit</directory>
19+
<testsuite name="Tests">
20+
<directory suffix="Test.php">./tests</directory>
2121
</testsuite>
2222
</testsuites>
2323
</phpunit>

src/Events/UnknownDevices.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace NotificationChannels\Pushwoosh\Events;
4+
5+
class UnknownDevices
6+
{
7+
/**
8+
* The referenced devices.
9+
*
10+
* @var string[]
11+
*/
12+
public $devices;
13+
14+
/**
15+
* The message ID.
16+
*
17+
* @var string
18+
*/
19+
public $message;
20+
21+
/**
22+
* Create a new event instance.
23+
*
24+
* @param string $message
25+
* @param array $devices
26+
* @return void
27+
*/
28+
public function __construct(string $message, array $devices)
29+
{
30+
$this->devices = $devices;
31+
$this->message = $message;
32+
}
33+
}

src/Exceptions/UnknownDeviceException.php

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/Pushwoosh.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
use function GuzzleHttp\json_decode;
77
use function GuzzleHttp\json_encode;
88
use GuzzleHttp\Psr7\Request;
9+
use Illuminate\Contracts\Events\Dispatcher;
910
use NotificationChannels\Pushwoosh\Concerns\DetectsPushwooshErrors;
11+
use NotificationChannels\Pushwoosh\Events\UnknownDevices;
1012
use NotificationChannels\Pushwoosh\Exceptions\PushwooshException;
11-
use NotificationChannels\Pushwoosh\Exceptions\UnknownDeviceException;
1213
use Throwable;
1314

1415
class Pushwoosh
@@ -17,22 +18,25 @@ class Pushwoosh
1718

1819
protected $application;
1920
protected $client;
21+
protected $dispatcher;
2022
protected $enabled;
2123
protected $token;
2224

2325
/**
2426
* Create a new Pushwoosh API client.
2527
*
2628
* @param \GuzzleHttp\ClientInterface $client
29+
* @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
2730
* @param string|null $application
2831
* @param string|null $token
2932
* @param bool $enabled
3033
* @return void
3134
*/
32-
public function __construct(ClientInterface $client, ?string $application, ?string $token, bool $enabled = true)
35+
public function __construct(ClientInterface $client, Dispatcher $dispatcher, ?string $application, ?string $token, bool $enabled = true)
3336
{
3437
$this->application = $application;
3538
$this->client = $client;
39+
$this->dispatcher = $dispatcher;
3640
$this->enabled = $enabled;
3741
$this->token = $token;
3842
}
@@ -66,7 +70,9 @@ public function createMessage(PushwooshPendingMessage $message)
6670
}
6771

6872
if (isset($response->response->UnknownDevices)) {
69-
throw new UnknownDeviceException($response->response->UnknownDevices);
73+
foreach ($response->response->UnknownDevices as $identifier => $devices) {
74+
$this->dispatcher->dispatch(new UnknownDevices($identifier, $devices));
75+
}
7076
}
7177

7278
$message->wasSent();

src/PushwooshServiceProvider.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace NotificationChannels\Pushwoosh;
44

55
use GuzzleHttp\Client;
6+
use Illuminate\Contracts\Foundation\Application;
67
use Illuminate\Notifications\ChannelManager;
78
use Illuminate\Support\ServiceProvider;
89

@@ -13,17 +14,18 @@ class PushwooshServiceProvider extends ServiceProvider
1314
*
1415
* @return void
1516
*/
16-
public function register()
17+
public function register(): void
1718
{
1819
$this->app->afterResolving(ChannelManager::class, function (ChannelManager $channels) {
1920
$channels->extend('pushwoosh', function ($app) {
2021
return $app[PushwooshChannel::class];
2122
});
2223
});
2324

24-
$this->app->bindIf(Pushwoosh::class, function ($app) {
25+
$this->app->bindIf(Pushwoosh::class, function (Application $app) {
2526
return new Pushwoosh(
26-
new Client(),
27+
$app->make(Client::class),
28+
$app['events'],
2729
$app['config']['services.pushwoosh.application'],
2830
$app['config']['services.pushwoosh.token'],
2931
$app['config']['services.pushwoosh.enabled'] ?? true

tests/TestCase.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace NotificationChannels\Pushwoosh\Tests;
4+
5+
use NotificationChannels\Pushwoosh\PushwooshServiceProvider;
6+
7+
abstract class TestCase extends \Orchestra\Testbench\TestCase
8+
{
9+
/**
10+
* Get the package providers.
11+
*
12+
* @param \Illuminate\Foundation\Application $app
13+
* @return string[]
14+
*/
15+
protected function getPackageProviders($app): array
16+
{
17+
return [
18+
PushwooshServiceProvider::class,
19+
];
20+
}
21+
}

tests/Unit/PushwooshTest.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
use GuzzleHttp\Client;
66
use GuzzleHttp\Psr7\Response;
7-
use Illuminate\Support\Str;
7+
use Illuminate\Support\Facades\Event;
88
use Mockery;
99
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
10+
use NotificationChannels\Pushwoosh\Events\UnknownDevices;
1011
use NotificationChannels\Pushwoosh\Exceptions\PushwooshException;
11-
use NotificationChannels\Pushwoosh\Exceptions\UnknownDeviceException;
1212
use NotificationChannels\Pushwoosh\Pushwoosh;
1313
use NotificationChannels\Pushwoosh\PushwooshPendingMessage;
14-
use PHPUnit\Framework\TestCase;
14+
use NotificationChannels\Pushwoosh\Tests\TestCase;
1515

1616
class PushwooshTest extends TestCase
1717
{
@@ -32,8 +32,12 @@ class PushwooshTest extends TestCase
3232
*/
3333
protected function setUp(): void
3434
{
35-
$this->client = Mockery::mock(Client::class);
36-
$this->pushwoosh = new Pushwoosh($this->client, Str::random(8), Str::random(24));
35+
parent::setUp();
36+
37+
Event::fake();
38+
39+
$this->app->instance(Client::class, $this->client = Mockery::mock(Client::class));
40+
$this->pushwoosh = $this->app->make(Pushwoosh::class);
3741
}
3842

3943
/**
@@ -84,11 +88,13 @@ public function testUnknownDevices()
8488
new Response(200, [], file_get_contents(__DIR__ . '/../Fixtures/unknown-devices.json'))
8589
);
8690

87-
$this->expectException(UnknownDeviceException::class);
88-
$this->expectExceptionMessage('Unknown device(s) referenced: foo, bar');
89-
9091
$this->pushwoosh->createMessage(
9192
new PushwooshPendingMessage($this->pushwoosh)
9293
);
94+
95+
Event::assertDispatched(UnknownDevices::class, function (UnknownDevices $event) {
96+
return $event->message === 'AF0B-EEFE4D5E-E445B2E9'
97+
&& $event->devices === ['foo', 'bar'];
98+
});
9399
}
94100
}

0 commit comments

Comments
 (0)