vendor/roothirsch/shop-bundle/EventSubscriber/Api/EstimateOrderSubscriber.php line 57

Open in your IDE?
  1. <?php
  2. // api/src/EventSubscriber/BookMailSubscriber.php
  3. namespace Roothirsch\ShopBundle\EventSubscriber\Api;
  4. use ApiPlatform\Core\EventListener\EventPriorities;
  5. use Roothirsch\CoreBundle\Messaging\MessagingService;
  6. use Roothirsch\ShopBundle\Entity\Estimate;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Spatie\Browsershot\Browsershot;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  12. final class EstimateOrderSubscriber implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var MessagingService
  16.      */
  17.     private $messagingService;
  18.     /**
  19.      * @var string
  20.      */
  21.     private $cacheDir;
  22.     /**
  23.      * @var TokenStorageInterface
  24.      */
  25.     private $tokenStorage;
  26.     /**
  27.      * @var EntityManagerInterface
  28.      */
  29.     private $entityManager;
  30.     public function __construct(
  31.         MessagingService $messagingService,
  32.         $cacheDir,
  33.         TokenStorageInterface $tokenStorage,
  34.         EntityManagerInterface $entityManager
  35.     ) {
  36.         $this->messagingService $messagingService;
  37.         $this->cacheDir $cacheDir;
  38.         $this->tokenStorage $tokenStorage;
  39.         $this->entityManager $entityManager;
  40.     }
  41.     public static function getSubscribedEvents()
  42.     {
  43.         return [
  44.             KernelEvents::VIEW => ['validate'EventPriorities::POST_VALIDATE],
  45.         ];
  46.     }
  47.     public function validate(\Symfony\Component\HttpKernel\Event\ViewEvent $event)
  48.     {
  49.         if ($event->getRequest()->attributes->get('_route') === 'api_estimates_order_item') {
  50.             /** @var Estimate $estimate */
  51.             $estimate $event->getControllerResult();
  52.             $request $event->getRequest();
  53.             $fileName $request->request->get('filename'date('d.m.Y') . '.pdf');
  54.             // sanitize file name for internet explorer string stuff
  55.             $fileName preg_replace('/[^a-zA-Z0-9\-\._]/'''$fileName);
  56.             $directory bin2hex(random_bytes(24));
  57.             $filePath $this->cacheDir '/rendered-pdf/' $directory '/';
  58.             if (!file_exists($filePath)) {
  59.                 mkdir($filePath0775true);
  60.             }
  61.             Browsershot::html($request->request->get('body'))
  62.                 ->showBrowserHeaderAndFooter()
  63.                 ->hideHeader()
  64.                 ->footerHtml(
  65.                     "
  66.                 <style>
  67.                     html, body {
  68.                       font-size: 10px;
  69.                       font-family: Helvetica;
  70.                       position: relative;
  71.                       color: #2a2a2a;
  72.                     }
  73.                     .fileName {
  74.                       position: absolute;
  75.                       left: 30px;
  76.                     }
  77.                     .meta {
  78.                       position: absolute;
  79.                       right: 30px;
  80.                     }
  81.                 </style>
  82.                 <div class='fileName'>$fileName</div>
  83.                 <div class='meta'>
  84.                     <span class='pageNumber'></span>
  85.                     <span>/</span>
  86.                     <span class='totalPages'></span>
  87.                 </div>
  88.                 "
  89.                 )
  90.                 ->noSandbox()
  91.                 ->showBackground()
  92.                 ->margins(10101510)
  93.                 ->format('A4')
  94.                 ->savePdf($filePath $fileName);
  95.             $user $this->tokenStorage->getToken()->getUser();
  96.             $estimate->setOrderedAt(new \DateTime());
  97.             $this->entityManager->persist($estimate);
  98.             $this->entityManager->flush();
  99.             $this->messagingService->sendOrderConfirmationMessage(
  100.                 $user,
  101.                 $estimate,
  102.                 $filePath $fileName,
  103.                 $request->getLocale()
  104.             );
  105.         }
  106.     }
  107. }