ProductQuantityRuleChecker::isEligible()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 11
c 2
b 0
f 0
dl 0
loc 19
rs 8.8333
cc 7
nc 6
nop 2
1
<?php
2
3
namespace DanielRolland\SyliusEnhancedPromotionsPlugin\Promotion\Checker;
4
5
use Sylius\Component\Core\Model\OrderInterface;
6
use Sylius\Component\Core\Model\OrderItemInterface;
7
use Sylius\Component\Core\Model\ProductInterface;
8
use Sylius\Component\Promotion\Checker\Rule\RuleCheckerInterface;
9
use Sylius\Component\Promotion\Exception\UnsupportedTypeException;
10
use Sylius\Component\Promotion\Model\PromotionSubjectInterface;
11
12
class ProductQuantityRuleChecker implements RuleCheckerInterface
13
{
14
15
    public const TYPE = 'product_quantity';
16
17
    public function isEligible(PromotionSubjectInterface $subject, array $configuration): bool
18
    {
19
        if (!$subject instanceof OrderInterface) {
20
            throw new UnsupportedTypeException($subject, OrderInterface::class);
21
        }
22
        /** @var OrderItemInterface $item */
23
        foreach ($subject->getItems() as $item) {
24
            if (!($configuration['product'] instanceof ProductInterface)) {
25
                throw new UnsupportedTypeException($configuration['product'], ProductInterface::class);
26
            }
27
            $product = $item->getProduct();
28
            if ($product instanceof ProductInterface) {
29
                if ($configuration['product']->getCode() === $product->getCode()
30
                    && $configuration['quantity'] <= $item->getQuantity()) {
31
                    return true;
32
                }
33
            }
34
        }
35
        return false;
36
    }
37
}
38