Completed
Push — 2.x ( 48ed54...fe246a )
by Tim
12s queued 10s
created

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