Passed
Pull Request — master (#57)
by Roman
08:36 queued 17s
created

PriceDistributor   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
c 1
b 0
f 0
dl 0
loc 105
rs 10
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A distributeOrderExpensesPrices() 0 8 2
A findPayonePayment() 0 10 3
A distribute() 0 22 3
A distributeOrderItemsPrices() 0 14 2
A calculateRoundedPrice() 0 7 1
1
<?php
2
3
/**
4
 * MIT License
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\Payone\Business\Distributor;
9
10
use Generated\Shared\Transfer\OrderTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\OrderTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Generated\Shared\Transfer\PaymentTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\PaymentTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
class PriceDistributor implements PriceDistributorInterface
14
{
15
    protected const PAYMENT_PROVIDER_NAME = 'Payone';
16
17
    /**
18
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
19
     *
20
     * @return \Generated\Shared\Transfer\OrderTransfer
21
     */
22
    public function distribute(OrderTransfer $orderTransfer): OrderTransfer
23
    {
24
        if ($orderTransfer->getPayments()->count() <= 1) {
25
            return $orderTransfer;
26
        }
27
28
        $payonePayment = $this->findPayonePayment($orderTransfer);
29
        if ($payonePayment === null) {
30
            return $orderTransfer;
31
        }
32
33
        $totals = $orderTransfer->getTotals();
34
35
        $roundingError = 0;
36
        $priceRatio = $payonePayment->getAmount() / $totals->getGrandTotal();
37
38
        $this->distributeOrderItemsPrices($orderTransfer, $priceRatio, $roundingError);
39
        $this->distributeOrderExpensesPrices($orderTransfer, $priceRatio, $roundingError);
40
41
        $orderTransfer->setTotals($totals->setGrandTotal($payonePayment->getAmount()));
42
43
        return $orderTransfer;
44
    }
45
46
    /**
47
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
48
     *
49
     * @return \Generated\Shared\Transfer\PaymentTransfer|null
50
     */
51
    protected function findPayonePayment(OrderTransfer $orderTransfer): ?PaymentTransfer
52
    {
53
        $orderTransfer->getPayments();
54
        foreach ($orderTransfer->getPayments() as $payment) {
55
            if ($payment->getPaymentProvider() === static::PAYMENT_PROVIDER_NAME) {
56
                return $payment;
57
            }
58
        }
59
60
        return null;
61
    }
62
63
    /**
64
     * @param int $unitPrice
65
     * @param float $priceRatio
66
     * @param float $roundingError
67
     *
68
     * @return int
69
     */
70
    protected function calculateRoundedPrice(int $unitPrice, float $priceRatio, float &$roundingError): int
71
    {
72
        $priceBeforeRound = ($unitPrice * $priceRatio) + $roundingError;
73
        $priceRounded = (int)round($priceBeforeRound);
74
        $roundingError = $priceBeforeRound - $priceRounded;
75
76
        return $priceRounded;
77
    }
78
79
    /**
80
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
81
     * @param float $priceRatio
82
     * @param float $roundingError
83
     *
84
     * @return void
85
     */
86
    protected function distributeOrderItemsPrices(OrderTransfer $orderTransfer, float $priceRatio, float &$roundingError): void
87
    {
88
        foreach ($orderTransfer->getItems() as $itemTransfer) {
89
            $priceRounded = $this->calculateRoundedPrice(
90
                $itemTransfer->getSumPriceToPayAggregation(),
91
                $priceRatio,
92
                $roundingError,
93
            );
94
95
            $itemTransfer->setSumPriceToPayAggregation($priceRounded);
96
97
            $unitPrice = $priceRounded / $itemTransfer->getQuantity();
98
            $itemTransfer->setUnitPriceToPayAggregation($unitPrice);
99
            $itemTransfer->setUnitGrossPrice($unitPrice);
100
        }
101
    }
102
103
    /**
104
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
105
     * @param float $priceRatio
106
     * @param float $roundingError
107
     *
108
     * @return void
109
     */
110
    protected function distributeOrderExpensesPrices(OrderTransfer $orderTransfer, float $priceRatio, float &$roundingError): void
111
    {
112
        foreach ($orderTransfer->getExpenses() as $expenseTransfer) {
113
            $expenseTransfer->setSumGrossPrice(
114
                $this->calculateRoundedPrice(
115
                    $expenseTransfer->getSumGrossPrice(),
116
                    $priceRatio,
117
                    $roundingError,
118
                ),
119
            );
120
        }
121
    }
122
}
123