src/Event/CountPaidOrder/CountPaidOrderSubscriber.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\Event\CountPaidOrder;
  3. use App\Entity\CountPaidOrder\CountPaidOrder;
  4. use App\Entity\Order\Order;
  5. use App\Entity\User\User;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Psr\Log\LoggerInterface;
  9. class  CountPaidOrderSubscriber implements EventSubscriberInterface
  10. {
  11.           private $_em;
  12.           private $_logger;
  13.           public function __construct(EntityManagerInterface $emLoggerInterface $logger)
  14.           {
  15.                     $this->_em $em;
  16.                     $this->_logger $logger;
  17.           }
  18.           public static function getSubscribedEvents()
  19.           {
  20.                     return [
  21.                         CountPaidOrderEvent::NAME => 'countPaidOrder'
  22.                     ];
  23.           }
  24.           public function countPaidOrder(CountPaidOrderEvent $event)
  25.           {
  26.                     $user_email $event->getUserEmail();
  27.                     $countPaidOrders $this->_em->getRepository(CountPaidOrder::class)->findOneBy(['email' => $user_email]);
  28.                     $user $this->_em->getRepository(User::class)->findOneBy(['email' => $user_email]);
  29.                     
  30.                     if (is_null($user)) {
  31.                         $this->_logger->critical('No email found', [
  32.                             'email' => $user_email,
  33.                         ]);
  34.                     
  35.                         return false;
  36.                     }
  37.                     $count $this->_em->getRepository(Order::class)->findBy([
  38.                         'ownerEmail' => $user_email,
  39.                         'isPaid' => 1
  40.                     ]);
  41.                     $orders $this->_em->getRepository(Order::class)->findBy(
  42.                         ['ownerEmail' => $user_email]
  43.                     );
  44.                     if (is_null($countPaidOrders)) {
  45.                               $countPaidOrders = new CountPaidOrder();
  46.                               $countPaidOrders->setEmail($user_email);
  47.                               $countPaidOrders->setCount(count($count));
  48.                               $countPaidOrders->setCreatedAt(new \DateTime());
  49.                               $this->_em->persist($countPaidOrders);
  50.                     } else {
  51.                               $countPaidOrders->setCount((int)$countPaidOrders->getCount() + 1);
  52.                               $countPaidOrders->setUpdatedAt(new \DateTime());
  53.                     }
  54.                     $user->setCountPaidOrder($countPaidOrders);
  55.                     $this->_em->flush();
  56.                     foreach ($orders as $order) {
  57.                               if ($order->getCountPaidOrder() == null) {
  58.                                         $order->setCountPaidOrder($countPaidOrders);
  59.                                         $this->_em->flush();
  60.                               }
  61.                     }
  62.           }
  63. }