TierPriceSubject   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 234
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 20
eloc 39
c 4
b 1
f 0
dl 0
loc 234
ccs 0
cts 47
cp 0
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A mapCustomerGroupCode() 0 3 2
A setLastPk() 0 3 1
A mapWebsiteCode() 0 3 2
A getLastEntityId() 0 3 1
A getStoreWebsiteIdByCode() 0 3 1
A getProcessedTierPrices() 0 3 1
A setUp() 0 28 6
A getCustomerGroupIdByCode() 0 12 2
A isAllGroups() 0 3 1
A addProcessedTierPrice() 0 3 1
A getLastPk() 0 3 1
A tearDown() 0 16 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\TierPrice\Subjects\TierPriceSubject
5
 *
6
 * PHP version 7
7
 *
8
 * @author    Klaas-Tido Rühl <[email protected]>
9
 * @author    Tim Wagner <[email protected]>
10
 * @copyright 2019 REFUSiON GmbH <[email protected]>
11
 * @license   https://opensource.org/licenses/MIT
12
 * @link      https://github.com/techdivision/import-product-tier-price
13
 * @link      https://www.techdivision.com
14
 * @link      https://www.refusion.com
15
 */
16
17
namespace TechDivision\Import\Product\TierPrice\Subjects;
18
19
use TechDivision\Import\Product\TierPrice\Utils\DefaultCodes;
20
use TechDivision\Import\Product\TierPrice\Utils\MemberNames;
21
use TechDivision\Import\Product\TierPrice\Utils\RegistryKeys;
22
use TechDivision\Import\Product\TierPrice\Utils\ConfigurationKeys;
23
use TechDivision\Import\Product\Subjects\AbstractProductSubject;
24
25
/**
26
 * Subject for processing tier prices.
27
 *
28
 * @author    Klaas-Tido Rühl <[email protected]>
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2019 REFUSiON GmbH <[email protected]>
31
 * @license   https://opensource.org/licenses/MIT
32
 * @link      https://github.com/techdivision/import-product-tier-price
33
 * @link      https://www.techdivision.com
34
 * @link      https://www.refusion.com
35
 */
36
class TierPriceSubject extends AbstractProductSubject
37
{
38
39
    /**
40
     * The array with the default customer group code mappings.
41
     *
42
     * @var array
43
     */
44
    protected $customerGroupCodeMappings = array(
45
        DefaultCodes::ALL_GROUPS => 'NOT LOGGED IN'
46
    );
47
48
    /**
49
     * The array with the default website code mappings.
50
     *
51
     * @var array
52
     */
53
    protected $websiteCodeMappings = array(
54
        DefaultCodes::ALL_WEBSITES => 'admin'
55
    );
56
57
    /**
58
     * The existing customer groups.
59
     *
60
     * @var array
61
     */
62
    protected $customerGroups = array();
63
64
    /**
65
     * The array that contains the IDs of the processed tier prices
66
     *
67
     * @var array
68
     */
69
    protected $processedTierPrices = array();
70
71
    /**
72
     * The ID of the PK of the related product that has been processed recently.
73
     *
74
     * @var integer
75
     */
76
    protected $lastPk;
77
78
    /**
79
     * Intializes the previously loaded global data for exactly one variants.
80
     *
81
     * @param string $serial The serial of the actual import
82
     *
83
     * @return void
84
     */
85
    public function setUp($serial)
86
    {
87
88
        // invoke the parent method
89
        parent::setUp($serial);
90
91
        // load the status of the actual import process
92
        $status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS);
93
94
        // load the available customer groups from the registry
95
        $this->customerGroups = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::CUSTOMER_GROUPS];
96
97
        // load the value IDs of the processed tier prices
98
        if (isset($status[RegistryKeys::PROCESSED_TIER_PRICES])) {
99
            $this->processedTierPrices = $status[RegistryKeys::PROCESSED_TIER_PRICES];
100
        }
101
102
        // merge the customer group code mappings from the configuration
103
        if ($this->getConfiguration()->hasParam(ConfigurationKeys::CUSTOMER_GROUP_CODE_MAPPINGS)) {
104
            foreach ($this->getConfiguration()->getParam(ConfigurationKeys::CUSTOMER_GROUP_CODE_MAPPINGS) as $row => $system) {
105
                $this->customerGroupCodeMappings[$row] = $system;
106
            }
107
        }
108
109
        // merge the website code mappings from the configuration
110
        if ($this->getConfiguration()->hasParam(ConfigurationKeys::WEBSITE_CODE_MAPPINGS)) {
111
            foreach ($this->getConfiguration()->getParam(ConfigurationKeys::WEBSITE_CODE_MAPPINGS) as $row => $system) {
112
                $this->websiteCodeMappings[$row] = $system;
113
            }
114
        }
115
    }
116
117
    /**
118
     * Clean up the global data after importing the bunch.
119
     *
120
     * @param string $serial The serial of the actual import
121
     *
122
     * @return void
123
     */
124
    public function tearDown($serial)
125
    {
126
127
        // load the registry processor
128
        $registryProcessor = $this->getRegistryProcessor();
129
130
        // update the status
131
        $registryProcessor->mergeAttributesRecursive(
132
            RegistryKeys::STATUS,
133
            array(
134
                RegistryKeys::PROCESSED_TIER_PRICES => $this->processedTierPrices
135
            )
136
        );
137
138
        // invoke the parent method
139
        parent::tearDown($serial);
140
    }
141
142
    /**
143
     * Add the ID of the processed tier price.
144
     *
145
     * @param integer $valueId The ID of the processed tier price
146
     * @param integer $pk      The PK of the related product
147
     *
148
     * @return void
149
     */
150
    public function addProcessedTierPrice($valueId, $pk)
151
    {
152
        $this->processedTierPrices[$valueId][] = $pk;
153
    }
154
155
    /**
156
     * Returns the array with the processed tier prices.
157
     *
158
     * @return array The array with the processed tier prices
159
     */
160
    public function getProcessedTierPrices()
161
    {
162
        return $this->processedTierPrices;
163
    }
164
165
    /**
166
     * Returns the customer group ID for the given code, if it exists.
167
     *
168
     * @param string $code The code of the requested customer group
169
     *
170
     * @return integer The ID of the customer group
171
     * @throws \Exception Is thrown, if the customer group with the passed ID is NOT available
172
     */
173
    public function getCustomerGroupIdByCode($code)
174
    {
175
176
        // map the customer group code and query whether or not the customer group with the passed code is available
177
        if (isset($this->customerGroups[$code = $this->mapCustomerGroupCode($code)])) {
178
            return (integer) $this->customerGroups[$code][MemberNames::CUSTOMER_GROUP_ID];
179
        }
180
181
        // throw an exception if the customer group with the passed code is NOT available
182
        throw new \Exception(
183
            $this->appendExceptionSuffix(
184
                sprintf('Found invalid customer group "%s"', $code)
185
            )
186
        );
187
    }
188
189
    /**
190
     * Set's the ID of the last PK used.
191
     *
192
     * @param integer $lastPk The PK
193
     *
194
     * @return void
195
     */
196
    public function setLastPk($lastPk)
197
    {
198
        $this->lastPk = $lastPk;
199
    }
200
201
    /**
202
     * Returns the ID of the last PK used.
203
     *
204
     * @return integer The PK
205
     */
206
    public function getLastPk()
207
    {
208
        return $this->lastPk;
209
    }
210
211
    /**
212
     * Return's the ID of the product that has been created recently.
213
     *
214
     * @return integer The entity Id
215
     * @see \TechDivision\Import\Product\Subjects\AbstractProductSubject::getLastEntityId()
216
     */
217
    public function getLastEntityId()
218
    {
219
        return $this->getLastPk();
220
    }
221
222
    /**
223
     * Return's the store website for the passed code.
224
     *
225
     * @param string $code The code of the store website to return the ID for
226
     *
227
     * @return integer The store website ID
228
     * @throws \Exception Is thrown, if the store website with the requested code is not available
229
     * @see \TechDivision\Import\Product\Subjects\AbstractProductSubject::getStoreWebsiteIdByCode()
230
     */
231
    public function getStoreWebsiteIdByCode($code)
232
    {
233
        return parent::getStoreWebsiteIdByCode($this->mapWebsiteCode($code));
234
    }
235
236
    /**
237
     * Queries whether or not the passed customer group code matches all groups or not.
238
     *
239
     * @param string $code The customer group code to query for
240
     *
241
     * @return boolean TRUE if the customer group code matches, else FALSE
242
     */
243
    public function isAllGroups($code)
244
    {
245
        return DefaultCodes::ALL_GROUPS === strtoupper($code);
246
    }
247
248
    /**
249
     * Maps the passed customer group code, if a mapping is available.
250
     *
251
     * @param string $code The customer group code to map
252
     *
253
     * @return string The mapped customer group code
254
     */
255
    protected function mapCustomerGroupCode($code)
256
    {
257
        return isset($this->customerGroupCodeMappings[$code]) ? $this->customerGroupCodeMappings[$code] : $code;
258
    }
259
260
    /**
261
     * Maps the passed website code, if a mapping is available.
262
     *
263
     * @param string $code The website code to map
264
     *
265
     * @return string The mapped website code
266
     */
267
    protected function mapWebsiteCode($code)
268
    {
269
        return isset($this->websiteCodeMappings[$code]) ? $this->websiteCodeMappings[$code] : $code;
270
    }
271
}
272