diff --git a/src/Command/DownloadCommand.php b/src/Command/DownloadCommand.php index 4808235..1645d0f 100644 --- a/src/Command/DownloadCommand.php +++ b/src/Command/DownloadCommand.php @@ -2,8 +2,6 @@ namespace App\Command; -use App\Constants; -use App\Entity\Episode; use App\Repository\PodcastRepository; use App\Service\DownloadService; use Doctrine\ORM\EntityManagerInterface; @@ -24,7 +22,7 @@ class DownloadCommand extends Command { public function __construct( protected DownloadService $downloadService, - protected EntityManagerInterface $entityManager, + protected EntityManagerInterface $em, protected PodcastRepository $podcastRepository, protected ParameterBagInterface $parameterBag, ) { @@ -55,30 +53,22 @@ class DownloadCommand extends Command return Command::FAILURE; } - $filepath = $this->downloadService->download($url); - if (false === $filepath) { + $episode = $this->downloadService->download($url); + if (false === $episode) { $io->error('Download failed.'); return Command::FAILURE; } - $filename = basename($filepath); - $publicFilepath = - $this->parameterBag->get('kernel.project_dir') - .'/' - .Constants::BASE_PUBLIC_DIR - .Constants::FILES_BASE_PATH - .$filename; - rename($filepath, $publicFilepath); - - $episode = new Episode(); - $episode->setTitle($input->getOption('title') ?: $filename); - $episode->setDescription($input->getOption('description')); - $episode->setAudioFilename($filename); $episode->setPodcast($podcast); - $episode->setPublicationDate(date_create()); - $this->entityManager->persist($episode); - $this->entityManager->flush(); + $episode->setDescription($input->getOption('description')); + // Override only if the option has been passed, because the download service sets a default title. + if (($title = $input->getOption('title')) !== null) { + $episode->setTitle($title); + } + + $this->em->persist($episode); + $this->em->flush(); $io->success("New Episode '{$episode->getTitle()}' added."); diff --git a/src/Entity/Episode.php b/src/Entity/Episode.php index d06372f..58cc49f 100644 --- a/src/Entity/Episode.php +++ b/src/Entity/Episode.php @@ -3,7 +3,6 @@ namespace App\Entity; use App\Constants; -use App\Entity\Podcast; use App\Repository\EpisodeRepository; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; @@ -99,10 +98,10 @@ class Episode public function getAudioUrl(): ?string { - if ($this->audioFilename === null) { + if (null === $this->audioFilename) { return null; } - return Constants::FILES_BASE_PATH . $this->audioFilename; + return Constants::FILES_BASE_PATH.$this->audioFilename; } } diff --git a/src/Service/DownloadService.php b/src/Service/DownloadService.php index 1e580b3..4dc7786 100644 --- a/src/Service/DownloadService.php +++ b/src/Service/DownloadService.php @@ -2,21 +2,46 @@ namespace App\Service; +use App\Constants; +use App\Entity\Episode; use Psr\Log\LoggerInterface; +use Symfony\Component\DependencyInjection\Attribute\Autowire; +/** Generate episodes from external sources. */ class DownloadService { public function __construct( protected LoggerInterface $logger, protected YoutubeService $youtubeService, - ) {} + #[Autowire('%kernel.project_dir%')] protected string $projectDir, + ) { + } /** - * Download the audio from this URL and return its filepath. + * Download the audio from this URL and save the corresponding episode. */ - public function download(string $url): string|false + public function download(string $url): Episode|false { // For now assume every URL is a Youtube URL. - return $this->youtubeService->download($url); + $filepath = $this->youtubeService->download($url); + if (false === $filepath) { + return false; + } + + $filename = basename($filepath); + $publicFilepath = + $this->projectDir + .'/' + .Constants::BASE_PUBLIC_DIR + .Constants::FILES_BASE_PATH + .$filename; + rename($filepath, $publicFilepath); + + $episode = new Episode(); + $episode->setTitle($filename); + $episode->setAudioFilename($filename); + $episode->setPublicationDate(date_create()); + + return $episode; } } diff --git a/src/Service/YoutubeService.php b/src/Service/YoutubeService.php index a852449..748b046 100644 --- a/src/Service/YoutubeService.php +++ b/src/Service/YoutubeService.php @@ -19,7 +19,7 @@ class YoutubeService '-x', '-O', 'after_move:filepath', '--restrict-filenames', '-P', sys_get_temp_dir(), - escapeshellarg($url), + $url, ]); try { $process->mustRun();