Skip to content

Commit 30caed7

Browse files
committed
Reduce class complexity by quick refactor
1 parent 25772e6 commit 30caed7

File tree

1 file changed

+55
-17
lines changed

1 file changed

+55
-17
lines changed

app/code/Magento/Catalog/Model/Product/TierPriceManagement.php

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
use Magento\Customer\Api\GroupRepositoryInterface;
1313
use Magento\Framework\Exception\CouldNotSaveException;
1414
use Magento\Framework\Exception\InputException;
15+
use Magento\Framework\Exception\LocalizedException;
1516
use Magento\Framework\Exception\TemporaryStateExceptionInterface;
17+
use Magento\Store\Api\Data\WebsiteInterface;
18+
use Magento\Store\Model\ScopeInterface;
1619

1720
/**
18-
* Product tier price management
19-
*
2021
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2122
*/
2223
class TierPriceManagement implements \Magento\Catalog\Api\ProductTierPriceManagementInterface
@@ -100,7 +101,7 @@ public function add($sku, $customerGroupId, $price, $qty)
100101
$product = $this->productRepository->get($sku, ['edit_mode' => true]);
101102
$tierPrices = $product->getData('tier_price');
102103
$websiteIdentifier = 0;
103-
$value = $this->config->getValue('catalog/price/scope', \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE);
104+
$value = $this->config->getValue('catalog/price/scope', ScopeInterface::SCOPE_WEBSITE);
104105
if ($value != 0) {
105106
$websiteIdentifier = $this->storeManager->getWebsite()->getId();
106107
}
@@ -160,9 +161,8 @@ public function remove($sku, $customerGroupId, $qty)
160161
{
161162
$product = $this->productRepository->get($sku, ['edit_mode' => true]);
162163
$websiteIdentifier = 0;
163-
$value = $this->config->getValue('catalog/price/scope', \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE);
164-
if ($value != 0) {
165-
$websiteIdentifier = $this->storeManager->getWebsite()->getId();
164+
if ($this->getPriceScopeConfig() !== 0) {
165+
$websiteIdentifier = $this->getCurrentWebsite()->getId();
166166
}
167167
$this->priceModifier->removeTierPrice($product, $customerGroupId, $qty, $websiteIdentifier);
168168
return true;
@@ -175,23 +175,17 @@ public function getList($sku, $customerGroupId)
175175
{
176176
$product = $this->productRepository->get($sku, ['edit_mode' => true]);
177177

178-
$priceKey = 'website_price';
179-
$value = $this->config->getValue('catalog/price/scope', \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE);
180-
if ($value == 0) {
181-
$priceKey = 'price';
182-
}
183-
184-
$cgi = ($customerGroupId === 'all'
178+
$cgi = $customerGroupId === 'all'
185179
? $this->groupManagement->getAllCustomersGroup()->getId()
186-
: $customerGroupId);
180+
: $customerGroupId;
187181

188182
$prices = [];
189183
$tierPrices = $product->getData('tier_price');
190184
if ($tierPrices !== null) {
185+
$priceKey = $this->getPriceKey();
186+
191187
foreach ($tierPrices as $price) {
192-
if ((is_numeric($customerGroupId) && (int) $price['cust_group'] === (int) $customerGroupId)
193-
|| ($customerGroupId === 'all' && $price['all_groups'])
194-
) {
188+
if ($this->isCustomerGroupApplicable($customerGroupId, $price)) {
195189
/** @var \Magento\Catalog\Api\Data\ProductTierPriceInterface $tierPrice */
196190
$tierPrice = $this->priceFactory->create();
197191
$tierPrice->setValue($price[$priceKey])
@@ -203,4 +197,48 @@ public function getList($sku, $customerGroupId)
203197
}
204198
return $prices;
205199
}
200+
201+
/**
202+
* Returns attribute code (key) that contains price
203+
*
204+
* @return string
205+
*/
206+
private function getPriceKey(): string
207+
{
208+
return $this->getPriceScopeConfig() === 0 ? 'price' : 'website_price';
209+
}
210+
211+
/**
212+
* Returns whether Price is applicable for provided Customer Group
213+
*
214+
* @param string $customerGroupId
215+
* @param array $priceArray
216+
* @return bool
217+
*/
218+
private function isCustomerGroupApplicable(string $customerGroupId, array $priceArray): bool
219+
{
220+
return ($customerGroupId === 'all' && $priceArray['all_groups'])
221+
|| (is_numeric($customerGroupId) && (int)$priceArray['cust_group'] === (int)$customerGroupId);
222+
}
223+
224+
/**
225+
* Returns current Price Scope configuration value
226+
*
227+
* @return string
228+
*/
229+
private function getPriceScopeConfig(): string
230+
{
231+
return (int)$this->config->getValue('catalog/price/scope', ScopeInterface::SCOPE_WEBSITE);
232+
}
233+
234+
/**
235+
* Returns current Website object
236+
*
237+
* @return WebsiteInterface
238+
* @throws LocalizedException
239+
*/
240+
private function getCurrentWebsite(): WebsiteInterface
241+
{
242+
return $this->storeManager->getWebsite();
243+
}
206244
}

0 commit comments

Comments
 (0)