src/EventSubscriber/DeliverySubscriber.php line 45

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Delivery;
  4. use App\Repository\DeliveryMethodRepository;
  5. use App\Repository\DeliveryRepository;
  6. use App\Service\ShopifyAPI;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Exception;
  9. use Google\Client;
  10. use Google\Service\Calendar;
  11. use Google_Service_Exception;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpKernel\Event\RequestEvent;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  17. use Symfony\Component\Mailer\MailerInterface;
  18. use Symfony\Component\Mime\Address;
  19. use Symfony\Component\Mime\Email;
  20. class DeliverySubscriber extends AbstractController implements EventSubscriberInterface
  21. {
  22.     private $api;
  23.     private $em;
  24.     private $methodRepository;
  25.     private $deliveryRepository;
  26.     private $mailer;
  27.     public function __construct(ShopifyAPI $shopifyAPIEntityManagerInterface $entityManagerDeliveryMethodRepository $deliveryMethodRepositoryDeliveryRepository $deliveryRepositoryMailerInterface $mailer)
  28.     {
  29.         $this->api $shopifyAPI;
  30.         $this->em $entityManager;
  31.         $this->methodRepository $deliveryMethodRepository;
  32.         $this->deliveryRepository $deliveryRepository;
  33.         $this->mailer $mailer;
  34.     }
  35.     /**
  36.      * @throws TransportExceptionInterface
  37.      * @throws \Google\Exception
  38.      */
  39.     public function onKernelRequest(RequestEvent $event)
  40.     {
  41.         if($event->getRequest()->getPathInfo() === '/admin/delivery'){
  42.             $orders $this->api->shopifyRequest('orders.json', [
  43.                 'financial_status'   => 'paid',
  44.             ]);
  45.             foreach ($orders as $order){
  46.                 foreach ($order as $item){
  47.                     $delivery $this->deliveryRepository->findOneBy(['shopify_id' => $item->id]);
  48.                     if (((!empty($item->shipping_lines) || $item->line_items[0]->title === 'Essayage virtuel') && $delivery === null)){
  49.                         $delivery = new Delivery();
  50.                         $delivery->setFullName($item->shipping_address->name ?? $item->billing_address->name);
  51.                         $delivery->setFirstName($item->shipping_address->first_name ?? $item->billing_address->first_name);
  52.                         $delivery->setLastName($item->shipping_address->last_name ?? $item->billing_address->last_name);
  53.                         $delivery->setEmail($item->customer->email);
  54.                         $delivery->setTel($item->shipping_address->phone ?? $item->billing_address->phone);
  55.                         $delivery->setLastUpdate(new \DateTime());
  56.                         $delivery->setCreationDate(new \DateTime());
  57.                         $delivery->setSent(!($item->fulfillment_status === null));
  58.                         $delivery->setShopifyId($item->id);
  59.                         foreach ($item->line_items as $product){
  60.                             if($delivery->getProduct() === null || $delivery->getProduct() === ''){
  61.                                 $delivery->setProduct(ucfirst($product->name));
  62.                             }else{
  63.                                 $delivery->setProduct($delivery->getProduct().' + '.ucfirst($product->name));
  64.                             }
  65.                         }
  66.                         $this->em->persist($delivery);
  67.                         $delivery->setAddress(isset($item->shipping_address->address1) ? $item->shipping_address->address1.', '.$item->shipping_address->city.', '.$item->shipping_address->zip $item->billing_address->address1.', '.$item->billing_address->city.', '.$item->billing_address->zip);
  68.                         $delivery->setDeliveryMethod($this->methodRepository->findOneBy(['name' => $item->shipping_lines[0]->code ?? 'Chronopost']));
  69.                         // if($delivery->isSent() === false){
  70.                         //     $deliveryConfirmation = (new Email())
  71.                         //         ->from(new Address('contact@lamusee.com', 'LAMUSEE'))
  72.                         //         ->subject('L\'Amusee - Nouvelle commande Shopify - '.$delivery->getFullName())
  73.                         //         ->to('contact@lamusee.com')
  74.                         //         ->html($this->render('admin/delivery/email/new_delivery_shopify.html.twig', ['delivery' => $delivery, 'number' => $item->name, 'ADMIN_URL_PREFIX' => $this->getParameter('url_prefix')])->getContent());
  75.                         //     $this->mailer->send($deliveryConfirmation);
  76.                         // }
  77.                     }
  78.                     //Ajouter commentaire sur le elseif pour désynchroniser le traitement des commandes Shopify
  79.                     elseif($delivery){
  80.                         $previous $delivery->isSent();
  81.                         $delivery->setSent(!($item->fulfillment_status === null));
  82.                         $delivery->setLastUpdate(new \DateTime());
  83.                         if($previous !== !($item->fulfillment_status === null)){
  84.                             $client = new Client();
  85.                             $client->setAuthConfig('../config/googleConfig.json');
  86.                             $id str_replace('-'''$delivery->getId()->jsonSerialize());
  87.                             $client->addScope(Calendar::CALENDAR_EVENTS);
  88.                             $calendarService = new Calendar($client);
  89.                             $calendarId 'o01cfgbnobksvm0kdnvo06jfs0@group.calendar.google.com';
  90.                             try {
  91.                                 $event $calendarService->events->get($calendarId$id);
  92.                                 if($event){
  93.                                     $event->setColorId($delivery->isSent() ? '4' null);
  94.                                     $calendarService->events->update($calendarId$event->getId(), $event);
  95.                                 }
  96.                             } catch (Google_Service_Exception $e){
  97.                             }
  98.                         }
  99.                     }
  100.                     try {
  101.                         $this->em->persist($delivery);
  102.                         $this->em->flush();
  103.                     } catch (Exception $exception) {
  104.                         dd($exception->getMessage());
  105.                     }
  106.                 }
  107.             }
  108.         }
  109.     }
  110.     public static function getSubscribedEvents()
  111.     {
  112.         return [
  113.             // On doit définir une priorité élevée
  114.             KernelEvents::REQUEST => [['onKernelRequest'20]],
  115.         ];
  116.     }
  117. }