Skip to content

Commit

Permalink
refactor: refresh count (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi authored Jun 29, 2024
1 parent 22c2857 commit b1e22c1
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* refactor: refactor counting strategy
* fix: use closure for counting, defer counting until it is asked
* refactor: refresh count

## 0.4.0

Expand Down
10 changes: 0 additions & 10 deletions packages/collections-common/src/Trait/CountableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,4 @@ final public function count(): int

throw new InvalidCountException('Invalid count');
}

/**
* Not part of the Countable interface. Used by the caller to refresh the
* stored count by querying the collection.
*/
final public function refreshCount(): void
{
$realCount = \count($this->getUnderlyingCountable());
$this->getCountStrategy()->setCount($this->getUnderlyingCountable(), $realCount);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ trait MinimalReadableRecollectionTrait
*/
use PageableTrait;

use RefreshableCountTrait;

/**
* @return ReadableCollection<TKey,T>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ trait ReadableRecollectionTrait
*/
use ReadableCollectionTrait;

use RefreshableCountTrait;

/**
* @param TKey $key
* @return T
Expand Down
28 changes: 28 additions & 0 deletions packages/collections-common/src/Trait/RefreshableCountTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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\Trait;

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

trait RefreshableCountTrait
{
abstract private function getCountStrategy(): CountStrategy;
abstract private function getUnderlyingCountable(): ?\Countable;

final public function refreshCount(): void
{
$realCount = \count($this->getUnderlyingCountable());
$this->getCountStrategy()->setCount($this->getUnderlyingCountable(), $realCount);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* @template-covariant T
* @extends PageableInterface<TKey,T>
*/
interface MinimalReadableRecollection extends PageableInterface
interface MinimalReadableRecollection extends PageableInterface, RefreshableCount
{
/**
* @template TMaybeContained
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @extends PageableInterface<TKey,T>
* @extends ReadableCollection<TKey,T>
*/
interface ReadableRecollection extends PageableInterface, ReadableCollection
interface ReadableRecollection extends PageableInterface, ReadableCollection, RefreshableCount
{
/**
* @param TKey $key
Expand Down
2 changes: 1 addition & 1 deletion packages/collections-contracts/src/Recollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
* @extends Collection<TKey,T>
* @extends ReadableRecollection<TKey,T>
*/
interface Recollection extends Collection, ReadableRecollection, RefreshableCountable
interface Recollection extends Collection, ReadableRecollection
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Rekalogika\Contracts\Collections;

interface RefreshableCountable extends \Countable
interface RefreshableCount
{
public function refreshCount(): void;
}
5 changes: 5 additions & 0 deletions packages/collections-orm/src/AbstractMinimalRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ private function getClass(): string
return $this->class;
}

private function getCountStrategy(): CountStrategy
{
return $this->count;
}

/**
* @param int<1,max> $itemsPerPage
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Doctrine\ORM\EntityManagerInterface;
use Rekalogika\Contracts\Collections\Exception\NotFoundException;
use Rekalogika\Domain\Collections\Common\Trait\PageableTrait;
use Rekalogika\Domain\Collections\Common\Trait\RefreshableCountTrait;

/**
* @template TKey of array-key
Expand All @@ -30,6 +31,8 @@ trait MinimalReadableRepositoryTrait
*/
use PageableTrait;

use RefreshableCountTrait;

abstract private function getEntityManager(): EntityManagerInterface;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Rekalogika\Collections\ORM\Trait;

use Rekalogika\Domain\Collections\Common\Trait\ReadableRecollectionTrait;
use Rekalogika\Domain\Collections\Common\Trait\RefreshableCountTrait;

/**
* @template TKey of array-key
Expand All @@ -35,6 +36,8 @@ trait ReadableRepositoryTrait
MinimalReadableRepositoryTrait::getOrFail insteadof ReadableRecollectionTrait;
}

use RefreshableCountTrait;

/**
* @return array<TKey,T>
*/
Expand Down

0 comments on commit b1e22c1

Please sign in to comment.