vendor/roothirsch/shop-bundle/EventSubscriber/Api/EstimateAddressSubscriber.php line 40

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\ShopBundle\Entity\Estimate;
  6. use Roothirsch\ShopBundle\Entity\EstimateAddress;
  7. use Roothirsch\ShopBundle\Repository\AddressRepository;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. final class EstimateAddressSubscriber implements EventSubscriberInterface
  12. {
  13. /**
  14. * @var EntityManagerInterface
  15. */
  16. protected $entityManager;
  17. /**
  18. * @var AddressRepository
  19. */
  20. private $estimateAddressRepository;
  21. public function __construct(EntityManagerInterface $entityManager, AddressRepository $estimateAddressRepository)
  22. {
  23. $this->entityManager = $entityManager;
  24. $this->estimateAddressRepository = $estimateAddressRepository;
  25. }
  26. public static function getSubscribedEvents()
  27. {
  28. return [
  29. KernelEvents::VIEW => ['validate', EventPriorities::PRE_VALIDATE],
  30. ];
  31. }
  32. public function validate(\Symfony\Component\HttpKernel\Event\ViewEvent $event)
  33. {
  34. if (!$event->getControllerResult() instanceof Estimate) {
  35. return;
  36. }
  37. /** @var \Roothirsch\ShopBundle\Entity\Estimate $estimate */
  38. $estimate = $event->getControllerResult();
  39. $this->updateCustomerAddress($estimate);
  40. $this->updateCarpenterAddresses($estimate);
  41. $this->entityManager->flush();
  42. }
  43. /**
  44. * @param \Roothirsch\ShopBundle\Entity\Estimate $estimate
  45. */
  46. protected function updateCustomerAddress(Estimate $estimate)
  47. {
  48. $sourceName = 'estimate_customer';
  49. $address = $this->estimateAddressRepository->findOneBy(
  50. [
  51. 'estimate' => $estimate,
  52. 'source' => $sourceName,
  53. ]
  54. );
  55. if (!$address) {
  56. $address = new EstimateAddress();
  57. $address
  58. ->setEstimate($estimate)
  59. ->setSource($sourceName);
  60. $this->entityManager->persist($address);
  61. }
  62. $address
  63. ->setOwner($estimate->getOwner())
  64. ->setCustomer($estimate->getCustomer())
  65. ->setAddress($estimate->getAddress())
  66. ->setContactPerson($estimate->getContactPerson())
  67. ->setIsHidden($estimate->getHideAddressInSelector());
  68. }
  69. /**
  70. * @param \Roothirsch\ShopBundle\Entity\Estimate $estimate
  71. */
  72. protected function updateCarpenterAddresses(Estimate $estimate)
  73. {
  74. $sourceName = 'estimate_item_carpenter';
  75. foreach ($estimate->getItems() as $item) {
  76. if (!isset($item['carpenterCompany']) || empty($item['carpenterCompany'])) {
  77. continue;
  78. }
  79. $address = $this->estimateAddressRepository->findOneBy(['itemShortcode' => $item['shortcode']]);
  80. if (!$address) {
  81. $address = new EstimateAddress();
  82. $address
  83. ->setEstimate($estimate)
  84. ->setSource($sourceName)
  85. ->setItemShortcode($item['shortcode']);
  86. $this->entityManager->persist($address);
  87. }
  88. $address
  89. ->setOwner($estimate->getOwner())
  90. ->setCustomer($item['carpenterCompany'])
  91. ->setAddress($item['carpenterAddress'])
  92. ->setContactPerson($estimate->getContactPerson())
  93. ->setIsHidden($estimate->getHideAddressInSelector());
  94. }
  95. }
  96. }