src/Eccube/Command/DeleteCartsCommand.php line 141

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Eccube\Command;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Eccube\Repository\CartRepository;
  15. use Symfony\Component\Console\Command\Command;
  16. use Symfony\Component\Console\Input\InputArgument;
  17. use Symfony\Component\Console\Input\InputInterface;
  18. use Symfony\Component\Console\Output\OutputInterface;
  19. use Symfony\Component\Console\Style\SymfonyStyle;
  20. use Symfony\Component\DependencyInjection\ContainerInterface;
  21. class DeleteCartsCommand extends Command
  22. {
  23.     protected static $defaultName 'eccube:delete-carts';
  24.     /**
  25.      * @var ContainerInterface
  26.      */
  27.     protected $container;
  28.     /**
  29.      * @var SymfonyStyle
  30.      */
  31.     protected $io;
  32.     /**
  33.      * @var string
  34.      */
  35.     protected $locale;
  36.     /**
  37.      * @var \DateTimeZone
  38.      */
  39.     protected $timezone;
  40.     /**
  41.      * @var \IntlDateFormatter
  42.      */
  43.     protected $formatter;
  44.     /**
  45.      * @var EntityManagerInterface
  46.      */
  47.     protected $entityManager;
  48.     /**
  49.      * @var CartRepository
  50.      */
  51.     private $cartRepository;
  52.     public function __construct(ContainerInterface $containerEntityManagerInterface $entityManagerCartRepository $cartRepository)
  53.     {
  54.         parent::__construct();
  55.         $this->container $container;
  56.         $this->entityManager $entityManager;
  57.         $this->cartRepository $cartRepository;
  58.     }
  59.     protected function configure()
  60.     {
  61.         $this
  62.             ->setDescription('Delete Carts from the database')
  63.             ->addArgument('date'InputArgument::REQUIRED'Deletes carts before the specified date');
  64.     }
  65.     protected function interact(InputInterface $inputOutputInterface $output)
  66.     {
  67.         if (null !== $input->getArgument('date')) {
  68.             return;
  69.         }
  70.         $pattern $this->formatter->getPattern();
  71.         $this->io->title('Delete Cart Command Interactive Wizard');
  72.         $this->io->text([
  73.             'If you prefer to not use this interactive wizard, provide the',
  74.             'arguments required by this command as follows:',
  75.             '',
  76.             ' $ php bin/console eccube:delete-cart <'.$pattern.'>',
  77.             '',
  78.             'Now we\'ll ask you for the value of all the missing command arguments.',
  79.             '',
  80.         ]);
  81.         $now = new \DateTime('now'$this->timezone);
  82.         $dateStr $this->formatter->format($now->getTimestamp());
  83.         $dateStr $this->io->ask('date'$dateStr);
  84.         $input->setArgument('date'$dateStr);
  85.     }
  86.     protected function initialize(InputInterface $inputOutputInterface $output)
  87.     {
  88.         $this->io = new SymfonyStyle($input$output);
  89.         $this->locale $this->container->getParameter('locale');
  90.         $this->timezone = new \DateTimeZone($this->container->getParameter('timezone'));
  91.         $this->formatter $this->createIntlFormatter();
  92.     }
  93.     protected function execute(InputInterface $inputOutputInterface $output)
  94.     {
  95.         $dateStr $input->getArgument('date');
  96.         $timestamp $this->formatter->parse($dateStr);
  97.         $dateTime = new \DateTime("@$timestamp"$this->timezone);
  98.         $this->deleteCarts($dateTime);
  99.         $this->io->success('Delete carts successful.');
  100.         return 0;
  101.     }
  102.     protected function deleteCarts(\DateTime $dateTime)
  103.     {
  104.         try {
  105.             $this->entityManager->beginTransaction();
  106.             $qb $this->cartRepository->createQueryBuilder('c')
  107.                 ->delete()
  108.                 ->where('c.update_date <= :date')
  109.                 ->setParameter('date'$dateTime);
  110.             $deleteRows $qb->getQuery()->getResult();
  111.             $this->entityManager->flush();
  112.             $this->entityManager->commit();
  113.             $this->io->comment("Deleted ${deleteRows} carts.");
  114.         } catch (\Exception $e) {
  115.             $this->io->error('Failed delete carts. Rollbacked.');
  116.             $this->entityManager->rollback();
  117.         }
  118.         return;
  119.     }
  120.     protected function createIntlFormatter()
  121.     {
  122.         return \IntlDateFormatter::create(
  123.             $this->locale,
  124.             \IntlDateFormatter::MEDIUM,
  125.             \IntlDateFormatter::NONE,
  126.             $this->timezone,
  127.             \IntlDateFormatter::GREGORIAN
  128.         );
  129.     }
  130. }