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. return;
  50. if ($event->getRequest()->attributes->get('_route') === 'api_estimates_order_item') {
  51. /** @var Estimate $estimate */
  52. $estimate = $event->getControllerResult();
  53. $request = $event->getRequest();
  54. $fileName = $request->request->get('filename', date('d.m.Y') . '.pdf');
  55. // sanitize file name for internet explorer string stuff
  56. $fileName = preg_replace('/[^a-zA-Z0-9\-\._]/', '', $fileName);
  57. $directory = bin2hex(random_bytes(24));
  58. $filePath = $this->cacheDir . '/rendered-pdf/' . $directory . '/';
  59. if (!file_exists($filePath)) {
  60. mkdir($filePath, 0775, true);
  61. }
  62. Browsershot::html($request->request->get('body'))
  63. ->showBrowserHeaderAndFooter()
  64. ->hideHeader()
  65. ->footerHtml(
  66. "
  67. <style>
  68. html, body {
  69. font-size: 10px;
  70. font-family: Helvetica;
  71. position: relative;
  72. color: #2a2a2a;
  73. }
  74. .fileName {
  75. position: absolute;
  76. left: 30px;
  77. }
  78. .meta {
  79. position: absolute;
  80. right: 30px;
  81. }
  82. </style>
  83. <div class='fileName'>$fileName</div>
  84. <div class='meta'>
  85. <span class='pageNumber'></span>
  86. <span>/</span>
  87. <span class='totalPages'></span>
  88. </div>
  89. "
  90. )
  91. ->noSandbox()
  92. ->showBackground()
  93. ->margins(10, 10, 15, 10)
  94. ->format('A4')
  95. ->savePdf($filePath . $fileName);
  96. $user = $this->tokenStorage->getToken()->getUser();
  97. $estimate->setOrderedAt(new \DateTime());
  98. $this->entityManager->persist($estimate);
  99. $this->entityManager->flush();
  100. $this->messagingService->sendOrderConfirmationMessage(
  101. $user,
  102. $estimate,
  103. $filePath . $fileName,
  104. $request->getLocale()
  105. );
  106. }
  107. }
  108. }