Completed
Push — master ( 0f74a6...eca3e7 )
by Kamil
20:39
created

NthOrderRuleChecker   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 45
rs 10
c 2
b 0
f 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B isEligible() 0 16 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;
13
14
use Sylius\Component\Core\Model\OrderInterface;
15
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
16
use Sylius\Component\Payment\Model\PaymentInterface;
17
use Sylius\Component\Promotion\Checker\RuleCheckerInterface;
18
use Sylius\Component\Promotion\Exception\UnsupportedTypeException;
19
use Sylius\Component\Promotion\Model\PromotionSubjectInterface;
20
21
/**
22
 * @author Saša Stamenković <[email protected]>
23
 * @author Joseph Bielawski <[email protected]>
24
 */
25
class NthOrderRuleChecker implements RuleCheckerInterface
26
{
27
    const TYPE = 'nth_order';
28
29
    /**
30
     * @var OrderRepositoryInterface
31
     */
32
    private $orderRepository;
33
34
    /**
35
     * @param OrderRepositoryInterface $orderRepository
36
     */
37
    public function __construct(OrderRepositoryInterface $orderRepository)
38
    {
39
        $this->orderRepository = $orderRepository;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function isEligible(PromotionSubjectInterface $subject, array $configuration)
46
    {
47
        if (!$subject instanceof OrderInterface) {
48
            throw new UnsupportedTypeException($subject, OrderInterface::class);
49
        }
50
51
        if (!isset($configuration['nth']) || !is_int($configuration['nth'])) {
52
            return false;
53
        }
54
55
        if (null === $customer = $subject->getCustomer()) {
56
            return false;
57
        }
58
59
        return $this->orderRepository->countByCustomerAndPaymentState($customer, PaymentInterface::STATE_COMPLETED) === ($configuration['nth'] - 1);
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getConfigurationFormType()
66
    {
67
        return 'sylius_promotion_rule_nth_order_configuration';
68
    }
69
}
70