ClearTierPriceObserver::isAllGroups()   A
last analyzed

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 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\TierPrice\Observers\ClearTierPriceObserver
5
 *
6
 * PHP version 7
7
 *
8
 * @author    Tim Wagner <[email protected]>
9
 * @license   https://opensource.org/licenses/MIT
10
 * @link      https://github.com/techdivision/import-product-tier-price
11
 * @link      https://www.techdivision.com
12
 */
13
14
namespace TechDivision\Import\Product\TierPrice\Observers;
15
16
use TechDivision\Import\Product\TierPrice\Utils\MemberNames;
17
use TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface;
18
use TechDivision\Import\Product\TierPrice\Services\TierPriceProcessorInterface;
19
use TechDivision\Import\Utils\RegistryKeys;
20
21
/**
22
 * Observer for deleting tier prices from the database.
23
 *
24
 * @author    Tim Wagner <[email protected]>
25
 * @license   https://opensource.org/licenses/MIT
26
 * @link      https://github.com/techdivision/import-product-tier-price
27
 * @link      https://www.techdivision.com
28
 */
29
class ClearTierPriceObserver extends AbstractProductTierPriceObserver
30
{
31
32
    /**
33
     * The trait that prepares the tier price data.
34
     *
35
     * @var \TechDivision\Import\Product\TierPrice\Observers\PrepareTierPriceTrait
36
     */
37
    use PrepareTierPriceTrait;
38
39
    /**
40
     * The available tier price value types.
41
     *
42
     * @var \TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface
43
     */
44
    protected $valueTypes;
45
46
    /**
47
     * Initialize the observer with the passed product tier price processor instance.
48
     *
49
     * @param \TechDivision\Import\Product\TierPrice\Services\TierPriceProcessorInterface $tierPriceProcessor The processor instance
50
     * @param \TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface            $valueTypes         The tier price value types
51
     */
52
    public function __construct(TierPriceProcessorInterface $tierPriceProcessor, ValueTypesInterface $valueTypes)
53
    {
54
55
        // pass the tier price processor through to the parent instance
56
        parent::__construct($tierPriceProcessor);
57
58
        // set the value types
59
        $this->valueTypes = $valueTypes;
60
    }
61
62
    /**
63
     * Returns the tier price value types.
64
     *
65
     * @return \TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface The tier price value types
66
     */
67
    protected function getValueTypes()
68
    {
69
        return $this->valueTypes;
70
    }
71
72
    /**
73
     * Process the observer's business logic.
74
     *
75
     * @return void
76
     * @throws \Exception
77
     */
78
    protected function process()
79
    {
80
81
        try {
82
            // prepare the tier price attributes of the actual row
83
            $tierPrice = $this->prepareAttributes();
84
85
            // load the values for loading the actual tier price
86
            $pk = $tierPrice[$this->getPrimaryKeyMemberName()];
87
            $qty = $tierPrice[MemberNames::QTY];
88
            $allGroups = $tierPrice[MemberNames::ALL_GROUPS];
89
            $websiteId = $tierPrice[MemberNames::WEBSITE_ID];
90
            $customerGroupId = $tierPrice[MemberNames::CUSTOMER_GROUP_ID];
91
92
            // delete the tier price if available
93
            if ($tierPrice = $this->loadTierPriceByPkAndAllGroupsAndCustomerGroupIdAndQtyAndWebsiteId($pk, $allGroups, $customerGroupId, $qty, $websiteId)) {
94
                $this->deleteTierPrice(array(MemberNames::VALUE_ID => $tierPrice[MemberNames::VALUE_ID]));
95
            }
96
        } catch (\Exception $e) {
97
            // query whether or not we're in no-strict mode
98
            if (!$this->getSubject()->isStrictMode()) {
99
                $this->getSubject()->getSystemLogger()->warning($e->getMessage());
100
                $this->mergeStatus(
101
                    array(
102
                        RegistryKeys::NO_STRICT_VALIDATIONS => array(
103
                            basename($this->getFilename()) => array(
104
                                $this->getLineNumber() => array(
105
                                    MemberNames::SKU => $e->getMessage()
106
                                )
107
                            )
108
                        )
109
                    )
110
                );
111
                $this->skipRow();
112
                return;
113
            }
114
115
            // throw the exception agatin in strict mode
116
            throw $e;
117
        }
118
    }
119
120
    /**
121
     * Loads and returns the product with the passed SKU.
122
     *
123
     * @param string $sku The SKU of the product to load
124
     *
125
     * @return array The product
126
     */
127
    protected function loadProduct($sku)
128
    {
129
        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

129
        return $this->getTierPriceProcessor()->/** @scrutinizer ignore-call */ loadProduct($sku);
Loading history...
130
    }
131
132
    /**
133
     * Returns the tier price with the given parameters.
134
     *
135
     * @param string  $pk              The PK of the product relation
136
     * @param integer $allGroups       The flag if all groups are affected or not
137
     * @param integer $customerGroupId The customer group ID
138
     * @param integer $qty             The tier price quantity
139
     * @param integer $websiteId       The website ID the tier price is related to
140
     *
141
     * @return array The tier price
142
     */
143
    protected function loadTierPriceByPkAndAllGroupsAndCustomerGroupIdAndQtyAndWebsiteId($pk, $allGroups, $customerGroupId, $qty, $websiteId)
144
    {
145
        return $this->getTierPriceProcessor()->loadTierPriceByPkAndAllGroupsAndCustomerGroupIdAndQtyAndWebsiteId($pk, $allGroups, $customerGroupId, $qty, $websiteId);
146
    }
147
148
    /**
149
     * Deletes the tierprice with the passed attributes.
150
     *
151
     * @param array       $row  The attributes of the entity to delete
152
     * @param string|null $name The name of the prepared statement that has to be executed
153
     *
154
     * @return void
155
     */
156
    protected function deleteTierPrice($row, $name = null)
157
    {
158
        $this->getTierPriceProcessor()->deleteTierPrice($row, $name);
159
    }
160
161
    /**
162
     * Returns the customer group ID for the given code, if it exists.
163
     *
164
     * @param string $code The code of the requested customer group
165
     *
166
     * @return integer|null The ID of the customer group
167
     */
168
    protected function getCustomerGroupIdByCode($code)
169
    {
170
        return $this->getSubject()->getCustomerGroupIdByCode($code);
171
    }
172
173
    /**
174
     * Return's the store website for the passed code.
175
     *
176
     * @param string $code The code of the store website to return the ID for
177
     *
178
     * @return integer The store website ID
179
     * @throws \Exception Is thrown, if the store website with the requested code is not available
180
     */
181
    protected function getStoreWebsiteIdByCode($code)
182
    {
183
        return $this->getSubject()->getStoreWebsiteIdByCode($code);
184
    }
185
186
    /**
187
     * Query whether or not the passed value type is valid.
188
     *
189
     * @param string $valueType The value type to query for
190
     *
191
     * @return boolean TRUE if the value type is valid, else FALSE
192
     */
193
    protected function isValueType($valueType)
194
    {
195
        return $this->getValueTypes()->isValueType($valueType);
196
    }
197
198
    /**
199
     * Queries whether or not the passed customer group code matches all groups or not.
200
     *
201
     * @param string $code The customer group code to query for
202
     *
203
     * @return boolean TRUE if the customer group code matches, else FALSE
204
     */
205
    protected function isAllGroups($code)
206
    {
207
        return $this->getSubject()->isAllGroups($code);
208
    }
209
}
210