vendor/ibexa/corporate-account/src/lib/Form/Processor/ApplicationFormProcessor.php line 74

Open in your IDE?
  1. <?php
  2. /**
  3. * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4. * @license For full copyright and license information view LICENSE file distributed with this source code.
  5. */
  6. declare(strict_types=1);
  7. namespace Ibexa\CorporateAccount\Form\Processor;
  8. use Ibexa\ContentForms\Data\Content\ContentCreateData;
  9. use Ibexa\ContentForms\Data\Content\ContentUpdateData;
  10. use Ibexa\ContentForms\Event\FormActionEvent;
  11. use Ibexa\Contracts\Core\Repository\ContentService;
  12. use Ibexa\Contracts\Core\Repository\Repository;
  13. use Ibexa\Contracts\Core\Repository\RoleService;
  14. use Ibexa\Contracts\Core\Repository\UserService;
  15. use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Operator;
  16. use Ibexa\Contracts\CorporateAccount\Service\ApplicationService;
  17. use Ibexa\Contracts\CorporateAccount\Service\CompanyService;
  18. use Ibexa\Contracts\CorporateAccount\Service\MemberService;
  19. use Ibexa\Contracts\CorporateAccount\Service\ShippingAddressService;
  20. use Ibexa\Contracts\CorporateAccount\Values\Application;
  21. use Ibexa\Contracts\CorporateAccount\Values\Query\Criterion\ApplicationEmail;
  22. use Ibexa\Core\Base\Exceptions\InvalidArgumentException;
  23. use Ibexa\CorporateAccount\Event\DispatcherEvents;
  24. use Ibexa\CorporateAccount\Values\Mapper\DomainMapperInterface;
  25. use Symfony\Component\HttpFoundation\RedirectResponse;
  26. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  27. class ApplicationFormProcessor extends FormProcessor
  28. {
  29. private ApplicationService $applicationService;
  30. private Repository $repository;
  31. public function __construct(
  32. ContentService $contentService,
  33. UserService $userService,
  34. RoleService $roleService,
  35. UrlGeneratorInterface $urlGenerator,
  36. DomainMapperInterface $domainMapper,
  37. CompanyService $companyService,
  38. MemberService $memberService,
  39. ShippingAddressService $shippingAddressService,
  40. Repository $repository,
  41. ApplicationService $applicationService
  42. ) {
  43. parent::__construct(
  44. $contentService,
  45. $userService,
  46. $roleService,
  47. $urlGenerator,
  48. $domainMapper,
  49. $companyService,
  50. $memberService,
  51. $shippingAddressService
  52. );
  53. $this->applicationService = $applicationService;
  54. $this->repository = $repository;
  55. }
  56. /**
  57. * @return array<string, mixed>
  58. */
  59. public static function getSubscribedEvents(): array
  60. {
  61. return [
  62. DispatcherEvents::APPLICATION_EDIT_PUBLISH => ['processCreate', 10],
  63. ];
  64. }
  65. public function processCreate(FormActionEvent $event): void
  66. {
  67. $data = $event->getData();
  68. $form = $event->getForm();
  69. if (!$data instanceof ContentCreateData && !$data instanceof ContentUpdateData) {
  70. throw new InvalidArgumentException(
  71. '$data',
  72. 'Expected ContentCreateData or ContentUpdateData'
  73. );
  74. }
  75. $languageCode = $form->getConfig()->getOption('languageCode');
  76. if ($data->isNew() && $data instanceof ContentCreateData) {
  77. $email = $data->fieldsData['user']->value->email;
  78. $applicationsCount = $this->repository->sudo(function () use ($email): int {
  79. return $this->applicationService->getApplicationsCount(
  80. new ApplicationEmail(Operator::EQ, $email)
  81. );
  82. });
  83. if ($applicationsCount > 0) {
  84. $event->setResponse(new RedirectResponse(
  85. $this->urlGenerator->generate('ibexa.corporate_account.customer_portal.corporate_account.register.confirmation')
  86. ));
  87. return;
  88. }
  89. $application = $this->createApplication($data, $languageCode);
  90. } elseif ($data instanceof ContentUpdateData) {
  91. $application = $this->updateApplication($data, $languageCode);
  92. }
  93. $event->setPayload('application', $application ?? null);
  94. }
  95. protected function createApplication(
  96. ContentCreateData $contentCreateData,
  97. string $languageCode
  98. ): Application {
  99. $applicationCreateStruct = $this->applicationService->newApplicationCreateStruct();
  100. $applicationCreateStruct->contentType = $contentCreateData->contentType;
  101. $applicationCreateStruct->mainLanguageCode = $contentCreateData->mainLanguageCode;
  102. $applicationCreateStruct->alwaysAvailable = $contentCreateData->alwaysAvailable;
  103. $mainLanguageCode = $this->resolveMainLanguageCode($contentCreateData);
  104. foreach ($contentCreateData->fieldsData as $fieldDefIdentifier => $fieldData) {
  105. if ($mainLanguageCode != $languageCode && !$fieldData->fieldDefinition->isTranslatable) {
  106. continue;
  107. }
  108. $applicationCreateStruct->setField($fieldDefIdentifier, $fieldData->value, $languageCode);
  109. }
  110. return $this->applicationService->createApplication($applicationCreateStruct);
  111. }
  112. protected function updateApplication(
  113. ContentUpdateData $contentUpdateData,
  114. string $languageCode
  115. ): Application {
  116. $applicationUpdateStruct = $this->applicationService->newApplicationUpdateStruct();
  117. $applicationUpdateStruct->initialLanguageCode = $contentUpdateData->initialLanguageCode;
  118. $applicationUpdateStruct->creatorId = $contentUpdateData->creatorId;
  119. $mainLanguageCode = $this->resolveMainLanguageCode($contentUpdateData);
  120. foreach ($contentUpdateData->fieldsData as $fieldDefIdentifier => $fieldData) {
  121. if ($mainLanguageCode != $languageCode && !$fieldData->fieldDefinition->isTranslatable) {
  122. continue;
  123. }
  124. $applicationUpdateStruct->setField($fieldDefIdentifier, $fieldData->value, $languageCode);
  125. }
  126. $application = $this->applicationService->getApplication($contentUpdateData->contentDraft->id);
  127. return $this->applicationService->updateApplication($application, $applicationUpdateStruct);
  128. }
  129. }