Rss.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Rss\Model;
  8. use Magento\Framework\App\ObjectManager;
  9. use Magento\Framework\App\Rss\DataProviderInterface;
  10. use Magento\Framework\Serialize\SerializerInterface;
  11. use Magento\Framework\App\FeedFactoryInterface;
  12. /**
  13. * Provides functionality to work with RSS feeds
  14. *
  15. * @api
  16. * @since 100.0.2
  17. */
  18. class Rss
  19. {
  20. /**
  21. * @var DataProviderInterface
  22. */
  23. protected $dataProvider;
  24. /**
  25. * @var \Magento\Framework\App\CacheInterface
  26. */
  27. protected $cache;
  28. /**
  29. * @var \Magento\Framework\App\FeedFactoryInterface
  30. */
  31. private $feedFactory;
  32. /**
  33. * @var SerializerInterface
  34. */
  35. private $serializer;
  36. /**
  37. * Rss constructor
  38. *
  39. * @param \Magento\Framework\App\CacheInterface $cache
  40. * @param SerializerInterface|null $serializer
  41. * @param FeedFactoryInterface|null $feedFactory
  42. */
  43. public function __construct(
  44. \Magento\Framework\App\CacheInterface $cache,
  45. SerializerInterface $serializer = null,
  46. FeedFactoryInterface $feedFactory = null
  47. ) {
  48. $this->cache = $cache;
  49. $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
  50. $this->feedFactory = $feedFactory ?: ObjectManager::getInstance()->get(FeedFactoryInterface::class);
  51. }
  52. /**
  53. * @return array
  54. */
  55. public function getFeeds()
  56. {
  57. if ($this->dataProvider === null) {
  58. return [];
  59. }
  60. $cache = false;
  61. if ($this->dataProvider->getCacheKey() && $this->dataProvider->getCacheLifetime()) {
  62. $cache = $this->cache->load($this->dataProvider->getCacheKey());
  63. }
  64. if ($cache) {
  65. return $this->serializer->unserialize($cache);
  66. }
  67. $data = $this->dataProvider->getRssData();
  68. if ($this->dataProvider->getCacheKey() && $this->dataProvider->getCacheLifetime()) {
  69. $this->cache->save(
  70. $this->serializer->serialize($data),
  71. $this->dataProvider->getCacheKey(),
  72. ['rss'],
  73. $this->dataProvider->getCacheLifetime()
  74. );
  75. }
  76. return $data;
  77. }
  78. /**
  79. * @param DataProviderInterface $dataProvider
  80. * @return $this
  81. */
  82. public function setDataProvider(DataProviderInterface $dataProvider)
  83. {
  84. $this->dataProvider = $dataProvider;
  85. return $this;
  86. }
  87. /**
  88. * @return string
  89. * @throws \Magento\Framework\Exception\InputException
  90. * @throws \Magento\Framework\Exception\RuntimeException
  91. */
  92. public function createRssXml()
  93. {
  94. $feed = $this->feedFactory->create($this->getFeeds(), FeedFactoryInterface::FORMAT_RSS);
  95. return $feed->getFormattedContent();
  96. }
  97. }