FixedAmount   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 32
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A canCalculate() 0 3 1
A calculate() 0 17 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
 * 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\PaymentFee\Types;
13
14
use BitBag\SyliusMolliePlugin\Entity\MollieGatewayConfig;
15
use BitBag\SyliusMolliePlugin\Order\AdjustmentInterface;
16
use BitBag\SyliusMolliePlugin\Payments\PaymentTerms\Options;
17
use Sylius\Component\Order\Factory\AdjustmentFactoryInterface;
18
use Sylius\Component\Order\Model\OrderInterface;
19
20
final class FixedAmount implements SurchargeTypeInterface
21
{
22
    /** @var AdjustmentFactoryInterface */
23
    private $adjustmentFactory;
24
25
    public function __construct(AdjustmentFactoryInterface $adjustmentFactory)
26
    {
27
        $this->adjustmentFactory = $adjustmentFactory;
28
    }
29
30
    public function calculate(OrderInterface $order, MollieGatewayConfig $paymentMethod): OrderInterface
31
    {
32
        $fixedAmount = $paymentMethod->getPaymentSurchargeFee()->getFixedAmount();
33
34
        if ($order->getAdjustments(AdjustmentInterface::FIXED_AMOUNT_ADJUSTMENT)) {
35
            $order->removeAdjustments(AdjustmentInterface::FIXED_AMOUNT_ADJUSTMENT);
36
        }
37
38
        /** @var AdjustmentInterface $adjustment */
39
        $adjustment = $this->adjustmentFactory->createNew();
40
        $adjustment->setType(AdjustmentInterface::FIXED_AMOUNT_ADJUSTMENT);
41
        $adjustment->setAmount((int) ($fixedAmount * 100));
42
        $adjustment->setNeutral(false);
43
44
        $order->addAdjustment($adjustment);
45
46
        return $order;
47
    }
48
49
    public function canCalculate(string $type): bool
50
    {
51
        return array_search($type, Options::getAvailablePaymentSurchargeFeeType()) === Options::FIXED_FEE;
52
    }
53
}
54