Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
imanghafoori1 committed May 6, 2024
1 parent 24b966d commit 7ea6a61
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
29 changes: 14 additions & 15 deletions src/Decorators/DecoratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@ public static function foreverCache($key)

public static function variadicParam()
{
return function ($callable) {
return function (...$param) use ($callable) {
$param = is_array($param[0]) ? $param[0] : $param;

return Container::getInstance()->call($callable, $param);
};
return fn ($callable) => function (...$param) use ($callable) {
return Container::getInstance()->call($callable, is_array($param[0]) ? $param[0] : $param);
};
}

Expand All @@ -36,16 +32,19 @@ public static function variadicParam()
*/
private static function getDecoratorFactory($key, $remember, $minutes = null): Closure
{
return function ($callable) use ($key, $minutes, $remember) {
return function (...$params) use ($callable, $key, $minutes, $remember) {
$cb = fn () => Container::getInstance()->call($callable, $params);
return fn ($callable) => fn (...$params) => DecoratorFactory::call($callable, $params, $key, $minutes, $remember);
}

private static function call($callable, array $params, $key, $minutes, $remember)
{
$caller = fn () => Container::getInstance()->call($callable, $params);

if (is_callable($key)) {
$key = $key(...$params);
}
if (is_callable($key)) {
$key = $key(...$params);
}

return Container::getInstance()->make('cache')->$remember(...array_filter([$key, $minutes, $cb], fn ($el) => ! is_null($el)));
};
};
$args = array_filter([$key, $minutes, $caller], fn ($value) => ! is_null($value));

return Container::getInstance()->make('cache')->$remember(...$args);
}
}
33 changes: 25 additions & 8 deletions tests/CacheResultDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,31 @@ class CacheResultDecoratorTest extends TestCase
{
public function testCacheResultDecorator()
{
Container::getInstance()->singleton('abc', abc::class);
Container::getInstance()->singleton('abc', cachee::class);
\MyFacade::forgetDecorations('getGiven');
\MyFacade::decorateMethod('getGiven', DecoratorFactory::cache('hello', 2));

App::shouldReceive('call')->once()->andReturn('We may never know?!');

$this->assertEquals('We may never know?!', \MyFacade::getGiven(1));
$this->assertEquals('We may never know?!', \MyFacade::getGiven(1));
$this->assertEquals('We may never know?!', \MyFacade::getGiven(1));

$this->assertEquals(1, cachee::$counter);
\MyFacade::forgetDecorations('getGiven');

// clean up:
cachee::$counter = 0;
}

public function testPermanentCacheResultDecorator()
{
Container::getInstance()->singleton('abc', abc::class);

Container::getInstance()->singleton('abc', cachee::class);
\MyFacade::forgetDecorations('getGiven');
\MyFacade::decorateMethod('getGiven', DecoratorFactory::foreverCache(function ($a) {
return 'cache_key_'.$a;
}));
\MyFacade::decorateMethod('getGiven', DecoratorFactory::foreverCache(
fn ($a) => 'cache_key_'.$a)
);

App::shouldReceive('call')->twice()->andReturn('We may never know?!');
cachee::$counter = 0;

$this->assertEquals('We may never know?!', \MyFacade::getGiven(1));
$this->assertEquals('We may never know?!', \MyFacade::getGiven(1));
Expand All @@ -39,6 +42,20 @@ public function testPermanentCacheResultDecorator()
$this->assertEquals('We may never know?!', \MyFacade::getGiven(2));
$this->assertEquals('We may never know?!', \MyFacade::getGiven(1));

$this->assertEquals(2, cachee::$counter);

\MyFacade::forgetDecorations('getGiven');
}
}

class cachee
{
public static $counter = 0;

public function getGiven()
{
self::$counter++;

return 'We may never know?!';
}
}

0 comments on commit 7ea6a61

Please sign in to comment.