Skip to content

Commit

Permalink
refactor: use Cache::rememberForever method (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored Aug 13, 2022
1 parent 7dc3f8a commit cf1e4da
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 50 deletions.
10 changes: 5 additions & 5 deletions src/Commands/Reload.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Monicahq\Cloudflare\Commands;

use Illuminate\Console\Command;
use Illuminate\Contracts\Cache\Factory;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\Cache\Factory as Cache;
use Illuminate\Contracts\Config\Repository as Config;
use Monicahq\Cloudflare\LaravelCloudflare;

class Reload extends Command
Expand All @@ -26,11 +26,11 @@ class Reload extends Command
/**
* Execute the console command.
*
* @param Factory $cache
* @param Repository $config
* @param \Illuminate\Contracts\Cache\Factory $cache
* @param \Illuminate\Contracts\Config\Repository $config
* @return void
*/
public function handle(Factory $cache, Repository $config)
public function handle(Cache $cache, Config $config)
{
$proxies = LaravelCloudflare::getProxies();

Expand Down
20 changes: 7 additions & 13 deletions src/Commands/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Monicahq\Cloudflare\Commands;

use Illuminate\Console\Command;
use Illuminate\Contracts\Cache\Factory;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\Cache\Factory as Cache;
use Illuminate\Contracts\Config\Repository as Config;

class View extends Command
{
Expand All @@ -25,22 +25,16 @@ class View extends Command
/**
* Execute the console command.
*
* @param Factory $cache
* @param Repository $config
* @param \Illuminate\Contracts\Cache\Factory $cache
* @param \Illuminate\Contracts\Config\Repository $config
* @return void
*/
public function handle(Factory $cache, Repository $config)
public function handle(Cache $cache, Config $config)
{
$proxies = $cache->store()->get($config->get('laravelcloudflare.cache'), []);

$rows = array_map(function ($value): array {
return [
$value,
];
}, $proxies);
$rows = array_map(fn ($value): array => [$value], $proxies);

$this->table([
'Address',
], $rows);
$this->table(['Address'], $rows);
}
}
6 changes: 1 addition & 5 deletions src/Http/Middleware/TrustProxies.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ class TrustProxies extends Middleware
protected function setTrustedProxyIpAddresses(Request $request)
{
$cacheKey = Config::get('laravelcloudflare.cache');
$cachedProxies = Cache::get($cacheKey, function () use ($cacheKey) {
return tap(LaravelCloudflare::getProxies(), function ($proxies) use ($cacheKey) {
Cache::forever($cacheKey, $proxies);
});
});
$cachedProxies = Cache::rememberForever($cacheKey, fn () => LaravelCloudflare::getProxies());

if (is_array($cachedProxies) && count($cachedProxies) > 0) {
$this->proxies = array_merge((array) $this->proxies, $cachedProxies);
Expand Down
4 changes: 1 addition & 3 deletions tests/FeatureTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ protected function resolveApplicationCore($app)
{
parent::resolveApplicationCore($app);

$app->detectEnvironment(function () {
return 'testing';
});
$app->detectEnvironment(fn () => 'testing');
}
}
44 changes: 23 additions & 21 deletions tests/Unit/Http/Middleware/TrustProxiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,63 @@

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Monicahq\Cloudflare\Facades\CloudflareProxies;
use Monicahq\Cloudflare\Http\Middleware\TrustProxies;
use Monicahq\Cloudflare\LaravelCloudflare;
use Monicahq\Cloudflare\Tests\FeatureTestCase;

class TrustProxiesTest extends FeatureTestCase
{
/** @test */
public function it_sets_trusted_proxies()
{
Cache::shouldReceive('get')
Cache::shouldReceive('rememberForever')
->with('cloudflare.proxies', \Closure::class)
->andReturn(['expect']);

$request = new Request();

$this->app->make(TrustProxies::class)->handle($request, function () {
});
$this->app->make(TrustProxies::class)->handle($request, fn () => null);

$proxies = $request->getTrustedProxies();

$this->assertEquals(['expect'], $proxies);
}

/** @test */
public function it_does_not_sets_trusted_proxies()
public function it_sets_trusted_proxies_with_cache()
{
Cache::shouldReceive('get')
->with('cloudflare.proxies', \Closure::class)
->andReturn([]);
LaravelCloudflare::getProxiesUsing(function () {
return ['expect'];
});

$request = new Request();
try {
$request = new Request();

$this->app->make(TrustProxies::class)->handle($request, function () {
});
$this->app->make(TrustProxies::class)->handle($request, fn () => null);

$proxies = $request->getTrustedProxies();
$proxies = $request->getTrustedProxies();

$this->assertEquals([], $proxies);
$this->assertEquals(['expect'], $proxies);
$this->assertTrue(Cache::has('cloudflare.proxies'));
$this->assertEquals(['expect'], Cache::get('cloudflare.proxies'));
} finally {
LaravelCloudflare::getProxiesUsing(null);
}
}

/** @test */
public function it_load_trustproxies()
public function it_does_not_sets_trusted_proxies()
{
CloudflareProxies::shouldReceive('load')
->once()
->andReturn(['expect']);
Cache::shouldReceive('rememberForever')
->with('cloudflare.proxies', \Closure::class)
->andReturn([]);

$request = new Request();

$this->app->make(TrustProxies::class)->handle($request, function () {
});
$this->app->make(TrustProxies::class)->handle($request, fn () => null);

$proxies = $request->getTrustedProxies();

$this->assertEquals(['expect'], $proxies);
$this->assertEquals(['expect'], $this->app['cache']->get('cloudflare.proxies'));
$this->assertEquals([], $proxies);
}
}
28 changes: 25 additions & 3 deletions tests/Unit/LaravelCloudflareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Monicahq\Cloudflare\Tests\Unit;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Monicahq\Cloudflare\Facades\CloudflareProxies;
use Monicahq\Cloudflare\Http\Middleware\TrustProxies;
use Monicahq\Cloudflare\LaravelCloudflare;
use Monicahq\Cloudflare\Tests\FeatureTestCase;
Expand All @@ -22,14 +24,34 @@ public function it_call_callback()
return ['expect'];
});

try {
$request = new Request();

$this->app->make(TrustProxies::class)->handle($request, fn () => null);

$proxies = $request->getTrustedProxies();

$this->assertTrue(static::$run);
$this->assertEquals(['expect'], $proxies);
} finally {
LaravelCloudflare::getProxiesUsing(null);
}
}

/** @test */
public function it_call_load()
{
CloudflareProxies::shouldReceive('load')
->once()
->andReturn(['expect']);

$request = new Request();

$this->app->make(TrustProxies::class)->handle($request, function () {
});
$this->app->make(TrustProxies::class)->handle($request, fn () => null);

$proxies = $request->getTrustedProxies();

$this->assertTrue(static::$run);
$this->assertEquals(['expect'], $proxies);
$this->assertEquals(['expect'], Cache::get('cloudflare.proxies'));
}
}

0 comments on commit cf1e4da

Please sign in to comment.