- <?php
- // api/src/EventSubscriber/BookMailSubscriber.php
- namespace Roothirsch\ShopBundle\EventSubscriber\Api;
- use ApiPlatform\Core\EventListener\EventPriorities;
- use Roothirsch\CoreBundle\Messaging\MessagingService;
- use Roothirsch\ShopBundle\Entity\Estimate;
- use Doctrine\ORM\EntityManagerInterface;
- use Spatie\Browsershot\Browsershot;
- use Symfony\Component\EventDispatcher\EventSubscriberInterface;
- use Symfony\Component\HttpKernel\KernelEvents;
- use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
- final class EstimateOrderSubscriber implements EventSubscriberInterface
- {
-     /**
-      * @var MessagingService
-      */
-     private $messagingService;
-     /**
-      * @var string
-      */
-     private $cacheDir;
-     /**
-      * @var TokenStorageInterface
-      */
-     private $tokenStorage;
-     /**
-      * @var EntityManagerInterface
-      */
-     private $entityManager;
-     public function __construct(
-         MessagingService $messagingService,
-         $cacheDir,
-         TokenStorageInterface $tokenStorage,
-         EntityManagerInterface $entityManager
-     ) {
-         $this->messagingService = $messagingService;
-         $this->cacheDir = $cacheDir;
-         $this->tokenStorage = $tokenStorage;
-         $this->entityManager = $entityManager;
-     }
-     public static function getSubscribedEvents()
-     {
-         return [
-             KernelEvents::VIEW => ['validate', EventPriorities::POST_VALIDATE],
-         ];
-     }
-     public function validate(\Symfony\Component\HttpKernel\Event\ViewEvent $event)
-     {
-         return;
-         if ($event->getRequest()->attributes->get('_route') === 'api_estimates_order_item') {
-             /** @var Estimate $estimate */
-             $estimate = $event->getControllerResult();
-             $request = $event->getRequest();
-             $fileName = $request->request->get('filename', date('d.m.Y') . '.pdf');
-             // sanitize file name for internet explorer string stuff
-             $fileName = preg_replace('/[^a-zA-Z0-9\-\._]/', '', $fileName);
-             $directory = bin2hex(random_bytes(24));
-             $filePath = $this->cacheDir . '/rendered-pdf/' . $directory . '/';
-             if (!file_exists($filePath)) {
-                 mkdir($filePath, 0775, true);
-             }
-             Browsershot::html($request->request->get('body'))
-                 ->showBrowserHeaderAndFooter()
-                 ->hideHeader()
-                 ->footerHtml(
-                     "
-                 <style>
-                     html, body {
-                       font-size: 10px;
-                       font-family: Helvetica;
-                       position: relative;
-                       color: #2a2a2a;
-                     }
-                     .fileName {
-                       position: absolute;
-                       left: 30px;
-                     }
-                     .meta {
-                       position: absolute;
-                       right: 30px;
-                     }
-                 </style>
-                 <div class='fileName'>$fileName</div>
-                 <div class='meta'>
-                     <span class='pageNumber'></span>
-                     <span>/</span>
-                     <span class='totalPages'></span>
-                 </div>
-                 "
-                 )
-                 ->noSandbox()
-                 ->showBackground()
-                 ->margins(10, 10, 15, 10)
-                 ->format('A4')
-                 ->savePdf($filePath . $fileName);
-             $user = $this->tokenStorage->getToken()->getUser();
-             $estimate->setOrderedAt(new \DateTime());
-             $this->entityManager->persist($estimate);
-             $this->entityManager->flush();
-             $this->messagingService->sendOrderConfirmationMessage(
-                 $user,
-                 $estimate,
-                 $filePath . $fileName,
-                 $request->getLocale()
-             );
-         }
-     }
- }