Passed
Push — master ( b464c5...0acd20 )
by
unknown
04:48
created

Calculate::calculatePaymentSurcharge()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 22
rs 8.8333
cc 7
nc 6
nop 2
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
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusMolliePlugin\PaymentFee;
14
15
use BitBag\SyliusMolliePlugin\Entity\MollieGatewayConfig;
16
use BitBag\SyliusMolliePlugin\Exceptions\UnknownPaymentSurchargeTye;
17
use BitBag\SyliusMolliePlugin\PaymentFee\Types\FixedAmount;
18
use BitBag\SyliusMolliePlugin\PaymentFee\Types\FixedAmountAndPercentage;
19
use BitBag\SyliusMolliePlugin\PaymentFee\Types\Percentage;
20
use Sylius\Component\Order\Model\OrderInterface;
21
22
final class Calculate
23
{
24
    /** @var FixedAmount */
25
    private $fixedAmount;
26
27
    /** @var Percentage */
28
    private $percentage;
29
30
    /** @var FixedAmountAndPercentage */
31
    private $fixedAmountAndPercentage;
32
33
    public function __construct(
34
        FixedAmount $fixedAmount,
35
        Percentage $percentage,
36
        FixedAmountAndPercentage $fixedAmountAndPercentage
37
    ) {
38
        $this->fixedAmount = $fixedAmount;
39
        $this->percentage = $percentage;
40
        $this->fixedAmountAndPercentage = $fixedAmountAndPercentage;
41
    }
42
43
    public function calculateFromCart(OrderInterface $order, MollieGatewayConfig $paymentMethod): ?OrderInterface
44
    {
45
        if (null === $paymentMethod->getPaymentSurchargeFee()->getType()) {
46
            return null;
47
        }
48
49
        return $this->calculatePaymentSurcharge($order, $paymentMethod);
50
    }
51
52
    private function calculatePaymentSurcharge(OrderInterface $order, MollieGatewayConfig $paymentMethod): OrderInterface
53
    {
54
        if (empty($paymentMethod->getPaymentSurchargeFee()->getType()) || $paymentMethod->getPaymentSurchargeFee()->getType() === ' ') {
55
            return $order;
56
        }
57
58
        $paymentType = $paymentMethod->getPaymentSurchargeFee()->getType();
59
60
        if ($this->fixedAmount->canCalculate($paymentType)) {
61
            return $this->fixedAmount->calculate($order, $paymentMethod);
62
        }
63
        if ($this->percentage->canCalculate($paymentType)) {
64
            return $this->percentage->calculate($order, $paymentMethod);
65
        }
66
        if ($this->fixedAmountAndPercentage->canCalculate($paymentType)) {
67
            return $this->fixedAmountAndPercentage->calculate($order, $paymentMethod);
68
        }
69
        if ('no_fee' === $paymentType) {
70
            return $order;
71
        }
72
73
        throw new UnknownPaymentSurchargeTye(sprintf('Unknown payment type %s', $paymentType));
74
    }
75
}
76