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

getOrderItemsAmount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 11
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
        if ($this->isFirstCapture($crefoPayOmsCommandTransfer)) {
60
            $crefoPayOmsCommandTransfer = $this->addExpensesRequest($crefoPayOmsCommandTransfer);
61
        }
62
63
        return $crefoPayOmsCommandTransfer;
64
    }
65
66
    /**
67
     * @param \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer
68
     *
69
     * @return \Generated\Shared\Transfer\CrefoPayApiCaptureRequestTransfer
70
     */
71
    protected function createCaptureRequestTransfer(CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer): CrefoPayApiCaptureRequestTransfer
72
    {
73
        return (new CrefoPayApiCaptureRequestTransfer())
74
            ->setMerchantID($this->config->getMerchantId())
75
            ->setStoreID($this->config->getStoreId())
76
            ->setOrderID($crefoPayOmsCommandTransfer->getPaymentCrefoPay()->getCrefoPayOrderId())
77
            ->setCaptureID(
78
                $this->utilTextService->generateRandomString(
79
                    $this->config->getCrefoPayApiCaptureIdLength()
80
                )
81
            );
82
    }
83
84
    /**
85
     * @param \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer
86
     *
87
     * @return bool
88
     */
89
    protected function isFirstCapture(CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer): bool
90
    {
91
        return $crefoPayOmsCommandTransfer->getPaymentCrefoPay()->getCapturedAmount() === 0;
92
    }
93
94
    /**
95
     * @param \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer
96
     *
97
     * @return \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer
98
     */
99
    protected function addExpensesRequest(CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer): CrefoPayOmsCommandTransfer
100
    {
101
        $expensesAmountToCapture = $this->calculateExpensesAmount($crefoPayOmsCommandTransfer->getOrder());
102
103
        if ($expensesAmountToCapture <= 0) {
104
            return $crefoPayOmsCommandTransfer;
105
        }
106
107
        $captureExpenseRequestTransfer = $this->createCaptureRequestTransfer($crefoPayOmsCommandTransfer);
108
        $captureExpenseRequestTransfer->setAmount($this->createAmountTransfer($expensesAmountToCapture));
109
        $expensesRequestTransfer = (new CrefoPayApiRequestTransfer())
110
            ->setCaptureRequest($captureExpenseRequestTransfer);
111
112
        return $crefoPayOmsCommandTransfer
113
            ->setExpensesRequest($expensesRequestTransfer);
114
    }
115
116
    /**
117
     * @param int $amount
118
     *
119
     * @return \Generated\Shared\Transfer\CrefoPayApiAmountTransfer
120
     */
121
    protected function createAmountTransfer(int $amount): CrefoPayApiAmountTransfer
122
    {
123
        return (new CrefoPayApiAmountTransfer())
124
            ->setAmount($amount);
125
    }
126
127
    /**
128
     * @param \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer
129
     *
130
     * @return int
131
     */
132
    protected function getOrderItemsAmount(CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer): int
133
    {
134
        return (int)array_sum(
135
            array_map(
136
                function (PaymentCrefoPayOrderItemTransfer $paymentCrefoPayOrderItemTransfer) {
137
                    return $paymentCrefoPayOrderItemTransfer->getAmount();
138
                },
139
                $crefoPayOmsCommandTransfer
140
                    ->getPaymentCrefoPayOrderItemCollection()
141
                    ->getCrefoPayOrderItems()
142
                    ->getArrayCopy()
143
            )
144
        );
145
    }
146
147
    /**
148
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
149
     *
150
     * @return int
151
     */
152
    protected function calculateExpensesAmount(OrderTransfer $orderTransfer): int
153
    {
154
        return (int)array_sum(
155
            array_map(
156
                function (ExpenseTransfer $expense) {
157
                    return $expense->getSumPriceToPayAggregation();
158
                },
159
                $orderTransfer->getExpenses()->getArrayCopy()
160
            )
161
        );
162
    }
163
}
164