Completed
Push — master ( 8fac40...c170a7 )
by Kamil
42:58
created

PromotionProcessor::process()   C

Complexity

Conditions 8
Paths 14

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 22
rs 6.6037
c 1
b 0
f 0
cc 8
eloc 11
nc 14
nop 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\Promotion\Processor;
13
14
use Sylius\Component\Promotion\Action\PromotionApplicatorInterface;
15
use Sylius\Component\Promotion\Checker\Eligibility\PromotionEligibilityCheckerInterface;
16
use Sylius\Component\Promotion\Model\PromotionSubjectInterface;
17
use Sylius\Component\Promotion\Provider\PreQualifiedPromotionsProviderInterface;
18
19
/**
20
 * @author Saša Stamenković <[email protected]>
21
 */
22
final class PromotionProcessor implements PromotionProcessorInterface
23
{
24
    /**
25
     * @var PreQualifiedPromotionsProviderInterface
26
     */
27
    private $preQualifiedPromotionsProvider;
28
29
    /**
30
     * @var PromotionEligibilityCheckerInterface
31
     */
32
    private $promotionEligibilityChecker;
33
34
    /**
35
     * @var PromotionApplicatorInterface
36
     */
37
    private $promotionApplicator;
38
39
    /**
40
     * @param PreQualifiedPromotionsProviderInterface $preQualifiedPromotionsProvider
41
     * @param PromotionEligibilityCheckerInterface $promotionEligibilityChecker
42
     * @param PromotionApplicatorInterface $promotionApplicator
43
     */
44
    public function __construct(
45
        PreQualifiedPromotionsProviderInterface $preQualifiedPromotionsProvider,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $preQualifiedPromotionsProvider exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
46
        PromotionEligibilityCheckerInterface $promotionEligibilityChecker,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $promotionEligibilityChecker exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
47
        PromotionApplicatorInterface $promotionApplicator
48
    ) {
49
        $this->preQualifiedPromotionsProvider = $preQualifiedPromotionsProvider;
50
        $this->promotionEligibilityChecker = $promotionEligibilityChecker;
51
        $this->promotionApplicator = $promotionApplicator;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function process(PromotionSubjectInterface $subject)
58
    {
59
        foreach ($subject->getPromotions() as $promotion) {
60
            $this->promotionApplicator->revert($subject, $promotion);
61
        }
62
63
        $preQualifiedPromotions = $this->preQualifiedPromotionsProvider->getPromotions($subject);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $preQualifiedPromotions exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
64
65
        foreach ($preQualifiedPromotions as $promotion) {
66
            if ($promotion->isExclusive() && $this->promotionEligibilityChecker->isEligible($subject, $promotion)) {
67
                $this->promotionApplicator->apply($subject, $promotion);
68
69
                return;
70
            }
71
        }
72
73
        foreach ($preQualifiedPromotions as $promotion) {
74
            if (!$promotion->isExclusive() && $this->promotionEligibilityChecker->isEligible($subject, $promotion)) {
75
                $this->promotionApplicator->apply($subject, $promotion);
76
            }
77
        }
78
    }
79
}
80