Completed
Push — master ( 06fc05...42cf72 )
by Michał
12:50
created

ContainsProductRuleChecker::isEligible()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 17
rs 9.2
c 1
b 0
f 0
cc 4
eloc 8
nc 4
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
 * @author Alexandre Bacco <[email protected]>
22
 * @author Mateusz Zalewski <[email protected]>
23
 */
24
class ContainsProductRuleChecker implements RuleCheckerInterface
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function isEligible(PromotionSubjectInterface $subject, array $configuration)
30
    {
31
        if (!$subject instanceof OrderInterface) {
32
            throw new UnsupportedTypeException($subject, OrderInterface::class);
33
        }
34
35
        /* @var $item OrderItemInterface */
36
        foreach ($subject->getItems() as $item) {
37
            if ($configuration['variant'] != $item->getVariant()->getId()) {
38
                continue;
39
            }
40
41
            return $this->isItemEligible($item, $configuration);
42
        }
43
44
        return (bool) $configuration['exclude'];
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function getConfigurationFormType()
51
    {
52
        return 'sylius_promotion_rule_contains_product_configuration';
53
    }
54
55
    /**
56
     * @param OrderItemInterface $item
57
     * @param array $configuration
58
     *
59
     * @return bool
60
     */
61
    private function isItemEligible(OrderItemInterface $item, array $configuration)
62
    {
63
        if (!$configuration['exclude']) {
64
            if (isset($configuration['count'])) {
65
                return $this->isItemQuantityEligible($item->getQuantity(), $configuration);
66
            }
67
68
            return true;
69
        }
70
71
        return false;
72
    }
73
74
    /**
75
     * @param int $quantity
76
     * @param array $configuration
77
     *
78
     * @return bool
79
     */
80
    private function isItemQuantityEligible($quantity, array $configuration)
81
    {
82
        if (isset($configuration['equal']) && $configuration['equal']) {
83
            return $quantity >= $configuration['count'];
84
        }
85
86
        return $quantity > $configuration['count'];
87
    }
88
}
89