Skip to content

Commit

Permalink
Merge pull request #103 from laravel-notification-channels/cache-client
Browse files Browse the repository at this point in the history
Automatically refresh tokens after 20 minutes
  • Loading branch information
dwightwatson authored Jun 10, 2020
2 parents e99ad69 + 7b1b49c commit 3a0cec0
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 18 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"require": {
"php": "^7.2.5",
"edamov/pushok": "^0.11",
"illuminate/cache": "^7.0",
"illuminate/config": "^7.0",
"illuminate/events": "^7.0",
"illuminate/notifications": "^7.0",
Expand Down
20 changes: 9 additions & 11 deletions src/ApnChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
class ApnChannel
{
/**
* The APNS client.
* The Pushok\Client factory.
*
* @var \Pushok\Client
* @var \NotificationChannels\Apn\ClientFactory
*/
protected $client;
protected $factory;

/**
* The event dispatcher.
Expand All @@ -27,12 +27,12 @@ class ApnChannel
/**
* Create a new channel instance.
*
* @param \Pushok\Client $client
* @param \NotificationChannels\Apn\ClientFactory $factory
* @param \Illuminate\Contracts\Events\Dispatcher $events
*/
public function __construct(Client $client, Dispatcher $events)
public function __construct(ClientFactory $factory, Dispatcher $events)
{
$this->client = $client;
$this->factory = $factory;
$this->events = $events;
}

Expand All @@ -53,11 +53,9 @@ public function send($notifiable, Notification $notification)

$message = $notification->toApn($notifiable);

$responses = $this->sendNotifications(
$message->client ?? $this->client,
$message,
$tokens
);
$client = $message->client ?? $this->factory->instance();

$responses = $this->sendNotifications($client, $message, $tokens);

$this->dispatchEvents($notifiable, $notification, $responses);

Expand Down
8 changes: 3 additions & 5 deletions src/ApnVoipChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ public function send($notifiable, Notification $notification)

$message = $notification->toApnVoip($notifiable);

$responses = $this->sendNotifications(
$message->client ?? $this->client,
$message,
$tokens
);
$client = $message->client ?? $this->factory->instance();

$responses = $this->sendNotifications($client, $message, $tokens);

$this->dispatchEvents($notifiable, $notification, $responses);

Expand Down
57 changes: 57 additions & 0 deletions src/ClientFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace NotificationChannels\Apn;

use Illuminate\Contracts\Cache\Repository;
use Illuminate\Contracts\Container\Container;
use Illuminate\Support\Carbon;
use Pushok\Client;

class ClientFactory
{
/**
* The number of minutes to cache the client.
*
* @var int
*/
const CACHE_MINUTES = 20;

/**
* The app instance.
*
* @var \Illuminate\Contracts\Container\Container
*/
protected $app;

/**
* The cache repository instance.
*
* @var \Illuminate\Contracts\Cache\Repository
*/
protected $cache;

/**
* Create a new factory instance.
*
* @param \Illuminate\Contracts\Container\Container $app
* @param \Illuminate\Contracts\Cache\Repository $cache
* @return void
*/
public function __construct(Container $app, Repository $cache)
{
$this->app = $app;
$this->cache = $cache;
}

/**
* Get an instance of the Pushok client, holding on to each in the cache for the given length of time.
*
* @return \Pushok\Client
*/
public function instance(): Client
{
return $this->cache->remember(Client::class, Carbon::now()->addMinutes(static::CACHE_MINUTES), function () {
return $this->app->make(Client::class);
});
}
}
5 changes: 4 additions & 1 deletion tests/ApnChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Notifications\Events\NotificationFailed;
use Mockery;
use NotificationChannels\Apn\ApnChannel;
use NotificationChannels\Apn\ClientFactory;
use Pushok\Client;
use Pushok\Response;

Expand All @@ -19,8 +20,10 @@ class ApnChannelTest extends TestCase
public function setUp(): void
{
$this->client = Mockery::mock(Client::class);
$this->factory = Mockery::mock(ClientFactory::class);
$this->factory->shouldReceive('instance')->andReturn($this->client);
$this->events = Mockery::mock(Dispatcher::class);
$this->channel = new ApnChannel($this->client, $this->events);
$this->channel = new ApnChannel($this->factory, $this->events);
}

/** @test */
Expand Down
5 changes: 4 additions & 1 deletion tests/ApnVoipChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Mockery;
use NotificationChannels\Apn\ApnMessage;
use NotificationChannels\Apn\ApnVoipChannel;
use NotificationChannels\Apn\ClientFactory;
use Pushok\Client;
use Pushok\Response;

Expand All @@ -22,8 +23,10 @@ class ApnVoipChannelTest extends TestCase
public function setUp(): void
{
$this->client = Mockery::mock(Client::class);
$this->factory = Mockery::mock(ClientFactory::class);
$this->factory->shouldReceive('instance')->andReturn($this->client);
$this->events = Mockery::mock(Dispatcher::class);
$this->channel = new ApnVoipChannel($this->client, $this->events);
$this->channel = new ApnVoipChannel($this->factory, $this->events);
}

/** @test */
Expand Down
44 changes: 44 additions & 0 deletions tests/ClientFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace NotificationChannels\Apn\Tests;

use Illuminate\Contracts\Cache\Repository;
use Illuminate\Contracts\Container\Container;
use Illuminate\Support\Carbon;
use Mockery;
use NotificationChannels\Apn\ClientFactory;
use Pushok\Client;

class ClientFactoryTest extends TestCase
{
protected $app;
protected $cache;
protected $factory;

public function setUp(): void
{
$this->app = Mockery::mock(Container::class);
$this->cache = Mockery::mock(Repository::class);
$this->factory = new ClientFactory($this->app, $this->cache);
}

/** @test */
public function it_returns_an_instance_through_the_cache()
{
$client = Mockery::mock(Client::class);

$this->app->shouldReceive('make')
->with(Client::class)
->andReturn($client);

$this->cache->shouldReceive('remember')
->with(Client::class, Mockery::type(Carbon::class), Mockery::on(function ($closure) use ($client) {
return $client == $closure();
}))
->andReturn($client);

$result = $this->factory->instance();

$this->assertSame($client, $result);
}
}

0 comments on commit 3a0cec0

Please sign in to comment.