Skip to content

Commit 30901be

Browse files
committed
CR recommendations
1 parent b2021d5 commit 30901be

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

app/code/Magento/CatalogCustomerGraphQl/Model/Resolver/PriceTiers.php

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ class PriceTiers implements ResolverInterface
6666
*/
6767
private $priceCurrency;
6868

69+
/**
70+
* @var array
71+
*/
72+
private $formatAndFilterTierPrices = [];
73+
74+
/**
75+
* @var array
76+
*/
77+
private $tierPricesQty = [];
78+
6979
/**
7080
* @param ValueFactory $valueFactory
7181
* @param TiersFactory $tiersFactory
@@ -115,17 +125,16 @@ public function resolve(
115125
return [];
116126
}
117127

118-
$productId = $product->getId();
128+
$productId = (int)$product->getId();
119129
$this->tiers->addProductFilter($productId);
120130

121131
return $this->valueFactory->create(
122132
function () use ($productId, $context) {
123-
/** @var StoreInterface $store */
124-
$store = $context->getExtensionAttributes()->getStore();
133+
$currencyCode = $context->getExtensionAttributes()->getStore()->getCurrentCurrencyCode();
125134

126135
$productPrice = $this->tiers->getProductRegularPrice($productId) ?? 0.0;
127136
$tierPrices = $this->tiers->getProductTierPrices($productId) ?? [];
128-
return $this->formatAndFilterTierPrices($tierPrices, $productPrice, $store);
137+
return $this->formatAndFilterTierPrices($tierPrices, $productPrice, $currencyCode);
129138
}
130139
);
131140
}
@@ -135,48 +144,45 @@ function () use ($productId, $context) {
135144
*
136145
* @param ProductTierPriceInterface[] $tierPrices
137146
* @param float $productPrice
138-
* @param StoreInterface $store
147+
* @param string $currencyCode
139148
* @return array
140149
*/
141150
private function formatAndFilterTierPrices(
142151
array $tierPrices,
143152
float $productPrice,
144-
StoreInterface $store
153+
string $currencyCode
145154
): array {
146-
$tiers = [];
147-
$qtyCache = [];
148155

149156
foreach ($tierPrices as $key => $tierPrice) {
150-
$this->formatTierPrices($productPrice, $store, $tierPrice, $tiers);
151-
$this->filterTierPrices($tierPrices, $key, $tierPrice, $qtyCache, $tiers);
157+
$tierPrice->setValue($this->priceCurrency->convertAndRound($tierPrice->getValue()));
158+
$this->formatTierPrices($productPrice, $currencyCode, $tierPrice);
159+
$this->filterTierPrices($tierPrices, $key, $tierPrice);
152160
}
153-
return $tiers;
161+
return $this->formatAndFilterTierPrices;
154162
}
155163

156164
/**
157165
* Format tier prices for output
158166
*
159167
* @param float $productPrice
160-
* @param StoreInterface $store
168+
* @param string $currencyCode
161169
* @param ProductTierPriceInterface $tierPrice
162-
* @param array $tiers
163170
*/
164-
private function formatTierPrices(float $productPrice, StoreInterface $store, &$tierPrice, &$tiers)
171+
private function formatTierPrices(float $productPrice, string $currencyCode, $tierPrice)
165172
{
166-
$tierPrice->setValue($this->priceCurrency->convertAndRound($tierPrice->getValue()));
167173
$percentValue = $tierPrice->getExtensionAttributes()->getPercentageValue();
168174
if ($percentValue && is_numeric($percentValue)) {
169175
$discount = $this->discount->getDiscountByPercent($productPrice, (float)$percentValue);
170176
} else {
171177
$discount = $this->discount->getDiscountByDifference($productPrice, (float)$tierPrice->getValue());
172178
}
173179

174-
$tiers[] = [
180+
$this->formatAndFilterTierPrices[] = [
175181
"discount" => $discount,
176182
"quantity" => $tierPrice->getQty(),
177183
"final_price" => [
178184
"value" => $tierPrice->getValue(),
179-
"currency" => $store->getCurrentCurrencyCode()
185+
"currency" => $currencyCode
180186
]
181187
];
182188
}
@@ -187,27 +193,23 @@ private function formatTierPrices(float $productPrice, StoreInterface $store, &$
187193
* @param array $tierPrices
188194
* @param int $key
189195
* @param ProductTierPriceInterface $tierPriceItem
190-
* @param array $qtyCache
191-
* @param array $tiers
192196
*/
193197
private function filterTierPrices(
194198
array $tierPrices,
195199
int $key,
196-
ProductTierPriceInterface $tierPriceItem,
197-
array &$qtyCache,
198-
array &$tiers
200+
ProductTierPriceInterface $tierPriceItem
199201
) {
200202
$qty = $tierPriceItem->getQty();
201-
if (isset($qtyCache[$qty])) {
202-
$priceQty = $qtyCache[$qty];
203+
if (isset($this->tierPricesQty[$qty])) {
204+
$priceQty = $this->tierPricesQty[$qty];
203205
if ((float)$tierPriceItem->getValue() < (float)$tierPrices[$priceQty]->getValue()) {
204-
unset($tiers[$priceQty]);
205-
$qtyCache[$priceQty] = $key;
206+
unset($this->formatAndFilterTierPrices[$priceQty]);
207+
$this->tierPricesQty[$priceQty] = $key;
206208
} else {
207-
unset($tiers[$key]);
209+
unset($this->formatAndFilterTierPrices[$key]);
208210
}
209211
} else {
210-
$qtyCache[$qty] = $key;
212+
$this->tierPricesQty[$qty] = $key;
211213
}
212214
}
213215
}

app/code/Magento/CatalogCustomerGraphQl/Model/Resolver/Product/Price/Tiers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function __construct(
7777
*
7878
* @param int $productId
7979
*/
80-
public function addProductFilter($productId): void
80+
public function addProductFilter(int $productId): void
8181
{
8282
$this->filterProductIds[] = $productId;
8383
}

app/code/Magento/CatalogCustomerGraphQl/Model/Resolver/TierPrices.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function resolve(
8383

8484
/** @var Product $product */
8585
$product = $value['model'];
86-
$productId = $product->getId();
86+
$productId = (int)$product->getId();
8787
$this->tiers->addProductFilter($productId);
8888

8989
return $this->valueFactory->create(

0 commit comments

Comments
 (0)