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

TierPriceSubject::mapCustomerGroupCode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

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