src/Module/Cms/Provider/CmsUnpublishedContentProvider.php line 91

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Module\Cms\Provider;
  4. use App\Module\Catalog\Provider\CategoryTreeProvider;
  5. use App\Module\Cms\Repository\StoryRepository;
  6. use App\Module\StoryblokService\Business\StoryblokClient;
  7. use App\Store\StoreContext;
  8. use Denios\Data\Cms\Story;
  9. use RuntimeException;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Symfony\Contracts\Cache\CacheInterface;
  12. /**
  13. * Class CmsUnpublishedContentProvider
  14. *
  15. * @package App\Module\Cms\Provider
  16. */
  17. class CmsUnpublishedContentProvider extends CmsContentProvider
  18. {
  19. private RequestStack $requestStack;
  20. private bool $showPreview = false;
  21. private StoryblokClient $storyblokClient;
  22. /**
  23. * CmsUnpublishedContentProvider constructor.
  24. *
  25. * @param StoreContext $storeContext
  26. * @param CategoryTreeProvider $categoryTreeProvider
  27. * @param RequestStack $requestStack
  28. * @param StoryblokClient $storyblokClient
  29. * @param StoryRepository $storyRepository
  30. * @param CacheInterface $cache
  31. */
  32. public function __construct(
  33. StoreContext $storeContext,
  34. CategoryTreeProvider $categoryTreeProvider,
  35. RequestStack $requestStack,
  36. StoryblokClient $storyblokClient,
  37. StoryRepository $storyRepository,
  38. CacheInterface $cache
  39. ) {
  40. parent::__construct($storeContext, $categoryTreeProvider, $storyRepository, $cache);
  41. $this->requestStack = $requestStack;
  42. $this->storyblokClient = $storyblokClient;
  43. }
  44. /**
  45. * @param string $fullSlug
  46. * @param string|null $prefix
  47. * @return Story|null
  48. */
  49. private function getContentByFullSlug(string $fullSlug, string $prefix = null): ?Story
  50. {
  51. if (!$this->showPreview) {
  52. throw new RuntimeException(sprintf('Unexpected call of %s while showPreview is false.', __METHOD__));
  53. }
  54. return $this->storyblokClient->getPreviewByFullSlug($fullSlug, '');
  55. }
  56. /**
  57. * @param string $contentId
  58. * @return Story|null
  59. */
  60. public function getContentById(string $contentId): ?Story
  61. {
  62. if (!$this->showPreview) {
  63. return parent::getContentById($contentId);
  64. }
  65. return $this->storyblokClient->getPreviewByUuid($contentId);
  66. }
  67. /**
  68. * Because the $slug is not the storyblok slug, we can not use it to load preview data.
  69. *
  70. * @param string $store
  71. * @param string $locale
  72. * @param string $slug
  73. * @param string|null $prefix
  74. * @return Story|null
  75. */
  76. public function getContentBySlug(string $store, string $locale, string $slug, string $prefix = null): ?Story
  77. {
  78. return parent::getContentBySlug($store, $locale, $slug, $prefix);
  79. }
  80. /**
  81. * @param string|null $prefix
  82. * @return Story|null
  83. */
  84. public function getContentBySlugForCurrentPage(string $prefix = null): ?Story
  85. {
  86. if (!$this->showPreview) {
  87. return parent::getContentBySlugForCurrentPage($prefix);
  88. }
  89. if ($this->currentStory !== null) {
  90. return $this->currentStory;
  91. }
  92. $request = $this->requestStack->getMainRequest();
  93. $fullSlug = $request->get('fullSlug');
  94. return $this->currentStory = $this->getContentByFullSlug($fullSlug, $prefix);
  95. }
  96. /**
  97. * @param bool $showPreview
  98. * @return void
  99. */
  100. public function setShowPreview(bool $showPreview): void
  101. {
  102. $this->showPreview = $showPreview;
  103. }
  104. }