123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- declare(strict_types=1);
- namespace Magento\Rss\Model;
- use Magento\Framework\App\ObjectManager;
- use Magento\Framework\App\Rss\DataProviderInterface;
- use Magento\Framework\Serialize\SerializerInterface;
- use Magento\Framework\App\FeedFactoryInterface;
- /**
- * Provides functionality to work with RSS feeds
- *
- * @api
- * @since 100.0.2
- */
- class Rss
- {
- /**
- * @var DataProviderInterface
- */
- protected $dataProvider;
- /**
- * @var \Magento\Framework\App\CacheInterface
- */
- protected $cache;
- /**
- * @var \Magento\Framework\App\FeedFactoryInterface
- */
- private $feedFactory;
- /**
- * @var SerializerInterface
- */
- private $serializer;
- /**
- * Rss constructor
- *
- * @param \Magento\Framework\App\CacheInterface $cache
- * @param SerializerInterface|null $serializer
- * @param FeedFactoryInterface|null $feedFactory
- */
- public function __construct(
- \Magento\Framework\App\CacheInterface $cache,
- SerializerInterface $serializer = null,
- FeedFactoryInterface $feedFactory = null
- ) {
- $this->cache = $cache;
- $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
- $this->feedFactory = $feedFactory ?: ObjectManager::getInstance()->get(FeedFactoryInterface::class);
- }
- /**
- * @return array
- */
- public function getFeeds()
- {
- if ($this->dataProvider === null) {
- return [];
- }
- $cache = false;
- if ($this->dataProvider->getCacheKey() && $this->dataProvider->getCacheLifetime()) {
- $cache = $this->cache->load($this->dataProvider->getCacheKey());
- }
- if ($cache) {
- return $this->serializer->unserialize($cache);
- }
- $data = $this->dataProvider->getRssData();
- if ($this->dataProvider->getCacheKey() && $this->dataProvider->getCacheLifetime()) {
- $this->cache->save(
- $this->serializer->serialize($data),
- $this->dataProvider->getCacheKey(),
- ['rss'],
- $this->dataProvider->getCacheLifetime()
- );
- }
- return $data;
- }
- /**
- * @param DataProviderInterface $dataProvider
- * @return $this
- */
- public function setDataProvider(DataProviderInterface $dataProvider)
- {
- $this->dataProvider = $dataProvider;
- return $this;
- }
- /**
- * @return string
- * @throws \Magento\Framework\Exception\InputException
- * @throws \Magento\Framework\Exception\RuntimeException
- */
- public function createRssXml()
- {
- $feed = $this->feedFactory->create($this->getFeeds(), FeedFactoryInterface::FORMAT_RSS);
- return $feed->getFormattedContent();
- }
- }
|