Completed
Push — symfony3 ( 405d0c...88ded0 )
by Kamil
32:03 queued 12:32
created

HasTaxonRuleChecker::isEligible()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
cc 6
eloc 10
nc 6
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\Promotion\Checker\Rule\RuleCheckerInterface;
17
use Sylius\Component\Promotion\Exception\UnsupportedTypeException;
18
use Sylius\Component\Promotion\Model\PromotionSubjectInterface;
19
20
/**
21
 * @author Saša Stamenković <[email protected]>
22
 */
23
final class HasTaxonRuleChecker implements RuleCheckerInterface
24
{
25
    const TYPE = 'has_taxon';
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function isEligible(PromotionSubjectInterface $subject, array $configuration)
31
    {
32
        if (!isset($configuration['taxons'])) {
33
            return;
34
        }
35
36
        if (!$subject instanceof OrderInterface) {
37
            throw new UnsupportedTypeException($subject, OrderInterface::class);
38
        }
39
40
        /* @var $item OrderItemInterface */
41
        foreach ($subject->getItems() as $item) {
42
            foreach ($item->getProduct()->getTaxons() as $taxon) {
43
                if (in_array($taxon->getCode(), $configuration['taxons'], true)) {
44
                    return true;
45
                }
46
            }
47
        }
48
49
        return false;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getConfigurationFormType()
56
    {
57
        return 'sylius_promotion_rule_has_taxon_configuration';
58
    }
59
}
60