PaymentFeeCalculateAction   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 81
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 30 4
A __construct() 0 14 1
A getPaymentFee() 0 12 3
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * You can find more information about us on https://bitbag.io and write us
7
 * an email on [email protected].
8
 */
9
10
declare(strict_types=1);
11
12
namespace BitBag\SyliusMolliePlugin\Controller\Action\Shop;
13
14
use BitBag\SyliusMolliePlugin\Entity\MollieGatewayConfig;
15
use BitBag\SyliusMolliePlugin\Helper\ConvertPriceToAmount;
16
use BitBag\SyliusMolliePlugin\PaymentFee\Calculate;
17
use Liip\ImagineBundle\Exception\Config\Filter\NotFoundException;
18
use Sylius\Component\Core\Model\OrderInterface;
19
use Sylius\Component\Order\Aggregator\AdjustmentsAggregatorInterface;
20
use Sylius\Component\Order\Context\CartContextInterface;
21
use Sylius\Component\Resource\Repository\RepositoryInterface;
22
use Symfony\Component\HttpFoundation\JsonResponse;
23
use Symfony\Component\HttpFoundation\Request;
24
use Symfony\Component\HttpFoundation\Response;
25
use Twig\Environment;
26
27
final class PaymentFeeCalculateAction implements PaymentFeeCalculateActionInterface
28
{
29
    /** @var Calculate */
30
    private $calculate;
31
32
    /** @var CartContextInterface */
33
    private $cartContext;
34
35
    /** @var RepositoryInterface */
36
    private $methodRepository;
37
38
    /** @var AdjustmentsAggregatorInterface */
39
    private $adjustmentsAggregator;
40
41
    /** @var ConvertPriceToAmount */
42
    private $convertPriceToAmount;
43
44
    /** @var Environment */
45
    private $twig;
46
47
    public function __construct(
48
        Calculate $calculate,
49
        CartContextInterface $cartContext,
50
        RepositoryInterface $methodRepository,
51
        AdjustmentsAggregatorInterface $adjustmentsAggregator,
52
        ConvertPriceToAmount $convertPriceToAmount,
53
        Environment $twig
54
    ) {
55
        $this->calculate = $calculate;
56
        $this->cartContext = $cartContext;
57
        $this->methodRepository = $methodRepository;
58
        $this->adjustmentsAggregator = $adjustmentsAggregator;
59
        $this->convertPriceToAmount = $convertPriceToAmount;
60
        $this->twig = $twig;
61
    }
62
63
    public function __invoke(Request $request, string $methodId): Response
64
    {
65
        $order = $this->cartContext->getCart();
66
        $method = $this->methodRepository->findOneBy(['methodId' => $methodId]);
67
68
        if (!$method instanceof MollieGatewayConfig) {
69
            throw new NotFoundException(sprintf('Method with id %s not found', $methodId));
70
        }
71
72
        /** @var OrderInterface $calculatedOrder */
73
        $calculatedOrder = $this->calculate->calculateFromCart($order, $method);
74
75
        if (null === $calculatedOrder) {
76
            return new JsonResponse([], Response::HTTP_OK);
77
        }
78
79
        $paymentFee = $this->getPaymentFee($calculatedOrder);
80
81
        if (empty($paymentFee)) {
82
            return new JsonResponse([], Response::HTTP_OK);
83
        }
84
85
        return new JsonResponse([
86
            'view' => $this->twig->render(
87
                'BitBagSyliusMolliePlugin:Shop/PaymentMollie:_paymentFeeTableTr.html.twig',
88
                [
89
                    'paymentFee' => $this->convertPriceToAmount->convert(reset($paymentFee)),
90
                ]
91
            ),
92
            'orderTotal' => $this->convertPriceToAmount->convert($calculatedOrder->getTotal()),
93
        ]);
94
    }
95
96
    private function getPaymentFee(OrderInterface $calculatedOrder): array
97
    {
98
        foreach (self::PAYMENTS_FEE_METHOD as $paymentFee) {
99
            $adjustmentsRecursively = $calculatedOrder->getAdjustmentsRecursively($paymentFee);
100
            if ($adjustmentsRecursively->isEmpty()) {
101
                continue;
102
            }
103
104
            return $this->adjustmentsAggregator->aggregate($adjustmentsRecursively);
105
        }
106
107
        return [];
108
    }
109
}
110