-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from laravel-notification-channels/cache-client
Automatically refresh tokens after 20 minutes
- Loading branch information
Showing
7 changed files
with
122 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |