vendor/ibexa/corporate-account/src/lib/Form/Processor/MemberNotificationFormProcessor.php line 49

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\AdminUi\Specification\SiteAccess\IsAdmin;
  9. use Ibexa\ContentForms\Event\FormActionEvent;
  10. use Ibexa\Contracts\AdminUi\Notification\TranslatableNotificationHandlerInterface;
  11. use Ibexa\CorporateAccount\Event\DispatcherEvents;
  12. use JMS\TranslationBundle\Annotation\Desc;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. final class MemberNotificationFormProcessor extends FormProcessor
  16. {
  17. private TranslatableNotificationHandlerInterface $notificationHandler;
  18. private RequestStack $requestStack;
  19. /** @var string[][] */
  20. private array $siteAccessGroups;
  21. /**
  22. * @param string[][] $siteAccessGroups
  23. */
  24. public function __construct(
  25. TranslatableNotificationHandlerInterface $notificationHandler,
  26. RequestStack $requestStack,
  27. array $siteAccessGroups
  28. ) {
  29. $this->notificationHandler = $notificationHandler;
  30. $this->requestStack = $requestStack;
  31. $this->siteAccessGroups = $siteAccessGroups;
  32. }
  33. public static function getSubscribedEvents(): array
  34. {
  35. return [
  36. DispatcherEvents::MEMBER_CREATE => ['addCreateSuccessNotification', 5],
  37. DispatcherEvents::MEMBER_UPDATE => ['addUpdateSuccessNotification', 5],
  38. ];
  39. }
  40. public function addCreateSuccessNotification(FormActionEvent $event): void
  41. {
  42. if (!$this->isAdminSiteAccess($this->requestStack->getCurrentRequest())) {
  43. return;
  44. }
  45. $this->notificationHandler->success(
  46. /** @Desc("Member created.") */
  47. 'member.create.success',
  48. [],
  49. 'ibexa_corporate_account'
  50. );
  51. }
  52. public function addUpdateSuccessNotification(FormActionEvent $event): void
  53. {
  54. if (!$this->isAdminSiteAccess($this->requestStack->getCurrentRequest())) {
  55. return;
  56. }
  57. /** @var \Ibexa\CorporateAccount\Form\Data\Member\MemberUpdateData $data */
  58. $data = $event->getData();
  59. $this->notificationHandler->success(
  60. /** @Desc("Member '%name%' updated.") */
  61. 'member.edit.success',
  62. ['%name%' => $data->getMember()->getName()],
  63. 'ibexa_corporate_account'
  64. );
  65. }
  66. protected function isAdminSiteAccess(?Request $request): bool
  67. {
  68. if ($request === null) {
  69. return false;
  70. }
  71. return (new IsAdmin($this->siteAccessGroups))->isSatisfiedBy($request->attributes->get('siteaccess'));
  72. }
  73. }