CaptureRequestBuilder   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 37
dl 0
loc 150
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A intToDecimalString() 0 3 1
A increaseTotalGrossAmount() 0 10 1
A __construct() 0 6 1
A buildExpenseItemTransfer() 0 10 1
A buildBaseCaptureRequestForOrder() 0 6 1
A addOrderItemToCaptureRequest() 0 11 1
A decimalToInt() 0 3 1
A increaseTotalNetAmount() 0 10 1
A addOrderExpenseToCaptureRequest() 0 8 1
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Zed\AfterPay\Business\Payment\Transaction\Capture;
9
10
use Generated\Shared\Transfer\AfterPayCallTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\AfterPayCallTransfer 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\AfterPayCaptureRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...yCaptureRequestTransfer 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
use Generated\Shared\Transfer\AfterPayRequestOrderItemTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...equestOrderItemTransfer 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...
13
use Generated\Shared\Transfer\ItemTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\ItemTransfer 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...
14
use SprykerEco\Shared\AfterPay\AfterPayConfig;
15
use SprykerEco\Zed\AfterPay\Business\Payment\Mapper\OrderToRequestTransferInterface;
16
use SprykerEco\Zed\AfterPay\Dependency\Facade\AfterPayToMoneyFacadeInterface;
17
18
class CaptureRequestBuilder implements CaptureRequestBuilderInterface
19
{
20
    /**
21
     * @var int
22
     */
23
    protected const ZERO_AMOUNT = 0;
24
25
    /**
26
     * @var \SprykerEco\Zed\AfterPay\Business\Payment\Mapper\OrderToRequestTransferInterface
27
     */
28
    protected $orderToRequestMapper;
29
30
    /**
31
     * @var \SprykerEco\Zed\AfterPay\Dependency\Facade\AfterPayToMoneyFacadeInterface
32
     */
33
    protected $money;
34
35
    /**
36
     * @param \SprykerEco\Zed\AfterPay\Business\Payment\Mapper\OrderToRequestTransferInterface $orderToRequestMapper
37
     * @param \SprykerEco\Zed\AfterPay\Dependency\Facade\AfterPayToMoneyFacadeInterface $money
38
     */
39
    public function __construct(
40
        OrderToRequestTransferInterface $orderToRequestMapper,
41
        AfterPayToMoneyFacadeInterface $money
42
    ) {
43
        $this->orderToRequestMapper = $orderToRequestMapper;
44
        $this->money = $money;
45
    }
46
47
    /**
48
     * @param \Generated\Shared\Transfer\AfterPayCallTransfer $afterPayCallTransfer
49
     *
50
     * @return \Generated\Shared\Transfer\AfterPayCaptureRequestTransfer
51
     */
52
    public function buildBaseCaptureRequestForOrder(AfterPayCallTransfer $afterPayCallTransfer): AfterPayCaptureRequestTransfer
53
    {
54
        $captureRequestTransfer = $this->orderToRequestMapper
55
            ->orderToBaseCaptureRequest($afterPayCallTransfer);
56
57
        return $captureRequestTransfer;
58
    }
59
60
    /**
61
     * @param \Generated\Shared\Transfer\ItemTransfer $orderItemTransfer
62
     * @param \Generated\Shared\Transfer\AfterPayCaptureRequestTransfer $captureRequestTransfer
63
     *
64
     * @return $this
65
     */
66
    public function addOrderItemToCaptureRequest(
67
        ItemTransfer $orderItemTransfer,
68
        AfterPayCaptureRequestTransfer $captureRequestTransfer
69
    ) {
70
        $orderItemRequestTransfer = $this->orderToRequestMapper->orderItemToAfterPayItemRequest($orderItemTransfer);
71
72
        $captureRequestTransfer->getOrderDetails()->addItem($orderItemRequestTransfer);
73
        $this->increaseTotalGrossAmount($orderItemRequestTransfer, $captureRequestTransfer);
74
        $this->increaseTotalNetAmount($orderItemRequestTransfer, $captureRequestTransfer);
75
76
        return $this;
77
    }
78
79
    /**
80
     * @param int $expenseAmount
81
     * @param \Generated\Shared\Transfer\AfterPayCaptureRequestTransfer $captureRequestTransfer
82
     *
83
     * @return $this
84
     */
85
    public function addOrderExpenseToCaptureRequest(
86
        int $expenseAmount,
87
        AfterPayCaptureRequestTransfer $captureRequestTransfer
88
    ) {
89
        $expenseItemRequestTransfer = $this->buildExpenseItemTransfer($expenseAmount);
90
        $this->addOrderItemToCaptureRequest($expenseItemRequestTransfer, $captureRequestTransfer);
91
92
        return $this;
93
    }
94
95
    /**
96
     * @param int $expenseAmount
97
     *
98
     * @return \Generated\Shared\Transfer\ItemTransfer
99
     */
100
    protected function buildExpenseItemTransfer(int $expenseAmount): ItemTransfer
101
    {
102
        return (new ItemTransfer())
103
            ->setSku(AfterPayConfig::CAPTURE_EXPENSE_SKU)
104
            ->setName(AfterPayConfig::CAPTURE_EXPENSE_DESCRIPTION)
105
            ->setUnitGrossPrice($expenseAmount)
106
            ->setUnitPriceToPayAggregation($expenseAmount)
107
            ->setUnitTaxAmountFullAggregation(static::ZERO_AMOUNT)
108
            ->setTaxRate(static::ZERO_AMOUNT)
109
            ->setQuantity(1);
110
    }
111
112
    /**
113
     * @param \Generated\Shared\Transfer\AfterPayRequestOrderItemTransfer $orderItemRequestTransfer
114
     * @param \Generated\Shared\Transfer\AfterPayCaptureRequestTransfer $captureRequestTransfer
115
     *
116
     * @return void
117
     */
118
    protected function increaseTotalNetAmount(
119
        AfterPayRequestOrderItemTransfer $orderItemRequestTransfer,
120
        AfterPayCaptureRequestTransfer $captureRequestTransfer
121
    ): void {
122
        $oldNetAmountDecimal = $this->decimalToInt((float)$captureRequestTransfer->getOrderDetails()->getTotalNetAmount());
123
        $itemNetAmountDecimal = $this->decimalToInt((float)$orderItemRequestTransfer->getNetUnitPrice());
124
125
        $newNetAmountDecimal = $oldNetAmountDecimal + $itemNetAmountDecimal;
126
        $captureRequestTransfer->getOrderDetails()->setTotalNetAmount(
127
            $this->intToDecimalString($newNetAmountDecimal),
128
        );
129
    }
130
131
    /**
132
     * @param \Generated\Shared\Transfer\AfterPayRequestOrderItemTransfer $orderItemRequestTransfer
133
     * @param \Generated\Shared\Transfer\AfterPayCaptureRequestTransfer $captureRequestTransfer
134
     *
135
     * @return void
136
     */
137
    protected function increaseTotalGrossAmount(
138
        AfterPayRequestOrderItemTransfer $orderItemRequestTransfer,
139
        AfterPayCaptureRequestTransfer $captureRequestTransfer
140
    ): void {
141
        $oldGrossAmountDecimal = $this->decimalToInt((float)$captureRequestTransfer->getOrderDetails()->getTotalGrossAmount());
142
        $itemGrossAmountDecimal = $this->decimalToInt((float)$orderItemRequestTransfer->getGrossUnitPrice());
143
144
        $newGrossAmountDecimal = $oldGrossAmountDecimal + $itemGrossAmountDecimal;
145
        $captureRequestTransfer->getOrderDetails()->setTotalGrossAmount(
146
            $this->intToDecimalString($newGrossAmountDecimal),
147
        );
148
    }
149
150
    /**
151
     * @param float $decimalValue
152
     *
153
     * @return int
154
     */
155
    protected function decimalToInt(float $decimalValue): int
156
    {
157
        return $this->money->convertDecimalToInteger($decimalValue);
158
    }
159
160
    /**
161
     * @param int $intValue
162
     *
163
     * @return string
164
     */
165
    protected function intToDecimalString(int $intValue): string
166
    {
167
        return (string)$this->money->convertIntegerToDecimal($intValue);
168
    }
169
}
170