Completed
Push — master ( eaf845...272b76 )
by Oleksandr
13s queued 10s
created

addExpensesRequest()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 4
nop 1
dl 0
loc 23
rs 9.5222
c 0
b 0
f 0
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\CrefoPay\Business\Oms\Command\Builder;
9
10
use Generated\Shared\Transfer\CrefoPayApiAmountTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...efoPayApiAmountTransfer 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\CrefoPayApiCaptureRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...iCaptureRequestTransfer 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\CrefoPayApiRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...foPayApiRequestTransfer 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\CrefoPayOmsCommandTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...foPayOmsCommandTransfer 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 Generated\Shared\Transfer\ExpenseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\ExpenseTransfer 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...
15
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...
16
use Generated\Shared\Transfer\PaymentCrefoPayOrderItemTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...efoPayOrderItemTransfer 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...
17
use SprykerEco\Zed\CrefoPay\CrefoPayConfig;
18
use SprykerEco\Zed\CrefoPay\Dependency\Service\CrefoPayToUtilTextServiceInterface;
19
20
class CaptureOmsCommandRequestBuilder implements CrefoPayOmsCommandRequestBuilderInterface
21
{
22
    /**
23
     * @var \SprykerEco\Zed\CrefoPay\Dependency\Service\CrefoPayToUtilTextServiceInterface
24
     */
25
    protected $utilTextService;
26
27
    /**
28
     * @var \SprykerEco\Zed\CrefoPay\CrefoPayConfig
29
     */
30
    protected $config;
31
32
    /**
33
     * @param \SprykerEco\Zed\CrefoPay\Dependency\Service\CrefoPayToUtilTextServiceInterface $utilTextService
34
     * @param \SprykerEco\Zed\CrefoPay\CrefoPayConfig $config
35
     */
36
    public function __construct(
37
        CrefoPayToUtilTextServiceInterface $utilTextService,
38
        CrefoPayConfig $config
39
    ) {
40
        $this->utilTextService = $utilTextService;
41
        $this->config = $config;
42
    }
43
44
    /**
45
     * @param \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer
46
     *
47
     * @return \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer
48
     */
49
    public function buildRequestTransfer(CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer): CrefoPayOmsCommandTransfer
50
    {
51
        $captureRequestTransfer = $this->createCaptureRequestTransfer($crefoPayOmsCommandTransfer);
52
        $amountToCapture = $this->getOrderItemsAmount($crefoPayOmsCommandTransfer);
53
        $captureRequestTransfer->setAmount($this->createAmountTransfer($amountToCapture));
54
        $requestTransfer = (new CrefoPayApiRequestTransfer())
55
            ->setCaptureRequest($captureRequestTransfer);
56
57
        $crefoPayOmsCommandTransfer->setRequest($requestTransfer);
58
59
        return $this->addExpensesRequest($crefoPayOmsCommandTransfer);
60
    }
61
62
    /**
63
     * @param \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer
64
     *
65
     * @return \Generated\Shared\Transfer\CrefoPayApiCaptureRequestTransfer
66
     */
67
    protected function createCaptureRequestTransfer(CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer): CrefoPayApiCaptureRequestTransfer
68
    {
69
        return (new CrefoPayApiCaptureRequestTransfer())
70
            ->setMerchantID($this->config->getMerchantId())
71
            ->setStoreID($this->config->getStoreId())
72
            ->setOrderID($crefoPayOmsCommandTransfer->getPaymentCrefoPay()->getCrefoPayOrderId())
73
            ->setCaptureID(
74
                $this->utilTextService->generateRandomString(
75
                    $this->config->getCrefoPayApiCaptureIdLength()
76
                )
77
            );
78
    }
79
80
    /**
81
     * @param \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer
82
     *
83
     * @return bool
84
     */
85
    protected function isFirstCapture(CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer): bool
86
    {
87
        return $crefoPayOmsCommandTransfer->getPaymentCrefoPay()->getCapturedAmount() === 0;
88
    }
89
90
    /**
91
     * @param \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer
92
     *
93
     * @return \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer
94
     */
95
    protected function addExpensesRequest(CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer): CrefoPayOmsCommandTransfer
96
    {
97
        if ($this->config->getCaptureExpensesSeparately() && !$this->isFirstCapture($crefoPayOmsCommandTransfer)) {
98
            return $crefoPayOmsCommandTransfer;
99
        }
100
101
        $expensesAmountToCapture = $this->calculateExpensesAmount($crefoPayOmsCommandTransfer->getOrder());
102
103
        if ($expensesAmountToCapture <= 0) {
104
            return $crefoPayOmsCommandTransfer;
105
        }
106
107
        if (!$this->config->getCaptureExpensesSeparately()) {
108
            return $this->addExpensesAmountToCaptureRequest($crefoPayOmsCommandTransfer, $expensesAmountToCapture);
109
        }
110
111
        $captureExpenseRequestTransfer = $this->createCaptureRequestTransfer($crefoPayOmsCommandTransfer);
112
        $captureExpenseRequestTransfer->setAmount($this->createAmountTransfer($expensesAmountToCapture));
113
        $expensesRequestTransfer = (new CrefoPayApiRequestTransfer())
114
            ->setCaptureRequest($captureExpenseRequestTransfer);
115
116
        return $crefoPayOmsCommandTransfer
117
            ->setExpensesRequest($expensesRequestTransfer);
118
    }
119
120
    /**
121
     * @param int $amount
122
     *
123
     * @return \Generated\Shared\Transfer\CrefoPayApiAmountTransfer
124
     */
125
    protected function createAmountTransfer(int $amount): CrefoPayApiAmountTransfer
126
    {
127
        return (new CrefoPayApiAmountTransfer())
128
            ->setAmount($amount);
129
    }
130
131
    /**
132
     * @param \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer
133
     *
134
     * @return int
135
     */
136
    protected function getOrderItemsAmount(CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer): int
137
    {
138
        return (int)array_sum(
139
            array_map(
140
                function (PaymentCrefoPayOrderItemTransfer $paymentCrefoPayOrderItemTransfer) {
141
                    return $paymentCrefoPayOrderItemTransfer->getAmount();
142
                },
143
                $crefoPayOmsCommandTransfer
144
                    ->getPaymentCrefoPayOrderItemCollection()
145
                    ->getCrefoPayOrderItems()
146
                    ->getArrayCopy()
147
            )
148
        );
149
    }
150
151
    /**
152
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
153
     *
154
     * @return int
155
     */
156
    protected function calculateExpensesAmount(OrderTransfer $orderTransfer): int
157
    {
158
        return (int)array_sum(
159
            array_map(
160
                function (ExpenseTransfer $expense) {
161
                    return $expense->getSumPriceToPayAggregation();
162
                },
163
                $orderTransfer->getExpenses()->getArrayCopy()
164
            )
165
        );
166
    }
167
168
    /**
169
     * @param \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer
170
     * @param int $expensesAmountToCapture
171
     *
172
     * @return \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer
173
     */
174
    protected function addExpensesAmountToCaptureRequest(
175
        CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer,
176
        int $expensesAmountToCapture
177
    ): CrefoPayOmsCommandTransfer {
178
        $orderItemsAmountToCapture = $crefoPayOmsCommandTransfer
179
            ->getRequest()
180
            ->getCaptureRequest()
181
            ->getAmount()
182
            ->getAmount();
183
184
        $crefoPayOmsCommandTransfer
185
            ->getRequest()
186
            ->getCaptureRequest()
187
            ->getAmount()
188
            ->setAmount($orderItemsAmountToCapture + $expensesAmountToCapture);
189
190
        return $crefoPayOmsCommandTransfer;
191
    }
192
}
193