You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
2.0 KiB
59 lines
2.0 KiB
<?php |
|
|
|
declare(strict_types=1); |
|
|
|
namespace AutoStore\Application\Commands; |
|
|
|
use AutoStore\Application\Interfaces\IItemRepository; |
|
use AutoStore\Application\Interfaces\IOrderService; |
|
use AutoStore\Application\Interfaces\ITimeProvider; |
|
use AutoStore\Domain\Policies\ItemExpirationPolicy; |
|
use AutoStore\Application\Exceptions\ApplicationException; |
|
use AutoStore\Domain\Exceptions\DomainException; |
|
use Psr\Log\LoggerInterface; |
|
|
|
class HandleExpiredItems |
|
{ |
|
private IItemRepository $itemRepository; |
|
private IOrderService $orderService; |
|
private ITimeProvider $timeProvider; |
|
private ItemExpirationPolicy $expirationPolicy; |
|
private LoggerInterface $logger; |
|
|
|
public function __construct( |
|
IItemRepository $itemRepository, |
|
IOrderService $orderService, |
|
ITimeProvider $timeProvider, |
|
LoggerInterface $logger |
|
) { |
|
$this->itemRepository = $itemRepository; |
|
$this->orderService = $orderService; |
|
$this->timeProvider = $timeProvider; |
|
$this->expirationPolicy = new ItemExpirationPolicy(); |
|
$this->logger = $logger; |
|
} |
|
|
|
public function execute(): void |
|
{ |
|
try { |
|
$currentTime = $this->timeProvider->now(); |
|
$items = $this->itemRepository->findExpired(); |
|
|
|
foreach ($items as $item) { |
|
$this->expirationPolicy->checkExpiration($item, $currentTime); |
|
|
|
if ($item->isExpired() && !$item->isOrdered()) { |
|
try { |
|
$this->orderService->orderItem($item); |
|
$item->markAsOrdered(); |
|
$this->itemRepository->save($item); |
|
} catch (\Exception $e) { |
|
$this->logger->error('Failed to place order for expired item ' . $item->getId() . ': ' . $e->getMessage()); |
|
} |
|
} |
|
} |
|
} catch (DomainException $e) { |
|
throw new ApplicationException('Failed to handle expired items: ' . $e->getMessage(), 0, $e); |
|
} |
|
} |
|
} |