Passed
Pull Request — feature/eco-2295/master (#7)
by Aleksey
08:00 queued 04:07
created

getOrderItemAmount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 9
rs 10
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
            $orderItemsAmountToCapture = $crefoPayOmsCommandTransfer
109
                ->getRequest()
110
                ->getCaptureRequest()
111
                ->getAmount()
112
                ->getAmount();
113
114
            $crefoPayOmsCommandTransfer
115
                ->getRequest()
116
                ->getCaptureRequest()
117
                ->getAmount()
118
                ->setAmount($orderItemsAmountToCapture + $expensesAmountToCapture);
119
120
            return $crefoPayOmsCommandTransfer;
121
        }
122
123
        $captureExpenseRequestTransfer = $this->createCaptureRequestTransfer($crefoPayOmsCommandTransfer);
124
        $captureExpenseRequestTransfer->setAmount($this->createAmountTransfer($expensesAmountToCapture));
125
        $expensesRequestTransfer = (new CrefoPayApiRequestTransfer())
126
            ->setCaptureRequest($captureExpenseRequestTransfer);
127
128
        return $crefoPayOmsCommandTransfer
129
            ->setExpensesRequest($expensesRequestTransfer);
130
    }
131
132
    /**
133
     * @param int $amount
134
     *
135
     * @return \Generated\Shared\Transfer\CrefoPayApiAmountTransfer
136
     */
137
    protected function createAmountTransfer(int $amount): CrefoPayApiAmountTransfer
138
    {
139
        return (new CrefoPayApiAmountTransfer())
140
            ->setAmount($amount);
141
    }
142
143
    /**
144
     * @param \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer
145
     *
146
     * @return int
147
     */
148
    protected function getOrderItemsAmount(CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer): int
149
    {
150
        return (int)array_sum(
151
            array_map(
152
                function (PaymentCrefoPayOrderItemTransfer $paymentCrefoPayOrderItemTransfer) {
153
                    return $paymentCrefoPayOrderItemTransfer->getAmount();
154
                },
155
                $crefoPayOmsCommandTransfer
156
                    ->getPaymentCrefoPayOrderItemCollection()
157
                    ->getCrefoPayOrderItems()
158
                    ->getArrayCopy()
159
            )
160
        );
161
    }
162
163
    /**
164
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
165
     *
166
     * @return int
167
     */
168
    protected function calculateExpensesAmount(OrderTransfer $orderTransfer): int
169
    {
170
        return (int)array_sum(
171
            array_map(
172
                function (ExpenseTransfer $expense) {
173
                    return $expense->getSumPriceToPayAggregation();
174
                },
175
                $orderTransfer->getExpenses()->getArrayCopy()
176
            )
177
        );
178
    }
179
}
180