Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Rss.php
Go to the documentation of this file.
1 <?php
7 declare(strict_types=1);
8 
9 namespace Magento\Rss\Model;
10 
15 
22 class Rss
23 {
27  protected $dataProvider;
28 
32  protected $cache;
33 
37  private $feedFactory;
38 
42  private $serializer;
43 
51  public function __construct(
52  \Magento\Framework\App\CacheInterface $cache,
53  SerializerInterface $serializer = null,
54  FeedFactoryInterface $feedFactory = null
55  ) {
56  $this->cache = $cache;
57  $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
58  $this->feedFactory = $feedFactory ?: ObjectManager::getInstance()->get(FeedFactoryInterface::class);
59  }
60 
64  public function getFeeds()
65  {
66  if ($this->dataProvider === null) {
67  return [];
68  }
69  $cache = false;
70  if ($this->dataProvider->getCacheKey() && $this->dataProvider->getCacheLifetime()) {
71  $cache = $this->cache->load($this->dataProvider->getCacheKey());
72  }
73 
74  if ($cache) {
75  return $this->serializer->unserialize($cache);
76  }
77 
78  $data = $this->dataProvider->getRssData();
79 
80  if ($this->dataProvider->getCacheKey() && $this->dataProvider->getCacheLifetime()) {
81  $this->cache->save(
82  $this->serializer->serialize($data),
83  $this->dataProvider->getCacheKey(),
84  ['rss'],
85  $this->dataProvider->getCacheLifetime()
86  );
87  }
88 
89  return $data;
90  }
91 
97  {
98  $this->dataProvider = $dataProvider;
99  return $this;
100  }
101 
107  public function createRssXml()
108  {
109  $feed = $this->feedFactory->create($this->getFeeds(), FeedFactoryInterface::FORMAT_RSS);
110  return $feed->getFormattedContent();
111  }
112 }
setDataProvider(DataProviderInterface $dataProvider)
Definition: Rss.php:96
__construct(\Magento\Framework\App\CacheInterface $cache, SerializerInterface $serializer=null, FeedFactoryInterface $feedFactory=null)
Definition: Rss.php:51