ProductQuantityRuleChecker   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
eloc 13
c 2
b 0
f 0
dl 0
loc 24
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B isEligible() 0 19 7
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