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 4a53224
Showing 1 changed file with 14 additions and 15 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);
}
}

0 comments on commit 4a53224

Please sign in to comment.