Skip to content

Commit

Permalink
Implemented PartnersSourceAdapter, IntegrationPartner *Implemented me…
Browse files Browse the repository at this point in the history
…thod getOrderByMagento *Implemented method *Added Marketplace configs *Set v1.1.1 *Added partnersResource type
  • Loading branch information
Returnless committed Jul 22, 2021
1 parent 8730806 commit c3428b5
Show file tree
Hide file tree
Showing 8 changed files with 287 additions and 40 deletions.
84 changes: 46 additions & 38 deletions Model/Api/OrderInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

namespace Returnless\Connector\Model\Api;

use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Module\ResourceInterface;
use Returnless\Connector\Api\OrderInfoInterface;
use Magento\Sales\Model\OrderRepository;
use Returnless\Connector\Api\OrderInfoInterface;
use Magento\Catalog\Model\ProductRepository;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Psr\Log\LoggerInterface;
use Magento\Catalog\Helper\Image;
use Returnless\Connector\Model\Config;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\App\ObjectManager;

/**
* Interface OrderInfo
Expand Down Expand Up @@ -45,21 +46,11 @@ class OrderInfo implements OrderInfoInterface
*/
protected $logger;

/**
* @var OrderRepository
*/
protected $orderRepository;

/**
* @var ProductRepository
*/
protected $productRepository;

/**
* @var SearchCriteriaBuilder
*/
protected $searchCriteriaBuilder;

/**
* @var Image
*/
Expand All @@ -70,33 +61,44 @@ class OrderInfo implements OrderInfoInterface
*/
protected $config;

/**
* @var OrderRepository
*/
protected $orderRepository;

/**
* @var SearchCriteriaBuilder
*/
protected $searchCriteriaBuilder;

/**
* OrderInfo constructor.
*
* @param OrderRepository $orderRepository
* @param ProductRepository $productRepository
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param LoggerInterface $logger
* @param Image $image
* @param Config $config
* @param ResourceInterface $moduleResource
* @param OrderRepository $orderRepository
* @param SearchCriteriaBuilder $searchCriteriaBuilder
*/
public function __construct(
OrderRepository $orderRepository,
ProductRepository $productRepository,
SearchCriteriaBuilder $searchCriteriaBuilder,
LoggerInterface $logger,
Image $image,
Config $config,
ResourceInterface $moduleResource
) {
$this->orderRepository = $orderRepository;
ResourceInterface $moduleResource,
OrderRepository $orderRepository,
SearchCriteriaBuilder $searchCriteriaBuilder
)
{
$this->productRepository = $productRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->logger = $logger;
$this->image = $image;
$this->config = $config;
$this->moduleResource = $moduleResource;
$this->orderRepository = $orderRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
}

/**
Expand All @@ -112,7 +114,12 @@ public function getOrderInfoReturnless($incrementId)
$this->logger->debug('[RET_ORDER_INFO] Increment Id', [$incrementId]);

try {
$order = $this->getOrderByIncrementId($incrementId);
$order = $this->getOrderByMagento($incrementId);
if (!$order->getId() && $this->config->getMarketplaceSearchEnabled()) {
/** @var \Returnless\Connector\Model\PartnersSourceAdapter $partnersSourceAdapter */
$partnersSourceAdapter = ObjectManager::getInstance()->get('Returnless\Connector\Model\PartnersSourceAdapter');
$order = $partnersSourceAdapter->getOrderByMarketplace($incrementId);
}

$orderInfo['id'] = $order->getIncrementId();
$orderInfo['order_id'] = $order->getEntityId();
Expand Down Expand Up @@ -253,6 +260,24 @@ public function getOrderInfoReturnless($incrementId)
$this->returnResult($response);
}

/**
* @param $incrementId
* @param string $searchKey
* @return \Magento\Framework\DataObject
*/
private function getOrderByMagento($incrementId, $searchKey = 'increment_id')
{
$searchCriteria = $this->searchCriteriaBuilder
->addFilter(
$searchKey,
$incrementId,
'eq'
)
->create();

return $this->orderRepository->getList($searchCriteria)->getFirstItem();
}

/**
* This method provides an ability to return Response Data
*
Expand Down Expand Up @@ -307,23 +332,6 @@ public function getImageByProduct($product)
return $image;
}

/**
* @param $incrementId
* @return mixed
*/
protected function getOrderByIncrementId($incrementId)
{
$searchCriteria = $this->searchCriteriaBuilder
->addFilter(
'increment_id',
$incrementId,
'eq'
)
->create();

return $this->orderRepository->getList($searchCriteria)->getFirstItem();
}

/**
* @param $result
*/
Expand Down
40 changes: 40 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ class Config
*/
const CONFIG_SEPARATE_BUNDLE_PRODUCTS = 'returnless_connector/general/bundle_enabled';

/**
* const Path to config enabled marketplace search
*/
const CONFIG_MARKETPLACE_SEARCH_ENABLED = 'returnless_connector/marketplace_orders/enabled';

/**
* const Path to config marketplace integration partner
*/
const CONFIG_MARKETPLACE_SEARCH_VENDOR_ID = 'returnless_connector/marketplace_orders/integration_partner';

/**
* Config constructor.
*
Expand All @@ -61,6 +71,36 @@ public function getEnabled($store = null)
return $enabled;
}

/**
* @param null $store
* @return string
*/
public function getMarketplaceSearchEnabled($store = null)
{
$enabled = (string)$this->scopeConfig->getValue(
self::CONFIG_MARKETPLACE_SEARCH_ENABLED,
ScopeInterface::SCOPE_STORE,
$store
);

return $enabled;
}

/**
* @param null $store
* @return string
*/
public function getMarketplaceSearchPartnerId($store = null)
{
$enabled = (string)$this->scopeConfig->getValue(
self::CONFIG_MARKETPLACE_SEARCH_VENDOR_ID,
ScopeInterface::SCOPE_STORE,
$store
);

return $enabled;
}

/**
* @param null $store
* @return string
Expand Down
26 changes: 26 additions & 0 deletions Model/Config/Source/IntegrationPartner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Returnless\Connector\Model\Config\Source;

use Magento\Framework\Option\ArrayInterface;

/**
* Class IntegrationPartner
* @package Returnless\Connector\Model\Config
*/
class IntegrationPartner implements ArrayInterface
{
/**
* @return array
*/
public function toOptionArray()
{
return
[
[
'value' => 'vendiro',
'label' => __('Vendiro')
]
];
}
}
151 changes: 151 additions & 0 deletions Model/PartnersSourceAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php

namespace Returnless\Connector\Model;

use Magento\Sales\Model\OrderRepository;
use Magento\Framework\Api\SearchCriteriaBuilder;;
use Returnless\Connector\Model\Config;
use Psr\Log\LoggerInterface as Logger;

/**
* Class PartnersSourceAdapter
* @package Returnless\Connector\Model
*/
class PartnersSourceAdapter
{
/**
* @var array|mixed
*/
private $partnersResource;

/**
* @var Config
*/
private $config;

/**
* @var OrderRepository
*/
private $orderRepository;

/**
* @var SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;

/**
* @var LoggerInterface
*
*/
private $logger;

/**
* PartnersSourceAdapter constructor.
* @param OrderRepository $orderRepository
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param \Returnless\Connector\Model\Config $config
* @param array $partnersResource
*/
public function __construct(
OrderRepository $orderRepository,
SearchCriteriaBuilder $searchCriteriaBuilder,
Config $config,
Logger $logger,
$partnersResource = []
) {
$this->config = $config;
$this->logger = $logger;
$this->orderRepository = $orderRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->partnersResource = $partnersResource;
}

/**
* @deprecated
*
* @param $orderId
* @return \Magento\Framework\DataObject|mixed
*/
public function getOrderById($orderId)
{
$order = $this->getOrderByMagento($orderId);
if (!$order->getId() && $this->config->getMarketplaceSearchEnabled()) {
try {
$order = $this->getOrderByMarketplace($orderId);
} catch (\Throwable $e) {
$this->logger->critical('Returnless Error PartnersSourceAdapter', ['exception' => $e]);
}
}

return $order;
}

/**
* @param $orderId
* @return \Magento\Framework\DataObject|mixed
*/
public function getOrderByMarketplace($orderId)
{
$partnerId = $this->config->getMarketplaceSearchPartnerId();
$collection = $this->getPartnersCollectionModel($partnerId);
$partnerOrder = $collection->addFieldToFilter(
$this->partnersResource[$partnerId]['keyForSearch'],
['eq' => $orderId]
)
->addFieldToSelect('order_id')
->getFirstItem();
$order = $this->getOrderByMagento($partnerOrder->getOrderId(), 'entity_id');

return $order;
}

/**
* @param $incrementId
* @param string $searchKey
* @return \Magento\Framework\DataObject
*/
private function getOrderByMagento($incrementId, $searchKey = 'increment_id')
{
$searchCriteria = $this->searchCriteriaBuilder
->addFilter(
$searchKey,
$incrementId,
'eq'
)
->create();

return $this->orderRepository->getList($searchCriteria)->getFirstItem();
}

/**
* @param $partnerId
* @return false|mixed
*/
private function getPartnersCollectionModel($partnerId)
{
$result = false;
if (!empty($this->partnersResource[$partnerId])) {
$result = $this->partnersResource[$partnerId]['model'];
}

return $result;
}

/**
* @return array
*/
public function getIntegrationPartners()
{
$result = [];
if(!empty($this->partnersResource)) {
foreach ($this->partnersResource as $partnerIntegrationCode => $value) {
$result[] = [
'value' => $partnerIntegrationCode,
'label' => __($value['label'])
];
}
}

return $result;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
"OSL-3.0",
"AFL-3.0"
],
"version": "1.1.0"
"version": "1.1.1"
}
Loading

0 comments on commit c3428b5

Please sign in to comment.