Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce TierPriceManagement class complexity by quick refactor #29202

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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