Passed
Pull Request — 17.x (#13)
by
unknown
53:35 queued 50:47
created

TierPriceObserver   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 225
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 19
eloc 46
c 5
b 1
f 0
dl 0
loc 225
ccs 0
cts 93
cp 0
rs 10

13 Methods

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

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

264
            /** @scrutinizer ignore-type */ $this->persistTierPrice($tierPriceData),
Loading history...
265
            $pk = $tierPriceData[$this->getPrimaryKeyMemberName()]
266
        );
267
        $this->addSkuToPkMapping($this->getValue(ColumnKeys::SKU), $pk);
268
    }
269
}
270