Skip to content

Commit

Permalink
feat (afup#1247): Speakers Expenses Files CleanCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
Stakovicz committed Apr 11, 2023
1 parent 893d7d4 commit 3a8d333
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions sources/AppBundle/Command/SpeakersExpensesFilesCleanCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace AppBundle\Command;

use AppBundle\SpeakerInfos\SpeakersExpensesStorage;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class SpeakersExpensesFilesCleanCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('speaker:expenses-files-clean')
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$container = $this->getContainer();
$container->get(SpeakersExpensesStorage::class)
->setLogger($container->get('logger'))
->cleanFiles();
}
}
14 changes: 14 additions & 0 deletions sources/AppBundle/Event/Model/Repository/EventRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
53 changes: 52 additions & 1 deletion sources/AppBundle/SpeakerInfos/SpeakersExpensesStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 3a8d333

Please sign in to comment.