src/EventSubscriber/Api/EstimateAddressSubscriber.php line 41

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