listFilesToDelete(); $total = count($files); $this->ui->print($total . " temporary files to be removed"); $this->ui->print("> Printing the first and last 50 :"); $i = 0; foreach ($files as $file) { $i++; if ($i > 50 && ($total - $i) > 50) { continue; } if (($total - $i) === 50) { $this->ui->print(' (...)'); } $this->ui->print(' * ' . $file->getPath()); } $this->purgeDb(false); } /** * Proceed to the deletion of the files and the purge of the DB */ public function execute(): void { $files = $this->listFilesToDelete(); $this->deleteFiles($files); $this->purgeDb(); } /** * List the files to delete in the DB * * @return Collection * @throws \Exception */ private function listFilesToDelete(): Collection { $maxDate = new \DateTime(self::DELETE_OLDER_THAN . ' days ago'); $this->ui->print('List temporary files created before ' . $maxDate->format('c')); $criteria = new Criteria(); $criteria->where( Criteria::expr()?->andX( Criteria::expr()?->eq('isTemporaryFile', 1), Criteria::expr()?->orX( Criteria::expr()?->lt('createDate', $maxDate), Criteria::expr()?->isNull('createDate') ) ) ); return $this->fileRepository->matching($criteria); } /** * Delete the files * * @param Collection $files */ private function deleteFiles(Collection $files): void { $total = count($files); $this->ui->print($total . " temporary files to be removed"); $author = $this->accessRepository->find(self::AUTHOR); if ($author === null) { throw new \RuntimeException('Access ' . self::AUTHOR . ' could not be found'); } $this->ui->print('Deleting files...'); $i = 0; $this->ui->progress(0, $total); foreach ($files as $file) { try { $i++; $this->ui->progress($i, $total); $this->storage->delete($file, $author); } catch (\RuntimeException $e) { $this->ui->print('ERROR : ' . $e->getMessage()); } } $this->ui->print($i . ' files deleted'); } /** * Purge the DB from temporary file records older than N days * * @param bool $commit * @throws \Exception */ private function purgeDb(bool $commit = true): void { $maxDate = new \DateTime(self::PURGE_RECORDS_OLDER_THAN . ' days ago'); $this->ui->print('Purge DB from records of files deleted before ' . $maxDate->format('c')); $this->em->beginTransaction(); $queryBuilder = $this->em->createQueryBuilder(); $q = $queryBuilder ->delete('File', 'f') ->where($queryBuilder->expr()->eq('f.isTemporaryFile', 1)) ->andWhere($queryBuilder->expr()->lt('f.updateDate', ':maxDate')) ->setParameter('maxDate', $maxDate) ->getQuery(); $purged = $q->getResult(); if ($commit) { $this->em->commit(); $this->ui->print('DB purged - ' . $purged . ' records permanently deleted'); return; } $this->em->rollback(); $this->ui->print('DB purged - ' . $purged . ' records would be permanently deleted'); } }