33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+ declare (strict_types=1 );
7+
68namespace Magento \Sales \CustomerData ;
79
10+ use Magento \Catalog \Model \Product ;
11+ use Magento \CatalogInventory \Api \StockRegistryInterface ;
812use Magento \Customer \CustomerData \SectionSourceInterface ;
913use Magento \Catalog \Api \ProductRepositoryInterface ;
14+ use Magento \Customer \Model \Session ;
15+ use Magento \Framework \App \Http \Context ;
1016use Magento \Framework \Exception \NoSuchEntityException ;
17+ use Magento \Sales \Model \Order ;
18+ use Magento \Sales \Model \Order \Config ;
19+ use Magento \Sales \Model \Order \Item ;
20+ use Magento \Sales \Model \ResourceModel \Order \Collection ;
21+ use Magento \Sales \Model \ResourceModel \Order \CollectionFactoryInterface ;
22+ use Magento \Store \Model \StoreManagerInterface ;
1123use Psr \Log \LoggerInterface ;
1224
1325/**
1426 * Returns information for "Recently Ordered" widget.
1527 * It contains list of 5 salable products from the last placed order.
1628 * Qty of products to display is limited by LastOrderedItems::SIDEBAR_ORDER_LIMIT constant.
29+ * @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
1730 */
1831class LastOrderedItems implements SectionSourceInterface
1932{
2033 /**
21- * Limit of orders in side bar
34+ * Limit of orders in sidebar
2235 */
23- const SIDEBAR_ORDER_LIMIT = 5 ;
36+ public const SIDEBAR_ORDER_LIMIT = 5 ;
2437
2538 /**
26- * @var \Magento\Sales\Model\ResourceModel\Order\ CollectionFactoryInterface
39+ * @var CollectionFactoryInterface
2740 */
2841 protected $ _orderCollectionFactory ;
2942
3043 /**
31- * @var \Magento\Sales\Model\Order\ Config
44+ * @var Config
3245 */
3346 protected $ _orderConfig ;
3447
3548 /**
36- * @var \Magento\Customer\Model\ Session
49+ * @var Session
3750 */
3851 protected $ _customerSession ;
3952
4053 /**
41- * @var \Magento\Framework\App\Http\ Context
54+ * @var Context
4255 */
4356 protected $ httpContext ;
4457
4558 /**
46- * @var \Magento\Sales\Model\ResourceModel\Order\ Collection
59+ * @var Collection
4760 */
4861 protected $ orders ;
4962
5063 /**
51- * @var \Magento\CatalogInventory\Api\ StockRegistryInterface
64+ * @var StockRegistryInterface
5265 */
5366 protected $ stockRegistry ;
5467
5568 /**
56- * @var \Magento\Store\Model\ StoreManagerInterface
69+ * @var StoreManagerInterface
5770 */
5871 private $ _storeManager ;
5972
6073 /**
61- * @var \Magento\Catalog\Api\ ProductRepositoryInterface
74+ * @var ProductRepositoryInterface
6275 */
6376 private $ productRepository ;
6477
@@ -68,20 +81,20 @@ class LastOrderedItems implements SectionSourceInterface
6881 private $ logger ;
6982
7083 /**
71- * @param \Magento\Sales\Model\ResourceModel\Order\ CollectionFactoryInterface $orderCollectionFactory
72- * @param \Magento\Sales\Model\Order\ Config $orderConfig
73- * @param \Magento\Customer\Model\ Session $customerSession
74- * @param \Magento\CatalogInventory\Api\ StockRegistryInterface $stockRegistry
75- * @param \Magento\Store\Model\ StoreManagerInterface $storeManager
84+ * @param CollectionFactoryInterface $orderCollectionFactory
85+ * @param Config $orderConfig
86+ * @param Session $customerSession
87+ * @param StockRegistryInterface $stockRegistry
88+ * @param StoreManagerInterface $storeManager
7689 * @param ProductRepositoryInterface $productRepository
7790 * @param LoggerInterface $logger
7891 */
7992 public function __construct (
80- \ Magento \ Sales \ Model \ ResourceModel \ Order \ CollectionFactoryInterface $ orderCollectionFactory ,
81- \ Magento \ Sales \ Model \ Order \ Config $ orderConfig ,
82- \ Magento \ Customer \ Model \ Session $ customerSession ,
83- \ Magento \ CatalogInventory \ Api \ StockRegistryInterface $ stockRegistry ,
84- \ Magento \ Store \ Model \ StoreManagerInterface $ storeManager ,
93+ CollectionFactoryInterface $ orderCollectionFactory ,
94+ Config $ orderConfig ,
95+ Session $ customerSession ,
96+ StockRegistryInterface $ stockRegistry ,
97+ StoreManagerInterface $ storeManager ,
8598 ProductRepositoryInterface $ productRepository ,
8699 LoggerInterface $ logger
87100 ) {
@@ -99,7 +112,7 @@ public function __construct(
99112 *
100113 * @return void
101114 */
102- protected function initOrders ()
115+ protected function initOrders (): void
103116 {
104117 $ customerId = $ this ->_customerSession ->getCustomerId ();
105118
@@ -116,18 +129,19 @@ protected function initOrders()
116129 * Get list of last ordered products
117130 *
118131 * @return array
132+ * @throws NoSuchEntityException
119133 */
120- protected function getItems ()
134+ protected function getItems (): array
121135 {
122136 $ items = [];
123137 $ order = $ this ->getLastOrder ();
124138 $ limit = self ::SIDEBAR_ORDER_LIMIT ;
125139
126140 if ($ order ) {
127141 $ website = $ this ->_storeManager ->getStore ()->getWebsiteId ();
128- /** @var \Magento\Sales\Model\Order\ Item $item */
142+ /** @var Item $item */
129143 foreach ($ order ->getParentItemsRandomCollection ($ limit ) as $ item ) {
130- /** @var \Magento\Catalog\Model\ Product $product */
144+ /** @var Product $product */
131145 try {
132146 $ product = $ this ->productRepository ->getById (
133147 $ item ->getProductId (),
@@ -145,6 +159,7 @@ protected function getItems()
145159 'name ' => $ item ->getName (),
146160 'url ' => $ url ,
147161 'is_saleable ' => $ this ->isItemAvailableForReorder ($ item ),
162+ 'product_id ' => $ item ->getProductId (),
148163 ];
149164 }
150165 }
@@ -156,10 +171,10 @@ protected function getItems()
156171 /**
157172 * Check item product availability for reorder
158173 *
159- * @param \Magento\Sales\Model\Order\ Item $orderItem
174+ * @param Item $orderItem
160175 * @return boolean
161176 */
162- protected function isItemAvailableForReorder (\ Magento \ Sales \ Model \ Order \ Item $ orderItem )
177+ protected function isItemAvailableForReorder (Item $ orderItem )
163178 {
164179 try {
165180 $ stockItem = $ this ->stockRegistry ->getStockItem (
@@ -175,7 +190,7 @@ protected function isItemAvailableForReorder(\Magento\Sales\Model\Order\Item $or
175190 /**
176191 * Last order getter
177192 *
178- * @return \Magento\Sales\Model\ Order|void
193+ * @return Order|void
179194 */
180195 protected function getLastOrder ()
181196 {
@@ -190,7 +205,7 @@ protected function getLastOrder()
190205 /**
191206 * @inheritdoc
192207 */
193- public function getSectionData ()
208+ public function getSectionData (): array
194209 {
195210 return ['items ' => $ this ->getItems ()];
196211 }
0 commit comments