From 6076fdcd20ebc1b6e9e1f10b5c99ea4e28f46365 Mon Sep 17 00:00:00 2001
From: Jeff Coleman <jeff@jeffcolemanwrites.com>
Date: Sun, 31 Mar 2019 07:10:58 -0700
Subject: [PATCH 1/5] Setting initial downloadable item status to available if
 \Magento\Downloadable\Model\Link\Purchased\Item::XML_PATH_ORDER_ITEM_STATUS
 is set to 'Pending' (https://github.com/magento/magento2/issues/21753)

---
 .../Observer/SaveDownloadableOrderItemObserver.php         | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/app/code/Magento/Downloadable/Observer/SaveDownloadableOrderItemObserver.php b/app/code/Magento/Downloadable/Observer/SaveDownloadableOrderItemObserver.php
index 64305cfce9b08..ef763336a6515 100644
--- a/app/code/Magento/Downloadable/Observer/SaveDownloadableOrderItemObserver.php
+++ b/app/code/Magento/Downloadable/Observer/SaveDownloadableOrderItemObserver.php
@@ -92,6 +92,11 @@ public function execute(\Magento\Framework\Event\Observer $observer)
         if ($purchasedLink->getId()) {
             return $this;
         }
+        $orderItemStatusToEnable = $this->_scopeConfig->getValue(
+            \Magento\Downloadable\Model\Link\Purchased\Item::XML_PATH_ORDER_ITEM_STATUS,
+            ScopeInterface::SCOPE_STORE,
+            $orderItem->getOrder()->getStoreId()
+        );
         if (!$product) {
             $product = $this->_createProductModel()->setStoreId(
                 $orderItem->getOrder()->getStoreId()
@@ -150,6 +155,8 @@ public function execute(\Magento\Framework\Event\Observer $observer)
                         )->setNumberOfDownloadsBought(
                             $numberOfDownloads
                         )->setStatus(
+                            1 == $orderItemStatusToEnable ?
+                            \Magento\Downloadable\Model\Link\Purchased\Item::LINK_STATUS_AVAILABLE :
                             \Magento\Downloadable\Model\Link\Purchased\Item::LINK_STATUS_PENDING
                         )->setCreatedAt(
                             $orderItem->getCreatedAt()

From d32da36e6cbddc176524ac4b68dc6fb78e1eda90 Mon Sep 17 00:00:00 2001
From: Jeff Coleman <jeff@jeffcolemanwrites.com>
Date: Sun, 31 Mar 2019 08:17:45 -0700
Subject: [PATCH 2/5] fix for coding style

---
 .../Observer/SaveDownloadableOrderItemObserver.php            | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/app/code/Magento/Downloadable/Observer/SaveDownloadableOrderItemObserver.php b/app/code/Magento/Downloadable/Observer/SaveDownloadableOrderItemObserver.php
index ef763336a6515..f2a1e981c2357 100644
--- a/app/code/Magento/Downloadable/Observer/SaveDownloadableOrderItemObserver.php
+++ b/app/code/Magento/Downloadable/Observer/SaveDownloadableOrderItemObserver.php
@@ -92,7 +92,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
         if ($purchasedLink->getId()) {
             return $this;
         }
-        $orderItemStatusToEnable = $this->_scopeConfig->getValue(
+        $statusToEnable = $this->_scopeConfig->getValue(
             \Magento\Downloadable\Model\Link\Purchased\Item::XML_PATH_ORDER_ITEM_STATUS,
             ScopeInterface::SCOPE_STORE,
             $orderItem->getOrder()->getStoreId()
@@ -155,7 +155,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
                         )->setNumberOfDownloadsBought(
                             $numberOfDownloads
                         )->setStatus(
-                            1 == $orderItemStatusToEnable ?
+                            1 == $statusToEnable ?
                             \Magento\Downloadable\Model\Link\Purchased\Item::LINK_STATUS_AVAILABLE :
                             \Magento\Downloadable\Model\Link\Purchased\Item::LINK_STATUS_PENDING
                         )->setCreatedAt(

From 2c166978e48e502ca86c267e07acc075a1720ec7 Mon Sep 17 00:00:00 2001
From: Jeff Coleman <jeff@jeffcolemanwrites.com>
Date: Sun, 31 Mar 2019 15:27:25 -0700
Subject: [PATCH 3/5] renamed variable for clarity and used defined constant
 for comparison

---
 .../Observer/SaveDownloadableOrderItemObserver.php          | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/app/code/Magento/Downloadable/Observer/SaveDownloadableOrderItemObserver.php b/app/code/Magento/Downloadable/Observer/SaveDownloadableOrderItemObserver.php
index f2a1e981c2357..fb4af4c3443a9 100644
--- a/app/code/Magento/Downloadable/Observer/SaveDownloadableOrderItemObserver.php
+++ b/app/code/Magento/Downloadable/Observer/SaveDownloadableOrderItemObserver.php
@@ -92,7 +92,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
         if ($purchasedLink->getId()) {
             return $this;
         }
-        $statusToEnable = $this->_scopeConfig->getValue(
+        $orderStatusToEnableItem = $this->_scopeConfig->getValue(
             \Magento\Downloadable\Model\Link\Purchased\Item::XML_PATH_ORDER_ITEM_STATUS,
             ScopeInterface::SCOPE_STORE,
             $orderItem->getOrder()->getStoreId()
@@ -155,10 +155,10 @@ public function execute(\Magento\Framework\Event\Observer $observer)
                         )->setNumberOfDownloadsBought(
                             $numberOfDownloads
                         )->setStatus(
-                            1 == $statusToEnable ?
+                            \Magento\Sales\Model\Order\Item::STATUS_PENDING == $orderStatusToEnableItem ?
                             \Magento\Downloadable\Model\Link\Purchased\Item::LINK_STATUS_AVAILABLE :
                             \Magento\Downloadable\Model\Link\Purchased\Item::LINK_STATUS_PENDING
-                        )->setCreatedAt(
+                       )->setCreatedAt(
                             $orderItem->getCreatedAt()
                         )->setUpdatedAt(
                             $orderItem->getUpdatedAt()

From 5410e077c534127f3492d53f8ab3981691bed2e3 Mon Sep 17 00:00:00 2001
From: Jeff Coleman <jeff@jeffcolemanwrites.com>
Date: Wed, 3 Apr 2019 00:42:30 -0700
Subject: [PATCH 4/5] fix to call Magento\Sales\Model\Order::getStoreId() only
 once

---
 .../Observer/SaveDownloadableOrderItemObserver.php           | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/app/code/Magento/Downloadable/Observer/SaveDownloadableOrderItemObserver.php b/app/code/Magento/Downloadable/Observer/SaveDownloadableOrderItemObserver.php
index fb4af4c3443a9..19339e4484ef6 100644
--- a/app/code/Magento/Downloadable/Observer/SaveDownloadableOrderItemObserver.php
+++ b/app/code/Magento/Downloadable/Observer/SaveDownloadableOrderItemObserver.php
@@ -92,14 +92,15 @@ public function execute(\Magento\Framework\Event\Observer $observer)
         if ($purchasedLink->getId()) {
             return $this;
         }
+        $storeId = $orderItem->getOrder()->getStoreId();
         $orderStatusToEnableItem = $this->_scopeConfig->getValue(
             \Magento\Downloadable\Model\Link\Purchased\Item::XML_PATH_ORDER_ITEM_STATUS,
             ScopeInterface::SCOPE_STORE,
-            $orderItem->getOrder()->getStoreId()
+            $storeId
         );
         if (!$product) {
             $product = $this->_createProductModel()->setStoreId(
-                $orderItem->getOrder()->getStoreId()
+                $storeId
             )->load(
                 $orderItem->getProductId()
             );

From d189b695f831c099cb050585e76c42d3894ca324 Mon Sep 17 00:00:00 2001
From: Pavel Bystritsky <p.bystritsky@yandex.ru>
Date: Fri, 12 Apr 2019 14:49:46 +0300
Subject: [PATCH 5/5] magento/magento2#22073: Static test fix.

---
 .../Observer/SaveDownloadableOrderItemObserver.php   | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/app/code/Magento/Downloadable/Observer/SaveDownloadableOrderItemObserver.php b/app/code/Magento/Downloadable/Observer/SaveDownloadableOrderItemObserver.php
index 19339e4484ef6..7c1d2748a3e9c 100644
--- a/app/code/Magento/Downloadable/Observer/SaveDownloadableOrderItemObserver.php
+++ b/app/code/Magento/Downloadable/Observer/SaveDownloadableOrderItemObserver.php
@@ -9,6 +9,8 @@
 use Magento\Store\Model\ScopeInterface;
 
 /**
+ * Saves data from order to purchased links.
+ *
  * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  */
 class SaveDownloadableOrderItemObserver implements ObserverInterface
@@ -159,7 +161,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
                             \Magento\Sales\Model\Order\Item::STATUS_PENDING == $orderStatusToEnableItem ?
                             \Magento\Downloadable\Model\Link\Purchased\Item::LINK_STATUS_AVAILABLE :
                             \Magento\Downloadable\Model\Link\Purchased\Item::LINK_STATUS_PENDING
-                       )->setCreatedAt(
+                        )->setCreatedAt(
                             $orderItem->getCreatedAt()
                         )->setUpdatedAt(
                             $orderItem->getUpdatedAt()
@@ -173,6 +175,8 @@ public function execute(\Magento\Framework\Event\Observer $observer)
     }
 
     /**
+     * Create purchased model.
+     *
      * @return \Magento\Downloadable\Model\Link\Purchased
      */
     protected function _createPurchasedModel()
@@ -181,6 +185,8 @@ protected function _createPurchasedModel()
     }
 
     /**
+     * Create product model.
+     *
      * @return \Magento\Catalog\Model\Product
      */
     protected function _createProductModel()
@@ -189,6 +195,8 @@ protected function _createProductModel()
     }
 
     /**
+     * Create purchased item model.
+     *
      * @return \Magento\Downloadable\Model\Link\Purchased\Item
      */
     protected function _createPurchasedItemModel()
@@ -197,6 +205,8 @@ protected function _createPurchasedItemModel()
     }
 
     /**
+     * Create items collection.
+     *
      * @return \Magento\Downloadable\Model\ResourceModel\Link\Purchased\Item\Collection
      */
     protected function _createItemsCollection()