vendor/ibexa/product-catalog/src/bundle/EventSubscriber/ContentFormSubscriber.php line 38

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\Bundle\ProductCatalog\EventSubscriber;
  8. use Ibexa\ContentForms\Data\Content\ContentCreateData;
  9. use Ibexa\ContentForms\Data\Content\ContentUpdateData;
  10. use Ibexa\ContentForms\Event\ContentFormEvents;
  11. use Ibexa\ContentForms\Event\FormActionEvent;
  12. use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
  13. use Ibexa\ProductCatalog\FieldType\ProductSpecification\Type;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpFoundation\RedirectResponse;
  16. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  17. final class ContentFormSubscriber implements EventSubscriberInterface
  18. {
  19. private UrlGeneratorInterface $urlGenerator;
  20. public function __construct(UrlGeneratorInterface $urlGenerator)
  21. {
  22. $this->urlGenerator = $urlGenerator;
  23. }
  24. public static function getSubscribedEvents(): array
  25. {
  26. return [
  27. ContentFormEvents::CONTENT_PUBLISH => ['onContentFormEvent', -255],
  28. ContentFormEvents::CONTENT_CANCEL => ['onContentFormEvent', -255],
  29. ];
  30. }
  31. public function onContentFormEvent(FormActionEvent $event): void
  32. {
  33. $contentType = $this->resolveContentType($event->getData());
  34. if ($contentType !== null) {
  35. $isProduct = $contentType->hasFieldDefinitionOfType(Type::FIELD_TYPE_IDENTIFIER);
  36. if ($isProduct) {
  37. $event->setResponse($this->createRedirectToProductListResponse());
  38. }
  39. }
  40. }
  41. private function createRedirectToProductListResponse(): RedirectResponse
  42. {
  43. $redirectionUrl = $this->urlGenerator->generate(
  44. 'ibexa.product_catalog.product.list'
  45. );
  46. return new RedirectResponse($redirectionUrl);
  47. }
  48. /**
  49. * @param mixed $data
  50. */
  51. private function resolveContentType($data): ?ContentType
  52. {
  53. if ($data instanceof ContentCreateData) {
  54. return $data->contentType;
  55. } elseif ($data instanceof ContentUpdateData) {
  56. return $data->contentDraft->getContentType();
  57. } else {
  58. return null;
  59. }
  60. }
  61. }