NotifyStock.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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\InventoryLowQuantityNotificationAdminUi\Block\Adminhtml\Rss;
  8. use Magento\Backend\Block\AbstractBlock;
  9. use Magento\Backend\Block\Context;
  10. use Magento\Framework\App\Rss\DataProviderInterface;
  11. use Magento\Framework\App\Rss\UrlBuilderInterface;
  12. use Magento\InventoryApi\Api\Data\SourceItemInterface;
  13. use Magento\InventoryLowQuantityNotification\Model\ResourceModel\Rss\NotifyStock\GetSourceItemsCollection;
  14. class NotifyStock extends AbstractBlock implements DataProviderInterface
  15. {
  16. /**
  17. * @var UrlBuilderInterface
  18. */
  19. private $rssUrlBuilder;
  20. /**
  21. * @var GetSourceItemsCollection
  22. */
  23. private $getSourceItemsCollection;
  24. /**
  25. * @param Context $context
  26. * @param GetSourceItemsCollection $getSourceItemsCollection
  27. * @param UrlBuilderInterface $rssUrlBuilder
  28. * @param array $data
  29. */
  30. public function __construct(
  31. Context $context,
  32. GetSourceItemsCollection $getSourceItemsCollection,
  33. UrlBuilderInterface $rssUrlBuilder,
  34. array $data = []
  35. ) {
  36. parent::__construct($context, $data);
  37. $this->rssUrlBuilder = $rssUrlBuilder;
  38. $this->getSourceItemsCollection = $getSourceItemsCollection;
  39. }
  40. /**
  41. * @return void
  42. */
  43. protected function _construct()
  44. {
  45. $this->setCacheKey('rss_catalog_notifystock');
  46. parent::_construct();
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function getRssData()
  52. {
  53. $newUrl = $this->rssUrlBuilder->getUrl(['_secure' => true, '_nosecret' => true, 'type' => 'notifystock']);
  54. $title = __('Low Stock Products');
  55. $data = ['title' => $title, 'description' => $title, 'link' => $newUrl, 'charset' => 'UTF-8'];
  56. foreach ($this->getSourceItemsCollection->execute() as $item) {
  57. $url = $this->getUrl(
  58. 'catalog/product/edit',
  59. ['id' => $item->getId(), '_secure' => true, '_nosecret' => true]
  60. );
  61. $qty = (float)$item->getData('qty');
  62. $description = __(
  63. '%1 has reached a quantity of %2 in source %3(Source Code: %4).',
  64. $item->getData('name'),
  65. $qty,
  66. $item->getData('source_name'),
  67. $item->getData(SourceItemInterface::SOURCE_CODE)
  68. );
  69. $data['entries'][] = ['title' => $item->getData('name'), 'link' => $url, 'description' => $description];
  70. }
  71. return $data;
  72. }
  73. /**
  74. * @inheritdoc
  75. */
  76. public function getCacheLifetime()
  77. {
  78. return 600;
  79. }
  80. /**
  81. * @inheritdoc
  82. */
  83. public function isAllowed()
  84. {
  85. return true;
  86. }
  87. /**
  88. * @inheritdoc
  89. */
  90. public function getFeeds()
  91. {
  92. return [];
  93. }
  94. /**
  95. * @inheritdoc
  96. */
  97. public function isAuthRequired()
  98. {
  99. return true;
  100. }
  101. }