vendor/sylius/resource-bundle/src/Bundle/Controller/FlashHelper.php line 120

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Sylius package.
  4. *
  5. * (c) Paweł Jędrzejewski
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\ResourceBundle\Controller;
  12. use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
  13. use Sylius\Component\Resource\Metadata\MetadataInterface;
  14. use Sylius\Component\Resource\Model\ResourceInterface;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  17. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  18. use Symfony\Component\Translation\TranslatorBagInterface;
  19. use Symfony\Contracts\Translation\TranslatorInterface;
  20. final class FlashHelper implements FlashHelperInterface
  21. {
  22. /** @var RequestStack|SessionInterface */
  23. private $requestStack;
  24. private TranslatorInterface $translator;
  25. private string $defaultLocale;
  26. /**
  27. * @param RequestStack|SessionInterface $requestStack
  28. */
  29. public function __construct(/* RequestStack */ $requestStack, TranslatorInterface $translator, string $defaultLocale)
  30. {
  31. /** @phpstan-ignore-next-line */
  32. if (!$requestStack instanceof SessionInterface && !$requestStack instanceof RequestStack) {
  33. throw new \InvalidArgumentException(sprintf('The first argument of "%s" should be instance of "%s" or "%s"', __METHOD__, SessionInterface::class, RequestStack::class));
  34. }
  35. if ($requestStack instanceof SessionInterface) {
  36. trigger_deprecation(
  37. 'sylius/resource-bundle',
  38. '1.10',
  39. 'Passing an instance of "%s" as the first constructor argument for "%s" is deprecated and will not be supported in 2.0. Pass an instance of "%s" instead.',
  40. SessionInterface::class,
  41. self::class,
  42. RequestStack::class,
  43. );
  44. }
  45. $this->requestStack = $requestStack;
  46. $this->translator = $translator;
  47. $this->defaultLocale = $defaultLocale;
  48. }
  49. public function addSuccessFlash(
  50. RequestConfiguration $requestConfiguration,
  51. string $actionName,
  52. ?ResourceInterface $resource = null,
  53. ): void {
  54. $this->addFlashWithType($requestConfiguration, $actionName, 'success');
  55. }
  56. public function addErrorFlash(RequestConfiguration $requestConfiguration, string $actionName): void
  57. {
  58. $this->addFlashWithType($requestConfiguration, $actionName, 'error');
  59. }
  60. public function addFlashFromEvent(RequestConfiguration $requestConfiguration, ResourceControllerEvent $event): void
  61. {
  62. $this->addFlash($event->getMessageType(), $event->getMessage(), $event->getMessageParameters());
  63. }
  64. private function addFlashWithType(RequestConfiguration $requestConfiguration, string $actionName, string $type): void
  65. {
  66. $metadata = $requestConfiguration->getMetadata();
  67. $parameters = $this->getParametersWithName($metadata, $actionName);
  68. $message = (string) $requestConfiguration->getFlashMessage($actionName);
  69. if (empty($message)) {
  70. return;
  71. }
  72. if ($this->isTranslationDefined($message, $this->defaultLocale, $parameters)) {
  73. if (!$this->translator instanceof TranslatorBagInterface) {
  74. $this->addFlash($type, $message, $parameters);
  75. return;
  76. }
  77. $this->addFlash($type, $message);
  78. return;
  79. }
  80. $this->addFlash(
  81. $type,
  82. $this->getResourceMessage($actionName),
  83. $parameters,
  84. );
  85. }
  86. private function addFlash(string $type, string $message, array $parameters = []): void
  87. {
  88. if (!empty($parameters)) {
  89. $message = $this->prepareMessage($message, $parameters);
  90. }
  91. if ($this->requestStack instanceof SessionInterface) {
  92. $session = $this->requestStack;
  93. } else {
  94. $session = $this->requestStack->getSession();
  95. }
  96. /** @var FlashBagInterface $flashBag */
  97. $flashBag = $session->getBag('flashes');
  98. $flashBag->add($type, $message);
  99. }
  100. private function prepareMessage(string $message, array $parameters): array
  101. {
  102. return [
  103. 'message' => $message,
  104. 'parameters' => $parameters,
  105. ];
  106. }
  107. private function getResourceMessage(string $actionName): string
  108. {
  109. return sprintf('sylius.resource.%s', $actionName);
  110. }
  111. private function isTranslationDefined(string $message, string $locale, array $parameters): bool
  112. {
  113. if ($this->translator instanceof TranslatorBagInterface) {
  114. $defaultCatalogue = $this->translator->getCatalogue($locale);
  115. return $defaultCatalogue->has($message, 'flashes');
  116. }
  117. return $message !== $this->translator->trans($message, $parameters, 'flashes');
  118. }
  119. private function getParametersWithName(MetadataInterface $metadata, string $actionName): array
  120. {
  121. if (stripos($actionName, 'bulk') !== false) {
  122. return ['%resources%' => ucfirst($metadata->getPluralName())];
  123. }
  124. return ['%resource%' => ucfirst($metadata->getHumanizedName())];
  125. }
  126. }