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

TierPriceObserver::addTierPriceDataToPkMapping()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 4
c 1
b 1
f 0
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
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
                foreach ($productWebsiteData as $productWebsite) {
124
                    if ($tierPriceData['website_id'] == $productWebsite['website_id']) {
125
                        // persist the tier price and mark it as processed
126
                        $this->addTierPriceDataToPkMapping($tierPriceData);
127
                    } else {
128
                        $this->getSubject()->getSystemLogger()->warning(
129
                            sprintf(
130
                                "The Product with the SKU %s has not assigned to the Website %s",
131
                                $productWebsite['sku'],
132
                                $tierPriceData['website_id']
133
                            )
134
                        );
135
                        $this->skipRow();
136
                        return;
137
                    }
138
                }
139
            }
140
        } catch (\Exception $e) {
141
            // query whether or not we're in debug mode
142
            if ($this->getSubject()->isDebugMode()) {
143
                $this->getSubject()->getSystemLogger()->warning($e->getMessage());
144
                $this->skipRow();
145
                return;
146
            }
147
            // throw the exception agatin
148
            throw $e;
149
        }
150
    }
151
    
152
    /**s
153
     * Initialize the product website with the passed attributes and returns an instance.
154
     *
155
     * @param array $attr The product website attributes
156
     *
157
     * @return array The initialized product website
158
     * @throws \RuntimeException Is thrown, if the attributes can not be initialized
159
     */
160
    protected function initializeTierPrice(array $attr)
161
    {
162
        return $attr;
163
    }
164
165
    /**
166
     * Loads and returns the product with the passed SKU.
167
     *
168
     * @param string $sku The SKU of the product to load
169
     *
170
     * @return array The product
171
     */
172
    protected function loadProduct($sku)
173
    {
174
        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

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

259
            /** @scrutinizer ignore-type */ $this->persistTierPrice($tierPriceData),
Loading history...
260
            $pk = $tierPriceData[$this->getPrimaryKeyMemberName()]
261
        );
262
        $this->addSkuToPkMapping($this->getValue(ColumnKeys::SKU), $pk);
263
    }
264
}
265