Completed
Push — master ( c0f573...fc0310 )
by Paweł
32:42 queued 22:36
created

HasTaxonRuleChecker::hasProductValidTaxon()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 2
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