77namespace Magento \Sales \Model \Order ;
88
99use Magento \Framework \Api \AttributeValueFactory ;
10+ use Magento \Framework \App \Config \ScopeConfigInterface ;
1011use Magento \Framework \App \ObjectManager ;
1112use Magento \Framework \Pricing \PriceCurrencyInterface ;
1213use Magento \Sales \Api \Data \CreditmemoInterface ;
14+ use Magento \Sales \Api \OrderRepositoryInterface ;
1315use Magento \Sales \Model \AbstractModel ;
1416use Magento \Sales \Model \EntityInterface ;
15- use Magento \Sales \Model \Order \InvoiceFactory ;
16- use Magento \Framework \App \Config \ScopeConfigInterface ;
1717
1818/**
1919 * Order creditmemo model
@@ -126,6 +126,11 @@ class Creditmemo extends AbstractModel implements EntityInterface, CreditmemoInt
126126 */
127127 private $ scopeConfig ;
128128
129+ /**
130+ * @var OrderRepositoryInterface
131+ */
132+ private $ orderRepository ;
133+
129134 /**
130135 * @param \Magento\Framework\Model\Context $context
131136 * @param \Magento\Framework\Registry $registry
@@ -144,6 +149,7 @@ class Creditmemo extends AbstractModel implements EntityInterface, CreditmemoInt
144149 * @param array $data
145150 * @param InvoiceFactory $invoiceFactory
146151 * @param ScopeConfigInterface $scopeConfig
152+ * @param OrderRepositoryInterface $orderRepository
147153 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
148154 */
149155 public function __construct (
@@ -163,7 +169,8 @@ public function __construct(
163169 \Magento \Framework \Data \Collection \AbstractDb $ resourceCollection = null ,
164170 array $ data = [],
165171 InvoiceFactory $ invoiceFactory = null ,
166- ScopeConfigInterface $ scopeConfig = null
172+ ScopeConfigInterface $ scopeConfig = null ,
173+ OrderRepositoryInterface $ orderRepository = null
167174 ) {
168175 $ this ->_creditmemoConfig = $ creditmemoConfig ;
169176 $ this ->_orderFactory = $ orderFactory ;
@@ -175,6 +182,7 @@ public function __construct(
175182 $ this ->priceCurrency = $ priceCurrency ;
176183 $ this ->invoiceFactory = $ invoiceFactory ?: ObjectManager::getInstance ()->get (InvoiceFactory::class);
177184 $ this ->scopeConfig = $ scopeConfig ?: ObjectManager::getInstance ()->get (ScopeConfigInterface::class);
185+ $ this ->orderRepository = $ orderRepository ?? ObjectManager::getInstance ()->get (OrderRepositoryInterface::class);
178186 parent ::__construct (
179187 $ context ,
180188 $ registry ,
@@ -237,8 +245,11 @@ public function setOrder(\Magento\Sales\Model\Order $order)
237245 public function getOrder ()
238246 {
239247 if (!$ this ->_order instanceof \Magento \Sales \Model \Order) {
240- $ this ->_order = $ this ->_orderFactory ->create ()->load ($ this ->getOrderId ());
248+ $ this ->_order = $ this ->getOrderId () ?
249+ $ this ->orderRepository ->get ($ this ->getOrderId ()) :
250+ $ this ->_orderFactory ->create ();
241251 }
252+
242253 return $ this ->_order ->setHistoryEntityName ($ this ->entityType );
243254 }
244255
@@ -449,6 +460,7 @@ public function canVoid()
449460 * Retrieve Creditmemo states array
450461 *
451462 * @return array
463+ * phpcs:disable Magento2.Functions.StaticFunction
452464 */
453465 public static function getStates ()
454466 {
@@ -461,11 +473,12 @@ public static function getStates()
461473 }
462474 return static ::$ _states ;
463475 }
476+ // phpcs:enable
464477
465478 /**
466479 * Retrieve Creditmemo state name by state identifier
467480 *
468- * @param int $stateId
481+ * @param int $stateId
469482 * @return \Magento\Framework\Phrase
470483 */
471484 public function getStateName ($ stateId = null )
0 commit comments