1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Copyright © 2016-present Spryker Systems GmbH. All rights reserved. |
||
5 | * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
||
6 | */ |
||
7 | |||
8 | namespace Spryker\Client\PriceProductStorage\Storage; |
||
9 | |||
10 | use Generated\Shared\Transfer\CurrencyTransfer; |
||
11 | use Generated\Shared\Transfer\MoneyValueTransfer; |
||
12 | use Generated\Shared\Transfer\PriceProductDimensionTransfer; |
||
13 | use Generated\Shared\Transfer\PriceProductStorageTransfer; |
||
14 | use Generated\Shared\Transfer\PriceProductTransfer; |
||
15 | use Spryker\Client\PriceProductStorage\Dependency\Service\PriceProductStorageToPriceProductServiceInterface; |
||
16 | use Spryker\Shared\PriceProductStorage\PriceProductStorageConfig; |
||
17 | use Spryker\Shared\PriceProductStorage\PriceProductStorageConstants; |
||
18 | |||
19 | class PriceProductMapper implements PriceProductMapperInterface |
||
20 | { |
||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | protected const INDEX_SEPARATOR = '-'; |
||
25 | |||
26 | /** |
||
27 | * @var \Spryker\Client\PriceProductStorage\Dependency\Service\PriceProductStorageToPriceProductServiceInterface |
||
28 | */ |
||
29 | protected $priceProductService; |
||
30 | |||
31 | /** |
||
32 | * @param \Spryker\Client\PriceProductStorage\Dependency\Service\PriceProductStorageToPriceProductServiceInterface $priceProductService |
||
33 | */ |
||
34 | public function __construct(PriceProductStorageToPriceProductServiceInterface $priceProductService) |
||
35 | { |
||
36 | $this->priceProductService = $priceProductService; |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * @param \Generated\Shared\Transfer\PriceProductStorageTransfer $priceProductStorageTransfer |
||
41 | * |
||
42 | * @return array<\Generated\Shared\Transfer\PriceProductTransfer> |
||
43 | */ |
||
44 | public function mapPriceProductStorageTransferToPriceProductTransfers(PriceProductStorageTransfer $priceProductStorageTransfer): array |
||
45 | { |
||
46 | $priceProductTransfers = []; |
||
47 | foreach ($priceProductStorageTransfer->getPrices() as $currencyCode => $prices) { |
||
48 | $this->getPriceProductTransfersFromPriceData($priceProductTransfers, $prices, $currencyCode); |
||
49 | } |
||
50 | |||
51 | return array_values($priceProductTransfers); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @param array<\Generated\Shared\Transfer\PriceProductTransfer> $priceProductTransfers |
||
56 | * @param array $prices |
||
57 | * @param string $currencyCode |
||
58 | * |
||
59 | * @return void |
||
60 | */ |
||
61 | protected function getPriceProductTransfersFromPriceData( |
||
62 | array &$priceProductTransfers, |
||
63 | array $prices, |
||
64 | string $currencyCode |
||
65 | ): void { |
||
66 | foreach (PriceProductStorageConfig::PRICE_MODES as $priceMode) { |
||
67 | if (!isset($prices[$priceMode])) { |
||
68 | continue; |
||
69 | } |
||
70 | foreach ($prices[$priceMode] as $priceAttribute => $priceValue) { |
||
71 | $priceProductTransfer = $this->findProductTransferInCollection($currencyCode, $priceAttribute, $priceProductTransfers); |
||
72 | |||
73 | $priceProductTransfer->setGroupKey($this->priceProductService->buildPriceProductGroupKey($priceProductTransfer)) |
||
74 | ->setIsMergeable(true); |
||
75 | |||
76 | if ($priceMode === PriceProductStorageConfig::PRICE_GROSS_MODE) { |
||
77 | $priceProductTransfer->getMoneyValue()->setGrossAmount($priceValue); |
||
78 | $priceProductTransfer = $this->setPriceData($priceProductTransfer, $prices); |
||
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
79 | |||
80 | continue; |
||
81 | } |
||
82 | |||
83 | $priceProductTransfer->getMoneyValue()->setNetAmount($priceValue); |
||
84 | $priceProductTransfer = $this->setPriceData($priceProductTransfer, $prices); |
||
85 | } |
||
86 | } |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @param \Generated\Shared\Transfer\PriceProductTransfer $priceProductTransfer |
||
91 | * @param array $prices |
||
92 | * |
||
93 | * @return \Generated\Shared\Transfer\PriceProductTransfer |
||
94 | */ |
||
95 | protected function setPriceData(PriceProductTransfer $priceProductTransfer, array $prices): PriceProductTransfer |
||
96 | { |
||
97 | $moneyValueTransfer = $priceProductTransfer->getMoneyValue(); |
||
98 | |||
99 | $priceData = $this->resolvePriceData($priceProductTransfer, $prices); |
||
100 | if ($priceData !== null) { |
||
101 | $moneyValueTransfer->setPriceData($priceData); |
||
102 | } |
||
103 | |||
104 | if (isset($prices[PriceProductStorageConfig::PRICE_DATA_BY_PRICE_TYPE])) { |
||
105 | $moneyValueTransfer->setPriceDataByPriceType($prices[PriceProductStorageConfig::PRICE_DATA_BY_PRICE_TYPE]); |
||
106 | } |
||
107 | |||
108 | return $priceProductTransfer; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @param \Generated\Shared\Transfer\PriceProductTransfer $priceProductTransfer |
||
113 | * @param array $prices |
||
114 | * |
||
115 | * @return string|null |
||
116 | */ |
||
117 | protected function resolvePriceData(PriceProductTransfer $priceProductTransfer, array $prices): ?string |
||
118 | { |
||
119 | $priceTypeName = $priceProductTransfer->getPriceTypeName(); |
||
120 | if (isset($prices[PriceProductStorageConfig::PRICE_DATA_BY_PRICE_TYPE][$priceTypeName])) { |
||
121 | return $prices[PriceProductStorageConfig::PRICE_DATA_BY_PRICE_TYPE][$priceTypeName]; |
||
122 | } |
||
123 | |||
124 | return $prices[PriceProductStorageConfig::PRICE_DATA] ?? null; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @param string $currencyCode |
||
129 | * @param string $priceType |
||
130 | * @param array<\Generated\Shared\Transfer\PriceProductTransfer> $priceProductTransfers |
||
131 | * |
||
132 | * @return \Generated\Shared\Transfer\PriceProductTransfer |
||
133 | */ |
||
134 | protected function findProductTransferInCollection(string $currencyCode, string $priceType, array &$priceProductTransfers): PriceProductTransfer |
||
135 | { |
||
136 | $index = implode(static::INDEX_SEPARATOR, [ |
||
137 | $currencyCode, |
||
138 | $priceType, |
||
139 | ]); |
||
140 | |||
141 | if (!isset($priceProductTransfers[$index])) { |
||
142 | $priceProductTransfers[$index] = (new PriceProductTransfer()) |
||
143 | ->setPriceDimension( |
||
144 | (new PriceProductDimensionTransfer()) |
||
145 | ->setType(PriceProductStorageConstants::PRICE_DIMENSION_DEFAULT), |
||
146 | ) |
||
147 | ->setMoneyValue( |
||
148 | (new MoneyValueTransfer()) |
||
149 | ->setCurrency((new CurrencyTransfer())->setCode($currencyCode)), |
||
150 | ) |
||
151 | ->setPriceTypeName($priceType); |
||
152 | } |
||
153 | |||
154 | return $priceProductTransfers[$index]; |
||
155 | } |
||
156 | } |
||
157 |