*/ readonly class DownloadMediaHandler implements HandlerInterface { public function __construct( private MessageBusInterface $bus, private DownloaderInterface $downloader, private DownloadRepository $downloadRepository, private UserRepository $userRepository, ) {} public function handle(CommandInterface $command): ResultInterface { $user = $this->userRepository->find($command->userId); $this->bus->dispatch(new AddEventLogCommand( $user, DownloadEvents::DOWNLOAD_STARTED->type(), DownloadEvents::DOWNLOAD_STARTED->message(), (array) $command )); if (null === $command->downloadId) { $download = $this->downloadRepository->insert( $user, $command->url, $command->title, $command->filename, $command->imdbId, $command->mediaType, "" ); } else { $download = $this->downloadRepository->find($command->downloadId); } try { if ($download->getStatus() !== 'Paused') { $this->downloadRepository->updateStatus($download->getId(), 'In Progress'); } $this->downloader->download( $command->mediaType, $command->title, $command->url, $download->getId() ); if ($download->getStatus() !== 'Paused') { $this->downloadRepository->updateStatus($download->getId(), 'Complete'); } } catch (\Throwable $exception) { throw new UnrecoverableMessageHandlingException($exception->getMessage(), 500); } $this->bus->dispatch(new AddEventLogCommand( $user, DownloadEvents::DOWNLOAD_FINISHED->type(), DownloadEvents::DOWNLOAD_FINISHED->message(), (array) $command )); return new DownloadMediaResult(200, "Success."); } }