vendor/perfectneeds/content-multi-lang-bundle/Service/DynamicContentService.php line 151

Open in your IDE?
  1. <?php
  2. namespace PN\ContentBundle\Service;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use PN\ContentBundle\Entity\DynamicContentAttribute;
  5. use PN\ServiceBundle\Service\ContainerParameterService;
  6. use Psr\Cache\InvalidArgumentException;
  7. use Symfony\Component\Asset\Packages;
  8. use Symfony\Component\Cache\Adapter\FilesystemAdapter;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Symfony\Component\HttpKernel\KernelInterface;
  12. use Symfony\Component\Routing\RouterInterface;
  13. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  14. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  15. use Symfony\Contracts\Cache\ItemInterface;
  16. class DynamicContentService
  17. {
  18. private string $environment;
  19. private EntityManagerInterface $em;
  20. private Packages $assetsManager;
  21. private RouterInterface $router;
  22. private TokenStorageInterface $tokenStorage;
  23. private AuthorizationCheckerInterface $authorizationChecker;
  24. private ContainerParameterService $containerParameterService;
  25. private ?Request $request = null;
  26. public function __construct(
  27. KernelInterface $kernel,
  28. EntityManagerInterface $em,
  29. RouterInterface $router,
  30. Packages $assetsManager,
  31. TokenStorageInterface $tokenStorage,
  32. AuthorizationCheckerInterface $authorizationChecker,
  33. RequestStack $requestStack,
  34. ContainerParameterService $containerParameterService
  35. )
  36. {
  37. $this->environment = $kernel->getEnvironment();
  38. $this->em = $em;
  39. $this->assetsManager = $assetsManager;
  40. $this->router = $router;
  41. $this->tokenStorage = $tokenStorage;
  42. $this->authorizationChecker = $authorizationChecker;
  43. $this->containerParameterService = $containerParameterService;
  44. if ($requestStack instanceof RequestStack) {
  45. $this->request = $requestStack->getCurrentRequest();
  46. }
  47. }
  48. /**
  49. * @throws InvalidArgumentException
  50. */
  51. public function getDynamicContentAttribute($dynamicContentAttributeId, bool $showEditBtn = true): string
  52. {
  53. $dynamicContentValue = $this->getDynamicContentValueFromCache($dynamicContentAttributeId);
  54. $editBtn = "";
  55. if ($showEditBtn === true and in_array($dynamicContentValue["type"],
  56. [DynamicContentAttribute::TYPE_TEXT, DynamicContentAttribute::TYPE_LONGTEXT])) {
  57. $editBtn = $this->showEditBtn($dynamicContentAttributeId);
  58. }
  59. return $dynamicContentValue["value"] . $editBtn;
  60. }
  61. public function showEditBtn($dynamicContentAttributeId): string
  62. {
  63. if (!$this->isGranted("ROLE_ADMIN")) {
  64. return '';
  65. }
  66. $url = $this->router->generate("dynamic_content_attribute_edit", ['id' => $dynamicContentAttributeId]);
  67. return ' <a href="' . $url . '" target="popup" style="text-decoration: underline;" onclick="window.open(\'' . $url . '\',\'popup\',\'width=600,height=600\'); return false;" title="Edit">Edit</a>';
  68. }
  69. private function isGranted(string $attributes): bool
  70. {
  71. if ($this->tokenStorage->getToken() == null) {
  72. return false;
  73. }
  74. return $this->authorizationChecker->isGranted($attributes, null);
  75. }
  76. /**
  77. * @throws InvalidArgumentException
  78. */
  79. private function getDynamicContentValueFromCache($dynamicContentAttributeId): array
  80. {
  81. $cache = new FilesystemAdapter(directory: $this->containerParameterService->get("kernel.cache_dir") . "/filesystemAdapter-cache");
  82. $cacheKey = $this->getCacheName($dynamicContentAttributeId);
  83. $locale = $this->request instanceof Request ? $this->request->getLocale() : "none";
  84. if ($this->environment == "dev") {
  85. $this->removeDynamicContentValueFromCache($dynamicContentAttributeId);
  86. }
  87. $data = $cache->get($cacheKey, function (ItemInterface $item) use ($dynamicContentAttributeId, $locale) {
  88. $item->expiresAfter(2592000); // expire after 30 days
  89. $dynamicContentAttributeValue = $this->getDynamicContentValue($dynamicContentAttributeId);
  90. return [
  91. "type" => $dynamicContentAttributeValue["type"],
  92. $locale => $dynamicContentAttributeValue["value"],
  93. ];
  94. });
  95. if (!array_key_exists($locale, $data)) {
  96. $cache->delete($cacheKey);
  97. $data = $cache->get($cacheKey, function (ItemInterface $item) use ($dynamicContentAttributeId, $locale, $data) {
  98. $item->expiresAfter(2592000); // expire after 30 days
  99. $data[$locale] = $this->getDynamicContentValue($dynamicContentAttributeId)["value"];
  100. return $data;
  101. });
  102. }
  103. return [
  104. "type" => $data["type"],
  105. "value" => $data[$locale],
  106. ];
  107. }
  108. /**
  109. * @throws \InvalidArgumentException
  110. */
  111. public function removeDynamicContentValueFromCache($dynamicContentAttributeId): void
  112. {
  113. $cacheKey = $this->getCacheName($dynamicContentAttributeId);
  114. $cache = new FilesystemAdapter(directory: $this->containerParameterService->get("kernel.cache_dir") . "/filesystemAdapter-cache");
  115. $cache->delete($cacheKey);
  116. }
  117. public function removeAllDynamicContentCache(): void
  118. {
  119. $cache = new FilesystemAdapter(directory: $this->containerParameterService->get("kernel.cache_dir") . "/filesystemAdapter-cache");
  120. $dynamicContentAttributes = $this->em->getRepository(DynamicContentAttribute::class)->findAll();
  121. foreach ($dynamicContentAttributes as $dynamicContentAttribute) {
  122. $cacheKey = $this->getCacheName($dynamicContentAttribute->getId());
  123. $cache->delete($cacheKey);
  124. }
  125. }
  126. private function getDynamicContentValue($dynamicContentAttributeId): array
  127. {
  128. $dynamicContentAttribute = $this->em->getRepository(DynamicContentAttribute::class)->find($dynamicContentAttributeId);
  129. if (!$dynamicContentAttribute) {
  130. return ["type" => null, "value" => ""];
  131. }
  132. if ($dynamicContentAttribute->getType() == DynamicContentAttribute::TYPE_IMAGE and $dynamicContentAttribute->getImage() != null) {
  133. return [
  134. "type" => $dynamicContentAttribute->getType(),
  135. "value" => $this->assetsManager->getUrl($dynamicContentAttribute->getImage()->getAssetPath()),
  136. ];
  137. } elseif ($dynamicContentAttribute->getType() == DynamicContentAttribute::TYPE_DOCUMENT and $dynamicContentAttribute->getDocument() != null) {
  138. $params = ["document" => $dynamicContentAttribute->getDocument()->getId()];
  139. return [
  140. "type" => $dynamicContentAttribute->getType(),
  141. "value" => $this->router->generate("download") . "?d=" . str_replace('"', "'",
  142. json_encode($params)),
  143. ];
  144. } elseif ($dynamicContentAttribute->getType() == DynamicContentAttribute::TYPE_HTML) {
  145. return [
  146. "type" => $dynamicContentAttribute->getType(),
  147. "value" => $dynamicContentAttribute->getValue(),
  148. ];
  149. } elseif ($dynamicContentAttribute->getType() == DynamicContentAttribute::TYPE_NUMBER) {
  150. return [
  151. "type" => $dynamicContentAttribute->getType(),
  152. "value" => $dynamicContentAttribute->getValue(),
  153. ];
  154. }
  155. return [
  156. "type" => $dynamicContentAttribute->getType(),
  157. "value" => nl2br($dynamicContentAttribute->getValue()),
  158. ];
  159. }
  160. private function getCacheName($id): string
  161. {
  162. return 'dynamic_content_' . $id;
  163. }
  164. }