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); } } }