Passed
Push — pac-383-tier-price ( b7a7c9...44f561 )
by
unknown
20:35
created

ProductTierPriceObserver::getArtefact()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 16
c 0
b 0
f 0
dl 0
loc 29
ccs 0
cts 28
cp 0
rs 9.7333
cc 1
nc 1
nop 7
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\TierPrice\Observers\ProductTierPriceObserver
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Klaas-Tido Rühl <[email protected]>
15
 * @author    Tim Wagner <[email protected]>
16
 * @copyright 2019 REFUSiON GmbH <[email protected]>
17
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
18
 * @link      https://github.com/techdivision/import-product-tier-price
19
 * @link      https://www.techdivision.com
20
 * @link      https://www.refusion.com
21
 */
22
23
namespace TechDivision\Import\Product\TierPrice\Observers;
24
25
use TechDivision\Import\Utils\StoreViewCodes;
26
use TechDivision\Import\Product\TierPrice\Utils\ColumnKeys;
27
use TechDivision\Import\Product\TierPrice\Utils\DefaultCodes;
28
use TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface;
29
use TechDivision\Import\Product\Observers\AbstractProductImportObserver;
30
31
/**
32
 * Observer for creating a separate import file for the tier price data.
33
 *
34
 * @author    Klaas-Tido Rühl <[email protected]>
35
 * @author    Tim Wagner <[email protected]>
36
 * @copyright 2019 REFUSiON GmbH <[email protected]>
37
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
38
 * @link      https://github.com/techdivision/import-product-tier-price
39
 * @link      https://www.techdivision.com
40
 * @link      https://www.refusion.com
41
 */
42
class ProductTierPriceObserver extends AbstractProductImportObserver
43
{
44
45
    /**
46
     * The artefact type.
47
     *
48
     * @var string
49
     */
50
    const ARTEFACT_TYPE = 'tier-price';
51
52
    /**
53
     * The available tier price value types.
54
     *
55
     * @var \TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface
56
     */
57
    protected $valueTypes;
58
59
    /**
60
     * Initializes the observer with the tier price value types.
61
     *
62
     * @param \TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface $valueTypes The tier price value types
63
     */
64
    public function __construct(ValueTypesInterface $valueTypes)
65
    {
66
        $this->valueTypes = $valueTypes;
67
    }
68
69
    /**
70
     * Returns the tier price value types.
71
     *
72
     * @return \TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface The tier price value types
73
     */
74
    protected function getValueTypes()
75
    {
76
        return $this->valueTypes;
77
    }
78
79
    /**
80
     * Process the observer's business logic.
81
     *
82
     * @return void
83
     * @throws \Exception
84
     */
85
    protected function process()
86
    {
87
88
        // initialize the array for the artefacts
89
        $artefacts = array();
90
91
        // load the SKU from the row
92
        $sku = $this->getValue(ColumnKeys::SKU);
93
94
        // prepare the store view code
95
        $this->getSubject()->prepareStoreViewCode();
96
97
        // try to load the store view code
98
        $storeViewCode = $this->getSubject()->getStoreViewCode(StoreViewCodes::ADMIN);
99
100
        // mapping only possible for admin store
101
        if ($storeViewCode != StoreViewCodes::ADMIN) {
102
            return;
103
        }
104
105
        // load the serialized tier price information from the column
106
        if ($tierPrices = $this->getValue(ColumnKeys::TIER_PRICES)) {
107
            // iterate over the found tier prices
108
            foreach ($this->explode($tierPrices, '|') as $tierPrice) {
109
                // explode the tier prices
110
                $explodedTierPrice = $this->getSubject()->explode($tierPrice);
111
                // build the dictionary
112
                $tierPriceImportData = array();
113
114
                // explode the tier price details
115
                foreach ($explodedTierPrice as $assignmentString) {
116
                    list($key, $value) = $this->explode($assignmentString, '=');
117
                    $tierPriceImportData[$key] = trim($value);
118
                }
119
120
                // load the quantity and the price for the tier price
121
                $qty = isset($tierPriceImportData[ColumnKeys::QTY]) ? $tierPriceImportData[ColumnKeys::QTY] : null;
122
                $price = isset($tierPriceImportData[ColumnKeys::PRICE]) ? $tierPriceImportData[ColumnKeys::PRICE] : null;
123
124
                // validate quantity and price
125
                if ($qty == null || $price == null) {
126
                    throw new \Exception(
127
                        $this->appendExceptionSuffix(
128
                            sprintf('Missing qty or price for tier price for product "%s"', $sku)
129
                        )
130
                    );
131
                }
132
133
                // load the other values from the extracted data
134
                $valueType = $this->getValueType($tierPriceImportData);
135
                $website = $this->getWebsiteCode($tierPriceImportData);
136
                $customerGroup = $this->getCustomerGroupCode($tierPriceImportData);
137
                $tierpriceWebsites = $this->explode($this->getValue(ColumnKeys::PRODUCT_WEBSITES), ',');
138
139
                if ($website === DefaultCodes::ALL_WEBSITES) {
140
                    // prepare the artefact we want to export
141
                    $artefacts = $this->getArtefact($sku, $qty, $price, $valueType, $website, $customerGroup, $artefacts);
0 ignored issues
show
Bug introduced by
It seems like $sku can also be of type null; however, parameter $sku of TechDivision\Import\Prod...Observer::getArtefact() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

141
                    $artefacts = $this->getArtefact(/** @scrutinizer ignore-type */ $sku, $qty, $price, $valueType, $website, $customerGroup, $artefacts);
Loading history...
142
                } elseif (in_array($website, $tierpriceWebsites)) {
143
                    // prepare the artefact we want to export
144
                    $artefacts = $this->getArtefact($sku, $qty, $price, $valueType, $website, $customerGroup, $artefacts);
145
                } else {
146
                    $this->getSubject()->getSystemLogger()->warning(
147
                        sprintf(
148
                            "The Product with the SKU %s has not assigned to the Website %s",
149
                            $sku,
150
                            $website
151
                        )
152
                    );
153
                    $this->skipRow();
154
                }
155
            }
156
            // add the artefact to the observer to be exported later
157
            $this->addArtefacts($artefacts);
158
        }
159
    }
160
161
    /**
162
     * Returns the tier price value type from the passed data, or the default one (fixed), if not set.
163
     *
164
     * @param array $data The data to load the tier price value type from
165
     *
166
     * @return string The tier price value type
167
     */
168
    protected function getValueType(array $data)
169
    {
170
        return isset($data[ColumnKeys::VALUE_TYPE]) ? $data[ColumnKeys::VALUE_TYPE] : $this->getValueTypes()->getFixed();
171
    }
172
173
    /**
174
     * Returns the website code from the passed data, or 'All Websites', if not set.
175
     *
176
     * @param array $data The data to load the website codefrom
177
     *
178
     * @return string The website code
179
     */
180
    protected function getWebsiteCode(array $data)
181
    {
182
        return $data[ColumnKeys::WEBSITE] ? $data[ColumnKeys::WEBSITE] : DefaultCodes::ALL_WEBSITES;
183
    }
184
185
    /**
186
     * Returns the customer group code from the passed data, or 'ALL GROUPS', if not set.
187
     *
188
     * @param array $data The data to load the customer group code from
189
     *
190
     * @return string The customer group code
191
     */
192
    protected function getCustomerGroupCode(array $data)
193
    {
194
        return $data[ColumnKeys::CUSTOMER_GROUP] ? $data[ColumnKeys::CUSTOMER_GROUP] : DefaultCodes::ALL_GROUPS;
195
    }
196
197
    /**
198
     * Create's and return's a new empty artefact entity.
199
     *
200
     * @param array $columns             The array with the column data
201
     * @param array $originalColumnNames The array with a mapping from the old to the new column names
202
     *
203
     * @return array The new artefact entity
204
     */
205
    protected function newArtefact(array $columns, array $originalColumnNames)
206
    {
207
        return $this->getSubject()->newArtefact($columns, $originalColumnNames);
208
    }
209
210
    /**
211
     * Add the passed product type artefacts to the product with the
212
     * last entity ID.
213
     *
214
     * @param array $artefacts The product type artefacts
215
     *
216
     * @return void
217
     * @uses \TechDivision\Import\Product\TierPrice\Subjects\TierPriceSubject::getLastEntityId()
218
     */
219
    protected function addArtefacts(array $artefacts)
220
    {
221
        $this->getSubject()->addArtefacts(ProductTierPriceObserver::ARTEFACT_TYPE, $artefacts);
222
    }
223
224
    /**
225
     * @param string $sku           Sku
226
     * @param string $qty           Quantity
227
     * @param string $price         Price
228
     * @param string $valueType     valueType
229
     * @param string $website       Website
230
     * @param string $customerGroup Customer Group
231
     * @param array  $artefacts     Artefact
232
     * @return array
233
     */
234
    protected function getArtefact(
235
        string $sku,
236
        string $qty,
237
        string $price,
238
        string $valueType,
239
        string $website,
240
        string $customerGroup,
241
        array $artefacts
242
    ) {
243
    // prepare the artefact we want to export
244
        $artefacts[] = $this->newArtefact(
245
            array(
246
                ColumnKeys::SKU => $sku,
247
                ColumnKeys::TIER_PRICE_QTY => $qty,
248
                ColumnKeys::TIER_PRICE => $price,
249
                ColumnKeys::TIER_PRICE_VALUE_TYPE => $valueType,
250
                ColumnKeys::TIER_PRICE_WEBSITE => $website,
251
                ColumnKeys::TIER_PRICE_CUSTOMER_GROUP => $customerGroup
252
            ),
253
            array(
254
                ColumnKeys::SKU => ColumnKeys::SKU,
255
                ColumnKeys::TIER_PRICE_QTY => ColumnKeys::TIER_PRICES,
256
                ColumnKeys::TIER_PRICE => ColumnKeys::TIER_PRICES,
257
                ColumnKeys::TIER_PRICE_VALUE_TYPE => ColumnKeys::TIER_PRICES,
258
                ColumnKeys::TIER_PRICE_WEBSITE => ColumnKeys::TIER_PRICES,
259
                ColumnKeys::TIER_PRICE_CUSTOMER_GROUP => ColumnKeys::TIER_PRICES
260
            )
261
        );
262
        return $artefacts;
263
    }
264
}
265