Completed
Push — 2.x ( 704b62 )
by Tim
04:55
created

ClearTierPriceObserver   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
eloc 22
dl 0
loc 157
ccs 0
cts 48
cp 0
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getStoreWebsiteIdByCode() 0 3 1
A process() 0 16 2
A getCustomerGroupIdByCode() 0 3 1
A getTierPriceProcessor() 0 3 1
A isAllGroups() 0 3 1
A deleteTierPrice() 0 3 1
A getValueTypes() 0 3 1
A __construct() 0 4 1
A loadTierPriceByEntityIdAndAllGroupsAndCustomerGroupIdAndQtyAndWebsiteId() 0 3 1
A isValueType() 0 3 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\TierPrice\Observers\ClearTierPriceObserver
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    Tim Wagner <[email protected]>
15
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
16
 * @link      https://github.com/techdivision/import-product-tier-price
17
 * @link      https://www.techdivision.com
18
 */
19
20
namespace TechDivision\Import\Product\TierPrice\Observers;
21
22
use TechDivision\Import\Product\Observers\AbstractProductImportObserver;
23
use TechDivision\Import\Product\TierPrice\Utils\MemberNames;
24
use TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface;
25
use TechDivision\Import\Product\TierPrice\Services\TierPriceProcessorInterface;
26
27
/**
28
 * Observer for deleting tier prices from the database.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      https://github.com/techdivision/import-product-tier-price
33
 * @link      https://www.techdivision.com
34
 */
35
class ClearTierPriceObserver extends AbstractProductImportObserver
36
{
37
38
    /**
39
     * The trait that prepares the tier price data.
40
     *
41
     * @var \TechDivision\Import\Product\TierPrice\Observers\PrepareTierPriceTrait
42
     */
43
    use PrepareTierPriceTrait;
44
45
    /**
46
     * The product tier price processor instance.
47
     *
48
     * @var \TechDivision\Import\Product\TierPrice\Services\TierPriceProcessorInterface
49
     */
50
    protected $tierPriceProcessor;
51
52
    /**
53
     * The available tier price value types.
54
     *
55
     * @var \TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface
56
     */
57
    protected $valueTypes;
58
59
    /**
60
     * Initialize the observer with the passed product tier price processor instance.
61
     *
62
     * @param \TechDivision\Import\Product\TierPrice\Services\TierPriceProcessorInterface $tierPriceProcessor The processor instance
63
     * @param \TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface            $valueTypes         The tier price value types
64
     */
65
    public function __construct(TierPriceProcessorInterface $tierPriceProcessor, ValueTypesInterface $valueTypes)
66
    {
67
        $this->tierPriceProcessor = $tierPriceProcessor;
68
        $this->valueTypes = $valueTypes;
69
    }
70
71
    /**
72
     * Returns the product URL rewrite processor instance.
73
     *
74
     * @return \TechDivision\Import\Product\TierPrice\Services\TierPriceProcessorInterface The processor instance
75
     */
76
    protected function getTierPriceProcessor()
77
    {
78
        return $this->tierPriceProcessor;
79
    }
80
81
    /**
82
     * Returns the tier price value types.
83
     *
84
     * @return \TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface The tier price value types
85
     */
86
    protected function getValueTypes()
87
    {
88
        return $this->valueTypes;
89
    }
90
91
    /**
92
     * Process the observer's business logic.
93
     *
94
     * @return void
95
     * @throws \Exception
96
     */
97
    protected function process()
98
    {
99
100
        // prepare the tier price attributes of the actual row
101
        $tierPrice = $this->prepareAttributes();
102
103
        // load the values for loading the actual tier price
104
        $qty = $tierPrice[MemberNames::QTY];
105
        $entityId = $tierPrice[MemberNames::ENTITY_ID];
106
        $allGroups = $tierPrice[MemberNames::ALL_GROUPS];
107
        $websiteId = $tierPrice[MemberNames::WEBSITE_ID];
108
        $customerGroupId = $tierPrice[MemberNames::CUSTOMER_GROUP_ID];
109
110
        // delete the tier price if available
111
        if ($tierPrice = $this->loadTierPriceByEntityIdAndAllGroupsAndCustomerGroupIdAndQtyAndWebsiteId($entityId, $allGroups, $customerGroupId, $qty, $websiteId)) {
112
            $this->deleteTierPrice(array(MemberNames::VALUE_ID => $tierPrice[MemberNames::VALUE_ID]));
113
        }
114
    }
115
116
    /**
117
     * Returns the tier price with the given parameters.
118
     *
119
     * @param string  $entityId        The entity ID of the product relation
120
     * @param integer $allGroups       The flag if all groups are affected or not
121
     * @param integer $customerGroupId The customer group ID
122
     * @param integer $qty             The tier price quantity
123
     * @param integer $websiteId       The website ID the tier price is related to
124
     *
125
     * @return array The tier price
126
     */
127
    protected function loadTierPriceByEntityIdAndAllGroupsAndCustomerGroupIdAndQtyAndWebsiteId($entityId, $allGroups, $customerGroupId, $qty, $websiteId)
128
    {
129
        return $this->getTierPriceProcessor()->loadTierPriceByEntityIdAndAllGroupsAndCustomerGroupIdAndQtyAndWebsiteId($entityId, $allGroups, $customerGroupId, $qty, $websiteId);
130
    }
131
132
    /**
133
     * Deletes the tierprice with the passed attributes.
134
     *
135
     * @param array       $row  The attributes of the entity to delete
136
     * @param string|null $name The name of the prepared statement that has to be executed
137
     *
138
     * @return void
139
     */
140
    protected function deleteTierPrice($row, $name = null)
141
    {
142
        $this->getTierPriceProcessor()->deleteTierPrice($row, $name);
143
    }
144
145
    /**
146
     * Returns the customer group ID for the given code, if it exists.
147
     *
148
     * @param string $code The code of the requested customer group
149
     *
150
     * @return integer|null The ID of the customer group
151
     */
152
    protected function getCustomerGroupIdByCode($code)
153
    {
154
        return $this->getSubject()->getCustomerGroupIdByCode($code);
155
    }
156
157
    /**
158
     * Return's the store website for the passed code.
159
     *
160
     * @param string $code The code of the store website to return the ID for
161
     *
162
     * @return integer The store website ID
163
     * @throws \Exception Is thrown, if the store website with the requested code is not available
164
     */
165
    protected function getStoreWebsiteIdByCode($code)
166
    {
167
        return $this->getSubject()->getStoreWebsiteIdByCode($code);
168
    }
169
170
    /**
171
     * Query whether or not the passed value type is valid.
172
     *
173
     * @param string $valueType The value type to query for
174
     *
175
     * @return boolean TRUE if the value type is valid, else FALSE
176
     */
177
    protected function isValueType($valueType)
178
    {
179
        return $this->getValueTypes()->isValueType($valueType);
180
    }
181
182
    /**
183
     * Queries whether or not the passed customer group code matches all groups or not.
184
     *
185
     * @param string $code The customer group code to query for
186
     *
187
     * @return boolean TRUE if the customer group code matches, else FALSE
188
     */
189
    protected function isAllGroups($code)
190
    {
191
        return $this->getSubject()->isAllGroups($code);
192
    }
193
}
194