File size: 1,781 Bytes
cf14c12 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | <?php
declare(strict_types=1);
namespace Billing\Application\CommandHandlers;
use Billing\Application\Commands\ListOrdersCommand;
use Billing\Domain\Entities\OrderEntity;
use Billing\Domain\Exceptions\OrderNotFoundException;
use Billing\Domain\Repositories\OrderRepositoryInterface;
use Shared\Domain\ValueObjects\CursorDirection;
use Traversable;
class ListOrdersCommandHandler
{
public function __construct(
private OrderRepositoryInterface $repo,
) {
}
/**
* @return Traversable<OrderEntity>
* @throws OrderNotFoundException
*/
public function handle(ListOrdersCommand $cmd): Traversable
{
$cursor = $cmd->cursor
? $this->repo->ofId($cmd->cursor)
: null;
$subs = $this->repo;
if ($cmd->sortDirection) {
$subs = $subs->sort($cmd->sortDirection, $cmd->sortParameter);
}
if ($cmd->status) {
$subs = $subs->filterByStatus($cmd->status);
}
if ($cmd->workspace) {
$subs = $subs->filterByWorkspace($cmd->workspace);
}
if ($cmd->plan) {
$subs = $subs->filterByPlan($cmd->plan);
}
if ($cmd->planSnapshot) {
$subs = $subs->filterByPlanSnapshot($cmd->planSnapshot);
}
if ($cmd->billingCycle) {
$subs = $subs->filterByBillingCycle($cmd->billingCycle);
}
if ($cmd->maxResults) {
$subs = $subs->setMaxResults($cmd->maxResults);
}
if ($cursor) {
if ($cmd->cursorDirection == CursorDirection::ENDING_BEFORE) {
return $subs = $subs->endingBefore($cursor);
}
return $subs->startingAfter($cursor);
}
return $subs;
}
}
|