1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Component\Core\Promotion\Checker\Rule; |
13
|
|
|
|
14
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
15
|
|
|
use Sylius\Component\Core\Model\OrderItemInterface; |
16
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
17
|
|
|
use Sylius\Component\Promotion\Checker\Rule\RuleCheckerInterface; |
18
|
|
|
use Sylius\Component\Promotion\Exception\UnsupportedTypeException; |
19
|
|
|
use Sylius\Component\Promotion\Model\PromotionSubjectInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Saša Stamenković <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
final class HasTaxonRuleChecker implements RuleCheckerInterface |
25
|
|
|
{ |
26
|
|
|
const TYPE = 'has_taxon'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function isEligible(PromotionSubjectInterface $subject, array $configuration) |
32
|
|
|
{ |
33
|
|
|
if (!isset($configuration['taxons'])) { |
34
|
|
|
return; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if (!$subject instanceof OrderInterface) { |
38
|
|
|
throw new UnsupportedTypeException($subject, OrderInterface::class); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/* @var $item OrderItemInterface */ |
42
|
|
|
foreach ($subject->getItems() as $item) { |
43
|
|
|
if ($this->hasProductValidTaxon($item->getProduct(), $configuration)) { |
44
|
|
|
return true; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return false; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param ProductInterface $product |
53
|
|
|
* @param array $configuration |
54
|
|
|
* |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
private function hasProductValidTaxon(ProductInterface $product, array $configuration) |
58
|
|
|
{ |
59
|
|
|
foreach ($product->getTaxons() as $taxon) { |
60
|
|
|
if (in_array($taxon->getCode(), $configuration['taxons'], true)) { |
61
|
|
|
return true; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return false; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|