Completed
Push — master ( 59db0f...abafe8 )
by Kamil
18:55
created

TaxonRuleChecker::isEligible()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 8.8571
cc 5
eloc 8
nc 5
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;
13
14
use Sylius\Component\Core\Model\OrderInterface;
15
use Sylius\Component\Core\Model\OrderItemInterface;
16
use Sylius\Component\Promotion\Checker\RuleCheckerInterface;
17
use Sylius\Component\Promotion\Exception\UnsupportedTypeException;
18
use Sylius\Component\Promotion\Model\PromotionSubjectInterface;
19
20
/**
21
 * Checks if order contains products with given taxon.
22
 *
23
 * @author Saša Stamenković <[email protected]>
24
 */
25
class TaxonRuleChecker implements RuleCheckerInterface
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function isEligible(PromotionSubjectInterface $subject, array $configuration)
31
    {
32
        if (!$subject instanceof OrderInterface) {
33
            throw new UnsupportedTypeException($subject, OrderInterface::class);
34
        }
35
36
        /* @var $item OrderItemInterface */
37
        foreach ($subject->getItems() as $item) {
38
            foreach ($item->getProduct()->getTaxons() as $taxon) {
39
                if ($configuration['taxons']->contains($taxon->getId())) {
40
                    return !$configuration['exclude'];
41
                }
42
            }
43
        }
44
45
        return (Boolean) $configuration['exclude'];
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function getConfigurationFormType()
52
    {
53
        return 'sylius_promotion_rule_taxon_configuration';
54
    }
55
}
56