Completed
Pull Request — master (#290)
by
unknown
41:51
created

AddCouponHandler   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 61
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 2
c 0
b 0
f 0
lcom 1
cbo 1
dl 61
loc 61
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 1
A handle() 17 17 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sylius\SyliusShopApiPlugin\Handler;
6
7
use Sylius\Component\Core\Model\OrderInterface;
8
use Sylius\Component\Core\Model\PromotionCouponInterface;
9
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
10
use Sylius\Component\Order\Processor\OrderProcessorInterface;
11
use Sylius\Component\Promotion\Checker\Eligibility\PromotionCouponEligibilityCheckerInterface;
12
use Sylius\Component\Promotion\Repository\PromotionCouponRepositoryInterface;
13
use Sylius\ShopApiPlugin\Command\AddCoupon;
14
use Webmozart\Assert\Assert;
15
16 View Code Duplication
final class AddCouponHandler
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
17
{
18
    /**
19
     * @var OrderRepositoryInterface
20
     */
21
    private $orderRepository;
22
23
    /**
24
     * @var PromotionCouponRepositoryInterface
25
     */
26
    private $couponRepository;
27
28
    /**
29
     * @var OrderProcessorInterface
30
     */
31
    private $orderProcessor;
32
33
    /**
34
     * @var PromotionCouponEligibilityCheckerInterface
35
     */
36
    private $couponEligibilityChecker;
37
38
    /**
39
     * @param OrderRepositoryInterface $orderRepository
40
     * @param PromotionCouponRepositoryInterface $couponRepository
41
     * @param OrderProcessorInterface $orderProcessor
42
     * @param PromotionCouponEligibilityCheckerInterface $couponEligibilityChecker
43
     */
44
    public function __construct(
45
        OrderRepositoryInterface $orderRepository,
46
        PromotionCouponRepositoryInterface $couponRepository,
47
        OrderProcessorInterface $orderProcessor,
48
        PromotionCouponEligibilityCheckerInterface $couponEligibilityChecker
49
    ) {
50
        $this->orderRepository = $orderRepository;
51
        $this->couponRepository = $couponRepository;
52
        $this->orderProcessor = $orderProcessor;
53
        $this->couponEligibilityChecker = $couponEligibilityChecker;
54
    }
55
56
    /**
57
     * @param AddCoupon $addCoupon
58
     */
59
    public function handle(AddCoupon $addCoupon)
60
    {
61
        /** @var OrderInterface $cart */
62
        $cart = $this->orderRepository->findOneBy(['tokenValue' => $addCoupon->orderToken()]);
63
64
        Assert::notNull($cart, sprintf('Cart with token %s has not been found.', $addCoupon->orderToken()));
65
66
        /** @var PromotionCouponInterface $coupon */
67
        $coupon = $this->couponRepository->findOneBy(['code' => $addCoupon->couponCode()]);
68
69
        Assert::notNull($coupon, sprintf('Coupon with code %s has not been found.', $addCoupon->couponCode()));
70
        Assert::true($this->couponEligibilityChecker->isEligible($cart, $coupon));
71
72
        $cart->setPromotionCoupon($coupon);
73
74
        $this->orderProcessor->process($cart);
75
    }
76
}
77