From 3a8d3334fad76ab17b8a9d94831c8dc2e01785f2 Mon Sep 17 00:00:00 2001 From: Stakovicz Date: Tue, 11 Apr 2023 20:39:04 +0200 Subject: [PATCH] feat (#1247): Speakers Expenses Files CleanCommand --- app/config/services.yml | 1 + .../SpeakersExpensesFilesCleanCommand.php | 26 +++++++++ .../Model/Repository/EventRepository.php | 14 +++++ .../SpeakerInfos/SpeakersExpensesStorage.php | 53 ++++++++++++++++++- 4 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 sources/AppBundle/Command/SpeakersExpensesFilesCleanCommand.php diff --git a/app/config/services.yml b/app/config/services.yml index 74ebfbfd8..6fc5a5d85 100644 --- a/app/config/services.yml +++ b/app/config/services.yml @@ -194,6 +194,7 @@ services: arguments: - "%kernel.root_dir%/../htdocs/uploads/speaker_expenses" - "/uploads/speaker_expenses" + - '@AppBundle\Event\Model\Repository\EventRepository' AppBundle\Event\Form\SpeakerFormDataFactory: autowire: true diff --git a/sources/AppBundle/Command/SpeakersExpensesFilesCleanCommand.php b/sources/AppBundle/Command/SpeakersExpensesFilesCleanCommand.php new file mode 100644 index 000000000..6312cdb29 --- /dev/null +++ b/sources/AppBundle/Command/SpeakersExpensesFilesCleanCommand.php @@ -0,0 +1,26 @@ +setName('speaker:expenses-files-clean') + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $container = $this->getContainer(); + $container->get(SpeakersExpensesStorage::class) + ->setLogger($container->get('logger')) + ->cleanFiles(); + } +} diff --git a/sources/AppBundle/Event/Model/Repository/EventRepository.php b/sources/AppBundle/Event/Model/Repository/EventRepository.php index a9a30b85f..dc414d961 100644 --- a/sources/AppBundle/Event/Model/Repository/EventRepository.php +++ b/sources/AppBundle/Event/Model/Repository/EventRepository.php @@ -172,6 +172,20 @@ public function getPreviousEvents($eventCount) return $query->query($this->getCollection(new HydratorSingleObject())); } + /** + * @param \DateTimeInterface $beforeDatetime + * @return \CCMBenchmark\Ting\Repository\CollectionInterface + * @throws \CCMBenchmark\Ting\Exception + * @throws \CCMBenchmark\Ting\Query\QueryException + */ + public function getPreviousEventsBefore($beforeDatetime) + { + $query = $this->getQuery('SELECT * FROM afup_forum WHERE date_debut <= :before_date ORDER BY date_debut DESC'); + $query->setParams(['before_date' => $beforeDatetime->format('Y-m-d')]); + + return $query->query($this->getCollection(new HydratorSingleObject())); + } + /** * @param $path * diff --git a/sources/AppBundle/SpeakerInfos/SpeakersExpensesStorage.php b/sources/AppBundle/SpeakerInfos/SpeakersExpensesStorage.php index 1f79ba23f..dfe9b34fc 100644 --- a/sources/AppBundle/SpeakerInfos/SpeakersExpensesStorage.php +++ b/sources/AppBundle/SpeakerInfos/SpeakersExpensesStorage.php @@ -3,7 +3,10 @@ namespace AppBundle\SpeakerInfos; +use AppBundle\Event\Model\Event; +use AppBundle\Event\Model\Repository\EventRepository; use AppBundle\Event\Model\Speaker; +use Psr\Log\LoggerInterface; use Symfony\Component\Filesystem\Exception\IOException; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Finder\Finder; @@ -15,12 +18,17 @@ class SpeakersExpensesStorage private $basePath; private $publicPath; private $filesystem; + /** @var EventRepository */ + private $eventRepository; + /** @var LoggerInterface */ + private $logger; - public function __construct($basePath, $publicPath) + public function __construct($basePath, $publicPath, $eventRepository) { $this->basePath = $basePath; $this->publicPath = $publicPath; $this->filesystem = new Filesystem(); + $this->eventRepository = $eventRepository; } public function store(UploadedFile $file, Speaker $speaker) @@ -63,6 +71,49 @@ public function getFiles(Speaker $speaker) return $files; } + /** + * @param LoggerInterface $logger + * + * @return $this + */ + public function setLogger(LoggerInterface $logger) + { + $this->logger = $logger; + + return $this; + } + + public function cleanFiles($duration = 'P12M') + { + $beforeDate = new \DateTime(); + $beforeDate->sub(new \DateInterval($duration)); + + $this->logInfo(sprintf('Speakers Expenses Storages clean before "%s"', $beforeDate->format('Y-m-d'))); + + $events = $this->eventRepository->getPreviousEventsBefore($beforeDate); + + /** @var Event $event */ + foreach ($events as $event) { + $this->logInfo(sprintf('Event "%s" #%d [%s]: ', $event->getTitle(), $event->getId(), $event->getDateStart()->format('Y-m-d'))); + + $directory = $this->basePath . '/' . $event->getId(); + + if ($this->filesystem->exists($directory)) { + $this->filesystem->remove($directory); + $this->logInfo(sprintf('Removing "%s" directory OK', $directory)); + } else { + $this->logInfo(sprintf('Directory "%s" does not exists', $directory)); + } + } + } + + private function logInfo($message) + { + if ($this->logger) { + $this->logger->info($message); + } + } + private function createDirectory($directory) { try {