Completed
Push — master ( fcfa74...4ea0bb )
by Paweł
20s
created

CustomerGroupRuleChecker   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 0
cbo 4
dl 0
loc 34
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B isEligible() 0 20 5
A getConfigurationFormType() 0 4 1
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\Bundle\CoreBundle\Form\Type\Promotion\Rule\CustomerGroupConfigurationType;
15
use Sylius\Component\Core\Model\CustomerInterface;
16
use Sylius\Component\Core\Model\OrderInterface;
17
use Sylius\Component\Promotion\Checker\Rule\RuleCheckerInterface;
18
use Sylius\Component\Promotion\Exception\UnsupportedTypeException;
19
use Sylius\Component\Promotion\Model\PromotionSubjectInterface;
20
21
/**
22
 * @author Michał Marcinkowski <[email protected]>
23
 */
24
class CustomerGroupRuleChecker 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
        if (null === $customer = $subject->getCustomer()) {
36
            return false;
37
        }
38
39
        if (!$customer instanceof CustomerInterface) {
40
            return false;
41
        }
42
43
        if (null === $customer->getGroup()) {
44
            return false;
45
        }
46
47
        return $configuration['group_code'] === $customer->getGroup()->getCode();
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getConfigurationFormType()
54
    {
55
        return CustomerGroupConfigurationType::class;
56
    }
57
}
58