Failed Conditions
Push — master ( 36cd34...e25ade )
by
unknown
47:16 queued 16:37
created

PossibleCsvHeaderExpander::getCurrencyCodes()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 15
rs 10
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace Spryker\Zed\MerchantProductDataImport\Business\Expander;
9
10
use Generated\Shared\Transfer\MerchantCriteriaTransfer;
11
use Generated\Shared\Transfer\MerchantStockCriteriaTransfer;
12
use Generated\Shared\Transfer\MerchantTransfer;
13
use Generated\Shared\Transfer\StoreTransfer;
14
use Spryker\Zed\MerchantProductDataImport\Dependency\Facade\MerchantProductDataImportToCurrencyFacadeInterface;
15
use Spryker\Zed\MerchantProductDataImport\Dependency\Facade\MerchantProductDataImportToLocaleFacadeInterface;
16
use Spryker\Zed\MerchantProductDataImport\Dependency\Facade\MerchantProductDataImportToMerchantFacadeInterface;
17
use Spryker\Zed\MerchantProductDataImport\Dependency\Facade\MerchantProductDataImportToMerchantStockFacadeInterface;
18
use Spryker\Zed\MerchantProductDataImport\Dependency\Facade\MerchantProductDataImportToProductAttributeFacadeInterface;
19
use Spryker\Zed\MerchantProductDataImport\Dependency\Facade\MerchantProductDataImportToStoreFacadeInterface;
20
use Spryker\Zed\MerchantProductDataImport\MerchantProductDataImportConfig;
21
22
class PossibleCsvHeaderExpander implements PossibleCsvHeaderExpanderInterface
23
{
24
    /**
25
     * @param \Spryker\Zed\MerchantProductDataImport\MerchantProductDataImportConfig $merchantProductDataImportConfig
26
     * @param \Spryker\Zed\MerchantProductDataImport\Dependency\Facade\MerchantProductDataImportToLocaleFacadeInterface $localeFacade
27
     * @param \Spryker\Zed\MerchantProductDataImport\Dependency\Facade\MerchantProductDataImportToMerchantFacadeInterface $merchantFacade
28
     * @param \Spryker\Zed\MerchantProductDataImport\Dependency\Facade\MerchantProductDataImportToMerchantStockFacadeInterface $merchantStockFacade
29
     * @param \Spryker\Zed\MerchantProductDataImport\Dependency\Facade\MerchantProductDataImportToStoreFacadeInterface $storeFacade
30
     * @param \Spryker\Zed\MerchantProductDataImport\Dependency\Facade\MerchantProductDataImportToCurrencyFacadeInterface $currencyFacade
31
     * @param \Spryker\Zed\MerchantProductDataImport\Dependency\Facade\MerchantProductDataImportToProductAttributeFacadeInterface $productAttributeFacade
32
     */
33
    public function __construct(
34
        protected MerchantProductDataImportConfig $merchantProductDataImportConfig,
35
        protected MerchantProductDataImportToLocaleFacadeInterface $localeFacade,
36
        protected MerchantProductDataImportToMerchantFacadeInterface $merchantFacade,
37
        protected MerchantProductDataImportToMerchantStockFacadeInterface $merchantStockFacade,
38
        protected MerchantProductDataImportToStoreFacadeInterface $storeFacade,
39
        protected MerchantProductDataImportToCurrencyFacadeInterface $currencyFacade,
40
        protected MerchantProductDataImportToProductAttributeFacadeInterface $productAttributeFacade
41
    ) {
42
    }
43
44
    /**
45
     * @param array<string, list<string>> $possibleCsvHeaders
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, list<string>> at position 4 could not be parsed: Expected '>' at position 4, but found 'list'.
Loading history...
46
     * @param \Generated\Shared\Transfer\MerchantTransfer $merchantTransfer
47
     *
48
     * @return array<string, list<string>>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, list<string>> at position 4 could not be parsed: Expected '>' at position 4, but found 'list'.
Loading history...
49
     */
50
    public function expand(array $possibleCsvHeaders, MerchantTransfer $merchantTransfer): array
51
    {
52
        if (!isset($possibleCsvHeaders[MerchantProductDataImportConfig::IMPORT_TYPE_MERCHANT_COMBINED_PRODUCT])) {
53
            return $possibleCsvHeaders;
54
        }
55
56
        $merchantCriteriaTransfer = (new MerchantCriteriaTransfer())
57
            ->setWithExpanders(false)
58
            ->setMerchantReference($merchantTransfer->getMerchantReferenceOrFail());
59
60
        $merchantTransfer = $this->merchantFacade->get($merchantCriteriaTransfer)->getMerchants()->getIterator()->current();
61
        if (!$merchantTransfer) {
62
            return $possibleCsvHeaders;
63
        }
64
65
        $headers = array_merge(
66
            $this->merchantProductDataImportConfig->getPossibleCsvHeaders(),
67
            $this->merchantProductDataImportConfig->getPossibleCsvLocaleHeaders(),
68
            $this->merchantProductDataImportConfig->getPossibleCsvAttributeHeaders(),
69
            $this->merchantProductDataImportConfig->getPossibleCsvStockHeaders(),
70
            $this->merchantProductDataImportConfig->getPossibleCsvPriceHeaders(),
71
            $this->merchantProductDataImportConfig->getPossibleCsvImageHeaders(),
72
        );
73
74
        $locales = array_values($this->localeFacade->getAvailableLocales());
75
        $stocks = $this->getMerchantStockNames($merchantTransfer);
76
        $stores = $this->getStoreNames();
77
        $currencies = $this->getCurrencyCodes(array_keys($stores));
78
        $attributes = $this->getAttributeKeys();
79
80
        $headers = $this->replacePlaceholders($headers, [
81
            '{locale}' => $locales,
82
            '{stock}' => $stocks,
83
            '{store}' => $stores,
84
            '{currency}' => $currencies,
85
            '{attribute}' => $attributes,
86
        ]);
87
88
        $possibleCsvHeaders[MerchantProductDataImportConfig::IMPORT_TYPE_MERCHANT_COMBINED_PRODUCT] = array_merge(
89
            $possibleCsvHeaders[MerchantProductDataImportConfig::IMPORT_TYPE_MERCHANT_COMBINED_PRODUCT],
90
            $headers,
91
        );
92
93
        return $possibleCsvHeaders;
94
    }
95
96
    /**
97
     * @param list<string> $headers
98
     * @param array<string, string|list<string>> $replacements
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, string|list<string>> at position 6 could not be parsed: Expected '>' at position 6, but found 'list'.
Loading history...
99
     *
100
     * @return list<string>
101
     */
102
    protected function replacePlaceholders(array $headers, array $replacements): array
103
    {
104
        $result = [];
105
        foreach ($headers as $header) {
106
            $result = array_merge($result, $this->expandHeaderPlaceholders($header, $replacements));
107
        }
108
109
        return $result;
110
    }
111
112
    /**
113
     * @param string $header
114
     * @param array<string, string|list<string>> $replacements
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, string|list<string>> at position 6 could not be parsed: Expected '>' at position 6, but found 'list'.
Loading history...
115
     *
116
     * @return list<string>
117
     */
118
    protected function expandHeaderPlaceholders(string $header, array $replacements): array
119
    {
120
        foreach ($replacements as $placeholder => $values) {
121
            if (strpos($header, $placeholder) !== false) {
122
                if (is_array($values)) {
123
                    $expanded = [];
124
                    foreach ($values as $value) {
125
                        $expanded = array_merge(
126
                            $expanded,
127
                            $this->expandHeaderPlaceholders(str_replace($placeholder, $value, $header), $replacements),
128
                        );
129
                    }
130
131
                    return $expanded;
132
                }
133
                $header = str_replace($placeholder, $values, $header);
134
            }
135
        }
136
137
        return [$header];
138
    }
139
140
    /**
141
     * @param \Generated\Shared\Transfer\MerchantTransfer $merchantTransfer
142
     *
143
     * @return list<string>
144
     */
145
    protected function getMerchantStockNames(MerchantTransfer $merchantTransfer): array
146
    {
147
        $stockNames = [];
148
        $stockTransfers = $this->merchantStockFacade->get(
149
            (new MerchantStockCriteriaTransfer())->setIdMerchant($merchantTransfer->getIdMerchantOrFail()),
150
        )->getStocks();
151
152
        foreach ($stockTransfers as $stockTransfer) {
153
            $stockNames[] = $stockTransfer->getNameOrFail();
154
        }
155
156
        return $stockNames;
157
    }
158
159
    /**
160
     * @return array<int, string>
161
     */
162
    protected function getStoreNames(): array
163
    {
164
        $storeNames = [];
165
        foreach ($this->storeFacade->getAllStores() as $stockTransfer) {
166
            $storeNames[$stockTransfer->getIdStoreOrFail()] = $stockTransfer->getNameOrFail();
167
        }
168
169
        return $storeNames;
170
    }
171
172
    /**
173
     * @param list<int> $storeIds
174
     *
175
     * @return list<string>
176
     */
177
    protected function getCurrencyCodes(array $storeIds): array
178
    {
179
        $currencyCodes = [];
180
        $storeTransfers = array_map(
181
            static fn (int $idStore): StoreTransfer => (new StoreTransfer())->setIdStore($idStore),
182
            $storeIds,
183
        );
184
185
        foreach ($this->currencyFacade->expandStoreTransfersWithCurrencies($storeTransfers) as $storeTransfer) {
186
            foreach ($storeTransfer->getAvailableCurrencyIsoCodes() as $code) {
187
                $currencyCodes[] = $code;
188
            }
189
        }
190
191
        return array_unique($currencyCodes);
192
    }
193
194
    /**
195
     * @return list<string>
196
     */
197
    protected function getAttributeKeys(): array
198
    {
199
        $attributeKeys = [];
200
        foreach ($this->productAttributeFacade->getProductAttributeCollection() as $productManagementAttributeTransfer) {
201
            $attributeKeys[] = $productManagementAttributeTransfer->getKeyOrFail();
202
        }
203
204
        return $attributeKeys;
205
    }
206
}
207