src/Command/DraftManageCommand.php line 20

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: Grigor
  5.  * Date: 12/24/19
  6.  * Time: 12:35 PM
  7.  */
  8. namespace App\Command;
  9. use App\Entity\Draft;
  10. use App\Entity\DraftFile;
  11. use Doctrine\ORM\EntityManager;
  12. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  13. use Symfony\Component\Console\Command\LockableTrait;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. use Symfony\Component\Console\Style\SymfonyStyle;
  17. class DraftManageCommand extends ContainerAwareCommand
  18. {
  19.     use LockableTrait;
  20.     protected static $defaultName 'state:delete-published-drafts';
  21.     /** @var EntityManager $em */
  22.     private $em;
  23.     /** @var SymfonyStyle $io */
  24.     private $io;
  25.     protected function configure()
  26.     {
  27.         $this->setDescription('Delete drafts published a day ago');
  28.     }
  29.     protected function initialize(InputInterface $inputOutputInterface $output)
  30.     {
  31.         parent::initialize($input$output);
  32.         $this->em $this->getContainer()->get('doctrine.orm.entity_manager');
  33.         // DISABLE SQL LOGGING, CAUSE IT CAUSES MEMORY SHORTAGE on large inserts
  34.         $this->em->getConnection()->getConfiguration()->setSQLLogger(null);
  35.         $this->io = new SymfonyStyle($input$output);
  36.     }
  37.     /**
  38.      * @param InputInterface $input
  39.      * @param OutputInterface $output
  40.      * @return int|null
  41.      * @throws \Exception
  42.      */
  43.     protected function execute(InputInterface $inputOutputInterface $output)
  44.     {
  45.         if (!$this->lock()) {
  46.             $output->writeln('The command is already running in another process.');
  47.             return 0;
  48.         }
  49.         /**
  50.          * @var Draft[] $drafts
  51.          */
  52.         $drafts $this->em->getRepository(Draft::class)->getPublishedDrafts();
  53.         if ($drafts) {
  54.             foreach ($drafts as $draft) {
  55.                 //  get local files & delete
  56.                 /**
  57.                  * @var DraftFile[] $draftFiles
  58.                  */
  59.                 $draftFiles $this->em->getRepository(DraftFile::class)->findBy(['draft' => $draft]);
  60.                 if ($draftFiles) {
  61.                     foreach ($draftFiles as $draftFile) {
  62.                         $uri $draftFile->getUri();
  63.                         $fileUsages $this->em->getRepository(DraftFile::class)->getFileUsagesWithException($uri$draft);
  64.                         if (!$fileUsages && file_exists(getcwd() . '/public/' $draftFile->getPath())) {
  65.                             unlink(getcwd() . '/public/' $draftFile->getPath());
  66.                         }
  67.                     }
  68.                 }
  69.                 $this->em->remove($draft);
  70.                 $this->em->flush();
  71.             }
  72.         }
  73.         $this->io->success('Done');
  74.         $this->release();
  75.         return null;
  76.     }
  77. }