TierPriceObserver   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 238
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 19
eloc 52
c 5
b 1
f 0
dl 0
loc 238
ccs 0
cts 68
cp 0
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
B process() 0 53 7
A __construct() 0 11 1
A loadProduct() 0 3 1
A isValueType() 0 3 1
A getStoreWebsiteIdByCode() 0 3 1
A getProductBunchProcessor() 0 3 1
A addProcessedTierPrice() 0 3 1
A persistTierPrice() 0 3 1
A isAllGroups() 0 3 1
A getValueTypes() 0 3 1
A initializeTierPrice() 0 3 1
A getCustomerGroupIdByCode() 0 3 1
A addTierPriceDataToPkMapping() 0 7 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\TierPrice\Observers\TierPriceUpdateObserver
5
 *
6
 * PHP version 7
7
 *
8
 * @author    Klaas-Tido Rühl <[email protected]>
9
 * @author    Tim Wagner <[email protected]>
10
 * @copyright 2019 REFUSiON GmbH <[email protected]>
11
 * @license   https://opensource.org/licenses/MIT
12
 * @link      https://github.com/techdivision/import-product-tier-price
13
 * @link      https://www.techdivision.com
14
 * @link      https://www.refusion.com
15
 */
16
17
namespace TechDivision\Import\Product\TierPrice\Observers;
18
19
use TechDivision\Import\Product\Services\ProductBunchProcessorInterface;
20
use TechDivision\Import\Product\TierPrice\Utils\MemberNames;
21
use TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface;
22
use TechDivision\Import\Product\TierPrice\Services\TierPriceProcessorInterface;
23
use TechDivision\Import\Product\TierPrice\Utils\ColumnKeys;
24
use TechDivision\Import\Product\TierPrice\Utils\DefaultCodes;
25
use TechDivision\Import\Utils\RegistryKeys;
26
27
/**
28
 * Observer for creating/updating/deleting tier prices from the database.
29
 *
30
 * @author    Klaas-Tido Rühl <[email protected]>
31
 * @author    Tim Wagner <[email protected]>
32
 * @copyright 2019 REFUSiON GmbH <[email protected]>
33
 * @license   https://opensource.org/licenses/MIT
34
 * @link      https://github.com/techdivision/import-product-tier-price
35
 * @link      https://www.techdivision.com
36
 * @link      https://www.refusion.com
37
 */
38
class TierPriceObserver extends AbstractProductTierPriceObserver
39
{
40
41
    /**
42
     * The trait that prepares the tier price data.
43
     *
44
     * @var \TechDivision\Import\Product\TierPrice\Observers\PrepareTierPriceTrait
45
     */
46
    use PrepareTierPriceTrait;
47
48
    /**
49
     * The available tier price value types.
50
     *
51
     * @var \TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface
52
     */
53
    protected $valueTypes;
54
55
    /**
56
     * @var ProductBunchProcessorInterface
57
     */
58
    protected $productBunchProcessor;
59
60
    /**
61
     * Initialize the observer with the passed product tier price processor instance.
62
     *
63
     * @param \TechDivision\Import\Product\TierPrice\Services\TierPriceProcessorInterface $tierPriceProcessor    The processor instance
64
     * @param \TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface            $valueTypes            The tier price value types
65
     * @param \TechDivision\Import\Product\Services\ProductBunchProcessorInterface        $productBunchProcessor The product processor instance
66
     */
67
    public function __construct(
68
        TierPriceProcessorInterface $tierPriceProcessor,
69
        ValueTypesInterface $valueTypes,
70
        ProductBunchProcessorInterface $productBunchProcessor
71
    ) {
72
        // set the value types
73
        $this->valueTypes = $valueTypes;
74
        $this->productBunchProcessor = $productBunchProcessor;
75
76
        // pass the tier price processor through to the parent instance
77
        parent::__construct($tierPriceProcessor);
78
    }
79
80
    /**
81
     * Returns the tier price value types.
82
     *
83
     * @return \TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface The tier price value types
84
     */
85
    protected function getValueTypes()
86
    {
87
        return $this->valueTypes;
88
    }
89
90
    /**
91
     * Return's the product bunch processor instance.
92
     *
93
     * @return ProductBunchProcessorInterface The product bunch processor instance
94
     */
95
    protected function getProductBunchProcessor()
96
    {
97
        return $this->productBunchProcessor;
98
    }
99
100
    /**
101
     * Process the observer's business logic.
102
     *
103
     * @return void
104
     * @throws \Exception
105
     */
106
    protected function process()
107
    {
108
109
        try {
110
            // intialize the tier price data
111
            $tierPriceData = $this->initializeTierPrice($this->prepareAttributes());
112
113
            if ($tierPriceData['website_id'] === 0) {
114
                $this->addTierPriceDataToPkMapping($tierPriceData);
115
            } else {
116
                $productWebsiteData = $this->getProductBunchProcessor()->loadProductWebsitesBySku(
117
                    $this->getValue(ColumnKeys::SKU)
118
                );
119
                $found = false;
120
                foreach ($productWebsiteData as $productWebsite) {
121
                    if ($tierPriceData['website_id'] == $productWebsite['website_id']) {
122
                        $found = true;
123
                        // persist the tier price and mark it as processed
124
                        $this->addTierPriceDataToPkMapping($tierPriceData);
125
                        break;
126
                    }
127
                }
128
                if (!$found) {
129
                    $this->getSubject()->getSystemLogger()->warning(
130
                        sprintf(
131
                            "The Product with the SKU %s has not assigned to the Website %s",
132
                            $this->getValue(ColumnKeys::SKU),
133
                            $tierPriceData['website_id']
134
                        )
135
                    );
136
                }
137
            }
138
        } catch (\Exception $e) {
139
            // query whether or not we're in no-strict mode
140
            if (!$this->getSubject()->isStrictMode()) {
141
                $this->getSubject()->getSystemLogger()->warning($e->getMessage());
142
                $this->mergeStatus(
143
                    array(
144
                        RegistryKeys::NO_STRICT_VALIDATIONS => array(
145
                            basename($this->getFilename()) => array(
146
                                $this->getLineNumber() => array(
147
                                    ColumnKeys::SKU =>  $e->getMessage()
148
                                )
149
                            )
150
                        )
151
                    )
152
                );
153
                $this->skipRow();
154
                return;
155
            }
156
157
            // throw the exception again in strict mode
158
            throw $e;
159
        }
160
    }
161
162
    /**s
163
     * Initialize the product website with the passed attributes and returns an instance.
164
     *
165
     * @param array $attr The product website attributes
166
     *
167
     * @return array The initialized product website
168
     * @throws \RuntimeException Is thrown, if the attributes can not be initialized
169
     */
170
    protected function initializeTierPrice(array $attr)
171
    {
172
        return $attr;
173
    }
174
175
    /**
176
     * Loads and returns the product with the passed SKU.
177
     *
178
     * @param string $sku The SKU of the product to load
179
     *
180
     * @return array The product
181
     */
182
    protected function loadProduct($sku)
183
    {
184
        return $this->getTierPriceProcessor()->loadProduct($sku);
0 ignored issues
show
Bug introduced by
The method loadProduct() does not exist on TechDivision\Import\Prod...PriceProcessorInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to TechDivision\Import\Prod...PriceProcessorInterface. ( Ignorable by Annotation )

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

184
        return $this->getTierPriceProcessor()->/** @scrutinizer ignore-call */ loadProduct($sku);
Loading history...
185
    }
186
187
    /**
188
     * Persists the tier price with the passed data.
189
     *
190
     * @param array       $row  The tier price to persist
191
     * @param string|null $name The name of the prepared statement that has to be executed
192
     *
193
     * @return string The ID of the persisted entity
194
     */
195
    protected function persistTierPrice(array $row, $name = null)
196
    {
197
        return $this->getTierPriceProcessor()->persistTierPrice($row, $name);
198
    }
199
200
    /**
201
     * Add the ID of the processed tier price.
202
     *
203
     * @param integer $valueId  The ID of the processed tier price
204
     * @param integer $entityId The entity ID of the related product
205
     *
206
     * @return void
207
     */
208
    protected function addProcessedTierPrice($valueId, $entityId)
209
    {
210
        $this->getSubject()->addProcessedTierPrice($valueId, $entityId);
211
    }
212
213
    /**
214
     * Returns the customer group ID for the given code, if it exists.
215
     *
216
     * @param string $code The code of the requested customer group
217
     *
218
     * @return integer|null The ID of the customer group
219
     */
220
    protected function getCustomerGroupIdByCode($code)
221
    {
222
        return $this->getSubject()->getCustomerGroupIdByCode($code);
223
    }
224
225
    /**
226
     * Return's the store website for the passed code.
227
     *
228
     * @param string $code The code of the store website to return the ID for
229
     *
230
     * @return integer The store website ID
231
     * @throws \Exception Is thrown, if the store website with the requested code is not available
232
     */
233
    protected function getStoreWebsiteIdByCode($code)
234
    {
235
        return $this->getSubject()->getStoreWebsiteIdByCode($code);
236
    }
237
238
    /**
239
     * Query whether or not the passed value type is valid.
240
     *
241
     * @param string $valueType The value type to query for
242
     *
243
     * @return boolean TRUE if the value type is valid, else FALSE
244
     */
245
    protected function isValueType($valueType)
246
    {
247
        return $this->getValueTypes()->isValueType($valueType);
248
    }
249
250
    /**
251
     * Queries whether or not the passed customer group code matches all groups or not.
252
     *
253
     * @param string $code The customer group code to query for
254
     *
255
     * @return boolean TRUE if the customer group code matches, else FALSE
256
     */
257
    protected function isAllGroups($code)
258
    {
259
        return $this->getSubject()->isAllGroups($code);
260
    }
261
262
    /**
263
     * Persist the tier price and mark it as processed
264
     *
265
     * @param array $tierPriceData TierPriceData
266
     *
267
     * @return void
268
     */
269
    protected function addTierPriceDataToPkMapping(array $tierPriceData)
270
    {
271
        $this->addProcessedTierPrice(
272
            $this->persistTierPrice($tierPriceData),
0 ignored issues
show
Bug introduced by
$this->persistTierPrice($tierPriceData) of type string is incompatible with the type integer expected by parameter $valueId of TechDivision\Import\Prod...addProcessedTierPrice(). ( Ignorable by Annotation )

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

272
            /** @scrutinizer ignore-type */ $this->persistTierPrice($tierPriceData),
Loading history...
273
            $pk = $tierPriceData[$this->getPrimaryKeyMemberName()]
274
        );
275
        $this->addSkuToPkMapping($this->getValue(ColumnKeys::SKU), $pk);
276
    }
277
}
278