Skip to content

Commit

Permalink
refactor: move count strategy resolver to CountStrategyUtil (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi authored Jul 7, 2024
1 parent f8e00af commit 3512eac
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 24 deletions.
22 changes: 1 addition & 21 deletions packages/collections-common/src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace Rekalogika\Domain\Collections\Common;

use Doctrine\Common\Collections\Order;
use Rekalogika\Domain\Collections\Common\Count\ConditionalDelegatedCountStrategy;
use Rekalogika\Domain\Collections\Common\Count\CountStrategy;
use Rekalogika\Domain\Collections\Common\KeyTransformer\DefaultKeyTransformer;
use Rekalogika\Domain\Collections\Common\KeyTransformer\KeyTransformer;
Expand Down Expand Up @@ -68,24 +67,5 @@ private function __construct()
/**
* @var null|\Closure(): CountStrategy
*/
private static ?\Closure $defaultCountStrategy = null;

/**
* @param \Closure(): CountStrategy $defaultCountStrategy
*/
public static function setDefaultCountStrategy(\Closure $defaultCountStrategy): void
{
self::$defaultCountStrategy = $defaultCountStrategy;
}

public static function getDefaultCountStrategy(): CountStrategy
{
if (self::$defaultCountStrategy === null) {
$countStrategy = fn (): CountStrategy => new ConditionalDelegatedCountStrategy();
} else {
$countStrategy = self::$defaultCountStrategy;
}

return $countStrategy();
}
public static ?\Closure $defaultCountStrategy = null;
}
39 changes: 39 additions & 0 deletions packages/collections-common/src/Internal/CountStrategyUtil.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

/*
* This file is part of rekalogika/collections package.
*
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/

namespace Rekalogika\Domain\Collections\Common\Internal;

use Rekalogika\Domain\Collections\Common\Configuration;
use Rekalogika\Domain\Collections\Common\Count\ConditionalDelegatedCountStrategy;
use Rekalogika\Domain\Collections\Common\Count\CountStrategy;

/**
* @internal
*/
final class CountStrategyUtil
{
private function __construct()
{
}

public static function getDefaultCountStrategy(): CountStrategy
{
if (Configuration::$defaultCountStrategy === null) {
$countStrategy = fn (): CountStrategy => new ConditionalDelegatedCountStrategy();
} else {
$countStrategy = Configuration::$defaultCountStrategy;
}

return $countStrategy();
}
}
6 changes: 3 additions & 3 deletions packages/collections-common/src/Trait/RefreshCountTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

namespace Rekalogika\Domain\Collections\Common\Trait;

use Rekalogika\Domain\Collections\Common\Configuration;
use Rekalogika\Domain\Collections\Common\Count\CountStrategy;
use Rekalogika\Domain\Collections\Common\Exception\InvalidCountException;
use Rekalogika\Domain\Collections\Common\Internal\CountStrategyUtil;

trait RefreshCountTrait
{
Expand All @@ -27,7 +27,7 @@ abstract private function getUnderlyingCountable(): ?\Countable;
*/
private function getCount(): int
{
$countStrategy = $this->getCountStrategy() ?? Configuration::getDefaultCountStrategy();
$countStrategy = $this->getCountStrategy() ?? CountStrategyUtil::getDefaultCountStrategy();

$result = $countStrategy->getCount($this->getUnderlyingCountable());

Expand All @@ -40,7 +40,7 @@ private function getCount(): int

final public function refreshCount(): void
{
$countStrategy = $this->getCountStrategy() ?? Configuration::getDefaultCountStrategy();
$countStrategy = $this->getCountStrategy() ?? CountStrategyUtil::getDefaultCountStrategy();

$realCount = \count($this->getUnderlyingCountable());
$countStrategy->setCount($this->getUnderlyingCountable(), $realCount);
Expand Down

0 comments on commit 3512eac

Please sign in to comment.