Skip to content

Commit

Permalink
ENGCOM-7993: Reduce TierPriceManagement class complexity by quick ref…
Browse files Browse the repository at this point in the history
…actor #29202

 - Merge Pull Request #29202 from lbajsarowicz/magento2:refactor/tier-price-management
 - Merged commits:
   1. 30caed7
   2. 3ec0f57
  • Loading branch information
magento-engcom-team committed Aug 14, 2020
2 parents 7198c76 + 3ec0f57 commit c229b5b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 18 deletions.
72 changes: 55 additions & 17 deletions app/code/Magento/Catalog/Model/Product/TierPriceManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
use Magento\Customer\Api\GroupRepositoryInterface;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\TemporaryStateExceptionInterface;
use Magento\Store\Api\Data\WebsiteInterface;
use Magento\Store\Model\ScopeInterface;

/**
* Product tier price management
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class TierPriceManagement implements \Magento\Catalog\Api\ProductTierPriceManagementInterface
Expand Down Expand Up @@ -100,7 +101,7 @@ public function add($sku, $customerGroupId, $price, $qty)
$product = $this->productRepository->get($sku, ['edit_mode' => true]);
$tierPrices = $product->getData('tier_price');
$websiteIdentifier = 0;
$value = $this->config->getValue('catalog/price/scope', \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE);
$value = $this->config->getValue('catalog/price/scope', ScopeInterface::SCOPE_WEBSITE);
if ($value != 0) {
$websiteIdentifier = $this->storeManager->getWebsite()->getId();
}
Expand Down Expand Up @@ -160,9 +161,8 @@ public function remove($sku, $customerGroupId, $qty)
{
$product = $this->productRepository->get($sku, ['edit_mode' => true]);
$websiteIdentifier = 0;
$value = $this->config->getValue('catalog/price/scope', \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE);
if ($value != 0) {
$websiteIdentifier = $this->storeManager->getWebsite()->getId();
if ($this->getPriceScopeConfig() !== 0) {
$websiteIdentifier = $this->getCurrentWebsite()->getId();
}
$this->priceModifier->removeTierPrice($product, $customerGroupId, $qty, $websiteIdentifier);
return true;
Expand All @@ -175,23 +175,17 @@ public function getList($sku, $customerGroupId)
{
$product = $this->productRepository->get($sku, ['edit_mode' => true]);

$priceKey = 'website_price';
$value = $this->config->getValue('catalog/price/scope', \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE);
if ($value == 0) {
$priceKey = 'price';
}

$cgi = ($customerGroupId === 'all'
$cgi = $customerGroupId === 'all'
? $this->groupManagement->getAllCustomersGroup()->getId()
: $customerGroupId);
: $customerGroupId;

$prices = [];
$tierPrices = $product->getData('tier_price');
if ($tierPrices !== null) {
$priceKey = $this->getPriceKey();

foreach ($tierPrices as $price) {
if ((is_numeric($customerGroupId) && (int) $price['cust_group'] === (int) $customerGroupId)
|| ($customerGroupId === 'all' && $price['all_groups'])
) {
if ($this->isCustomerGroupApplicable($customerGroupId, $price)) {
/** @var \Magento\Catalog\Api\Data\ProductTierPriceInterface $tierPrice */
$tierPrice = $this->priceFactory->create();
$tierPrice->setValue($price[$priceKey])
Expand All @@ -203,4 +197,48 @@ public function getList($sku, $customerGroupId)
}
return $prices;
}

/**
* Returns attribute code (key) that contains price
*
* @return string
*/
private function getPriceKey(): string
{
return $this->getPriceScopeConfig() === 0 ? 'price' : 'website_price';
}

/**
* Returns whether Price is applicable for provided Customer Group
*
* @param string $customerGroupId
* @param array $priceArray
* @return bool
*/
private function isCustomerGroupApplicable(string $customerGroupId, array $priceArray): bool
{
return ($customerGroupId === 'all' && $priceArray['all_groups'])
|| (is_numeric($customerGroupId) && (int)$priceArray['cust_group'] === (int)$customerGroupId);
}

/**
* Returns current Price Scope configuration value
*
* @return int
*/
private function getPriceScopeConfig(): int
{
return (int)$this->config->getValue('catalog/price/scope', ScopeInterface::SCOPE_WEBSITE);
}

/**
* Returns current Website object
*
* @return WebsiteInterface
* @throws LocalizedException
*/
private function getCurrentWebsite(): WebsiteInterface
{
return $this->storeManager->getWebsite();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ public function testSuccessDeleteTierPrice()
->method('getValue')
->with('catalog/price/scope', ScopeInterface::SCOPE_WEBSITE)
->willReturn(0);
$this->priceModifierMock->expects($this->once())->method('removeTierPrice')->with($this->productMock, 4, 5, 0);
$this->priceModifierMock->expects($this->once())
->method('removeTierPrice')
->with($this->productMock, 4, 5, 0);

$this->assertTrue($this->service->remove('product_sku', 4, 5, 0));
}
Expand Down

0 comments on commit c229b5b

Please sign in to comment.