/
Rule.php
467 lines (403 loc) · 9.44 KB
/
Rule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
<?php
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/
namespace Pimcore\Bundle\EcommerceFrameworkBundle\PricingManager;
use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
use Pimcore\Bundle\EcommerceFrameworkBundle\PricingManager\Action\CartActionInterface;
use Pimcore\Bundle\EcommerceFrameworkBundle\PricingManager\Action\ProductActionInterface;
use Pimcore\Bundle\EcommerceFrameworkBundle\PricingManager\Condition\BracketInterface;
use Pimcore\Bundle\EcommerceFrameworkBundle\PricingManager\Rule\Dao;
use Pimcore\Cache\RuntimeCache;
use Pimcore\Logger;
use Pimcore\Model\AbstractModel;
use Pimcore\Model\Exception\NotFoundException;
use Pimcore\Security\SecurityHelper;
/**
* @method Dao getDao()
*/
class Rule extends AbstractModel implements RuleInterface
{
/**
* @param int $id
*
* @return RuleInterface|null
*/
public static function getById($id)
{
$cacheKey = Dao::TABLE_NAME . '_' . $id;
try {
$rule = RuntimeCache::get($cacheKey);
} catch (\Exception $e) {
try {
$ruleClass = get_called_class();
/** @var Rule $rule */
$rule = new $ruleClass();
$rule->getDao()->getById($id);
RuntimeCache::set($cacheKey, $rule);
} catch (NotFoundException $ex) {
Logger::debug($ex->getMessage());
return null;
}
}
return $rule;
}
/**
* @var int|null
*/
protected $id;
/**
* @var string
*/
protected $name;
/**
* @var string[]
*/
protected $label = [];
/**
* @var string[]
*/
protected $description = [];
/**
* @var ConditionInterface|null
*/
protected ?ConditionInterface $condition = null;
/**
* @var ActionInterface[]
*/
protected $action = [];
/**
* @var string
*/
protected $behavior;
/**
* @var bool
*/
protected $active;
/**
* @var int
*/
protected $prio;
/**
* load model with serializes data from db
*
* @param string $key
* @param mixed $value
*
* @return AbstractModel
*
* @internal
*/
public function setValue($key, $value)
{
$method = 'set' . $key;
if (method_exists($this, $method)) {
switch ($method) {
// localized fields
case 'setlabel':
case 'setdescription':
$value = unserialize($value);
if ($value === false) {
return $this;
} else {
$this->$key = $value;
}
return $this;
// objects
case 'setactions':
case 'setcondition':
$value = unserialize($value);
if ($value === false) {
return $this;
}
}
$this->$method($value);
}
return $this;
}
/**
* @param int|null $id
*
* @return $this
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* @return int|null
*/
public function getId()
{
return $this->id;
}
/**
* @param string $label
* @param string $locale
*
* @return $this
*/
public function setLabel($label, $locale = null)
{
$this->label[$this->getLanguage($locale)] = $label;
return $this;
}
/**
* @param string $locale
*
* @return string|null
*/
public function getLabel($locale = null)
{
return $this->label[$this->getLanguage($locale)] ?? null;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
* @param string|null $locale
*
* @return $this
*/
public function setName($name, $locale = null)
{
$this->name = SecurityHelper::convertHtmlSpecialChars($name);
return $this;
}
/**
* @param string $description
* @param string $locale
*
* @return $this
*/
public function setDescription($description, $locale = null)
{
$this->description[$this->getLanguage($locale)] = $description;
return $this;
}
/**
* @param string $locale
*
* @return string|null
*/
public function getDescription($locale = null)
{
return $this->description[$this->getLanguage($locale)] ?? null;
}
/**
* @param string $behavior
*
* @return $this
*/
public function setBehavior($behavior)
{
$this->behavior = $behavior;
return $this;
}
/**
* @return string
*/
public function getBehavior()
{
return $this->behavior;
}
/**
* @param bool $active
*
* @return $this
*/
public function setActive($active)
{
$this->active = $active;
return $this;
}
/**
* @return bool
*/
public function getActive()
{
return $this->active;
}
/**
* @param ConditionInterface $condition
*
* @return $this
*/
public function setCondition(ConditionInterface $condition)
{
$this->condition = $condition;
return $this;
}
/**
* @return ConditionInterface|null
*/
public function getCondition(): ?ConditionInterface
{
return $this->condition;
}
/**
* @param ActionInterface[] $action
*
* @return $this
*/
public function setActions(array $action)
{
$this->action = $action;
return $this;
}
/**
* @return ActionInterface[]
*/
public function getActions()
{
return $this->action;
}
/**
* @param int $prio
*
* @return $this
*/
public function setPrio($prio)
{
$this->prio = (int)$prio;
return $this;
}
/**
* @return int
*/
public function getPrio()
{
return $this->prio;
}
/**
* @return $this
*/
public function save()
{
$this->getDao()->save();
return $this;
}
/**
* delete item
*/
public function delete()
{
$this->getDao()->delete();
}
/**
* test all conditions if this rule is valid
*
* @param EnvironmentInterface $environment
*
* @return bool
*/
public function check(EnvironmentInterface $environment)
{
$condition = $this->getCondition();
if ($condition) {
return $condition->check($environment);
}
return true;
}
/**
* checks if rule has at least one action that changes product price (and not cart price)
*
* @return bool
*/
public function hasProductActions()
{
foreach ($this->getActions() as $action) {
if ($action instanceof ProductActionInterface) {
return true;
}
}
return false;
}
/**
* checks if rule has at least one action that changes cart price
*
* @return bool
*/
public function hasCartActions()
{
foreach ($this->getActions() as $action) {
if ($action instanceof CartActionInterface) {
return true;
}
}
return false;
}
/**
* @param EnvironmentInterface $environment
*
* @return $this
*/
public function executeOnProduct(EnvironmentInterface $environment)
{
foreach ($this->getActions() as $action) {
if ($action instanceof ProductActionInterface) {
$action->executeOnProduct($environment);
}
}
return $this;
}
/**
* @param EnvironmentInterface $environment
*
* @return $this
*/
public function executeOnCart(EnvironmentInterface $environment)
{
foreach ($this->getActions() as $action) {
if ($action instanceof CartActionInterface) {
$action->executeOnCart($environment);
}
}
return $this;
}
/**
* gets current language
*
* @param string|null $language
*
* @return string
*/
protected function getLanguage($language = null)
{
if ($language) {
return (string) $language;
}
return Factory::getInstance()->getEnvironment()->getSystemLocale();
}
/**
* @param string $typeClass
*
* @return ConditionInterface[]
*/
public function getConditionsByType(string $typeClass): array
{
$conditions = [];
$rootCondition = $this->getCondition();
if ($rootCondition instanceof BracketInterface) {
$conditions = $rootCondition->getConditionsByType($typeClass);
} elseif ($rootCondition instanceof $typeClass) {
$conditions[] = $rootCondition;
}
return $conditions;
}
}