FeedFactory.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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\Framework\App;
  8. use Magento\Framework\ObjectManagerInterface;
  9. use Psr\Log\LoggerInterface;
  10. /**
  11. * Feed factory
  12. */
  13. class FeedFactory implements FeedFactoryInterface
  14. {
  15. /**
  16. * @var ObjectManagerInterface
  17. */
  18. private $objectManager;
  19. /**
  20. * @var LoggerInterface
  21. */
  22. private $logger;
  23. /**
  24. * @var array
  25. */
  26. private $formats;
  27. /**
  28. * @param ObjectManagerInterface $objectManger
  29. * @param LoggerInterface $logger
  30. * @param array $formats
  31. */
  32. public function __construct(
  33. ObjectManagerInterface $objectManger,
  34. LoggerInterface $logger,
  35. array $formats
  36. ) {
  37. $this->objectManager = $objectManger;
  38. $this->logger = $logger;
  39. $this->formats = $formats;
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function create(array $data, string $format = FeedFactoryInterface::FORMAT_RSS) : FeedInterface
  45. {
  46. if (!isset($this->formats[$format])) {
  47. throw new \Magento\Framework\Exception\InputException(
  48. new \Magento\Framework\Phrase('The format is not supported')
  49. );
  50. }
  51. if (!is_subclass_of($this->formats[$format], \Magento\Framework\App\FeedInterface::class)) {
  52. throw new \Magento\Framework\Exception\InputException(
  53. new \Magento\Framework\Phrase('Wrong format handler type')
  54. );
  55. }
  56. try {
  57. return $this->objectManager->create(
  58. $this->formats[$format],
  59. ['data' => $data]
  60. );
  61. } catch (\Exception $e) {
  62. $this->logger->error($e->getMessage());
  63. throw new \Magento\Framework\Exception\RuntimeException(
  64. new \Magento\Framework\Phrase('There has been an error with import'),
  65. $e
  66. );
  67. }
  68. }
  69. }