RssManager.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Rss\Model;
  7. use Magento\Framework\App\Rss\DataProviderInterface;
  8. use Magento\Framework\App\Rss\RssManagerInterface;
  9. /**
  10. * Rss Manager
  11. *
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class RssManager implements RssManagerInterface
  16. {
  17. /**
  18. * @var \Magento\Framework\App\Rss\DataProviderInterface[]
  19. */
  20. protected $providers;
  21. /**
  22. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  23. * @param array $dataProviders
  24. */
  25. public function __construct(
  26. \Magento\Framework\ObjectManagerInterface $objectManager,
  27. array $dataProviders = []
  28. ) {
  29. $this->objectManager = $objectManager;
  30. $this->providers = $dataProviders;
  31. }
  32. /**
  33. * Return Rss Data Provider by Rss Feed Id.
  34. *
  35. * @param string $type
  36. * @return DataProviderInterface
  37. * @throws \InvalidArgumentException
  38. */
  39. public function getProvider($type)
  40. {
  41. if (!isset($this->providers[$type])) {
  42. throw new \InvalidArgumentException('Unknown provider with type: ' . $type);
  43. }
  44. $provider = $this->providers[$type];
  45. if (is_string($provider)) {
  46. $provider = $this->objectManager->get($provider);
  47. }
  48. if (!$provider instanceof DataProviderInterface) {
  49. throw new \InvalidArgumentException('Provider should implement DataProviderInterface');
  50. }
  51. $this->providers[$type] = $provider;
  52. return $this->providers[$type];
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function getProviders()
  58. {
  59. $result = [];
  60. foreach (array_keys($this->providers) as $type) {
  61. $result[] = $this->getProvider($type);
  62. }
  63. return $result;
  64. }
  65. }