Completed
Branch master (9d8d38)
by Łukasz
03:25
created

AddCouponHandler   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 51
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A handle() 0 15 1
1
<?php
2
3
namespace Sylius\ShopApiPlugin\Handler;
4
5
use Sylius\Component\Core\Model\OrderInterface;
6
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
7
use Sylius\Component\Order\Processor\OrderProcessorInterface;
8
use Sylius\Component\Promotion\Repository\PromotionCouponRepositoryInterface;
9
use Sylius\ShopApiPlugin\Command\AddCoupon;
10
use Webmozart\Assert\Assert;
11
12
final class AddCouponHandler
13
{
14
    /**
15
     * @var OrderRepositoryInterface
16
     */
17
    private $orderRepository;
18
19
    /**
20
     * @var PromotionCouponRepositoryInterface
21
     */
22
    private $couponRepository;
23
24
    /**
25
     * @var OrderProcessorInterface
26
     */
27
    private $orderProcessor;
28
29
    /**
30
     * @param OrderRepositoryInterface $orderRepository
31
     * @param PromotionCouponRepositoryInterface $couponRepository
32
     * @param OrderProcessorInterface $orderProcessor
33
     */
34
    public function __construct(
35
        OrderRepositoryInterface $orderRepository,
36
        PromotionCouponRepositoryInterface $couponRepository,
37
        OrderProcessorInterface $orderProcessor
38
    ) {
39
        $this->orderRepository = $orderRepository;
40
        $this->couponRepository = $couponRepository;
41
        $this->orderProcessor = $orderProcessor;
42
    }
43
44
    /**
45
     * @param AddCoupon $addCoupon
46
     */
47
    public function handle(AddCoupon $addCoupon)
48
    {
49
        /** @var OrderInterface $cart */
50
        $cart = $this->orderRepository->findOneBy(['tokenValue' => $addCoupon->orderToken()]);
51
52
        Assert::notNull($cart, sprintf('Cart with token %s has not been found.', $addCoupon->orderToken()));
53
54
        $coupon = $this->couponRepository->findOneBy(['code' => $addCoupon->couponCode()]);
55
56
        Assert::notNull($coupon, sprintf('Coupon with code %s has not been found.', $addCoupon->couponCode()));
57
58
        $cart->setPromotionCoupon($coupon);
0 ignored issues
show
Bug introduced by
It seems like $coupon defined by $this->couponRepository-...dCoupon->couponCode())) on line 54 can also be of type object; however, Sylius\Component\Core\Mo...e::setPromotionCoupon() does only seem to accept null|object<Sylius\Compo...omotionCouponInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
59
60
        $this->orderProcessor->process($cart);
61
    }
62
}
63