Passed
Push — pac-383-tier-price ( b7a7c9 )
by
unknown
03:27
created

TierPriceObserver::getProductBunchProcessor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 0
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->addProcessedTierPrice($this->persistTierPrice($tierPriceData), $pk = $tierPriceData[$this->getPrimaryKeyMemberName()]);
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

118
                $this->addProcessedTierPrice(/** @scrutinizer ignore-type */ $this->persistTierPrice($tierPriceData), $pk = $tierPriceData[$this->getPrimaryKeyMemberName()]);
Loading history...
119
                $this->addSkuToPkMapping($this->getValue(ColumnKeys::SKU), $pk);
120
            } else {
121
                $productWebsiteData = $this->getProductBunchProcessor()->loadProductWebsitesBySku(
122
                    $this->getValue(ColumnKeys::SKU)
123
                );
124
                foreach ($productWebsiteData as $productWebsite) {
125
                    if ($tierPriceData['website_id'] == $productWebsite['website_id']) {
126
                        // persist the tier price and mark it as processed
127
                        $this->addProcessedTierPrice($this->persistTierPrice($tierPriceData), $pk = $tierPriceData[$this->getPrimaryKeyMemberName()]);
128
129
                        $this->addSkuToPkMapping($this->getValue(ColumnKeys::SKU), $pk);
130
                    } else {
131
                        $this->getSubject()->getSystemLogger()->warning(
132
                            sprintf(
133
                                "The Product with the SKU %s has not assigned to the Website %s",
134
                                $productWebsite['sku'],
135
                                $tierPriceData['website_id']
136
                            )
137
                        );
138
                        $this->skipRow();
139
                        return;
140
                    }
141
                }
142
            }
143
        } catch (\Exception $e) {
144
            // query whether or not we're in debug mode
145
            if ($this->getSubject()->isDebugMode()) {
146
                $this->getSubject()->getSystemLogger()->warning($e->getMessage());
147
                $this->skipRow();
148
                return;
149
            }
150
            // throw the exception agatin
151
            throw $e;
152
        }
153
    }
154
    
155
    /**s
156
     * Initialize the product website with the passed attributes and returns an instance.
157
     *
158
     * @param array $attr The product website attributes
159
     *
160
     * @return array The initialized product website
161
     * @throws \RuntimeException Is thrown, if the attributes can not be initialized
162
     */
163
    protected function initializeTierPrice(array $attr)
164
    {
165
        return $attr;
166
    }
167
168
    /**
169
     * Loads and returns the product with the passed SKU.
170
     *
171
     * @param string $sku The SKU of the product to load
172
     *
173
     * @return array The product
174
     */
175
    protected function loadProduct($sku)
176
    {
177
        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

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