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

RefundOmsCommandRequestBuilder::getCaptureId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 1
dl 0
loc 19
rs 9.9
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\CrefoPayApiRefundRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...piRefundRequestTransfer 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\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...
15
use SprykerEco\Zed\CrefoPay\Business\Exception\InvalidItemsToRefundAggregationException;
16
use SprykerEco\Zed\CrefoPay\CrefoPayConfig;
17
18
class RefundOmsCommandRequestBuilder implements CrefoPayOmsCommandRequestBuilderInterface
19
{
20
    protected const INVALID_ITEMS_AGGREGATION_MESSAGE = 'Order items to refund have to have same captureId.';
21
22
    /**
23
     * @var \SprykerEco\Zed\CrefoPay\CrefoPayConfig
24
     */
25
    protected $config;
26
27
    /**
28
     * @param \SprykerEco\Zed\CrefoPay\CrefoPayConfig $config
29
     */
30
    public function __construct(CrefoPayConfig $config)
31
    {
32
        $this->config = $config;
33
    }
34
35
    /**
36
     * @param \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer
37
     *
38
     * @return \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer
39
     */
40
    public function buildRequestTransfer(CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer): CrefoPayOmsCommandTransfer
41
    {
42
        $requestTransfer = (new CrefoPayApiRequestTransfer())
43
            ->setRefundRequest($this->createRefundRequestTransfer($crefoPayOmsCommandTransfer));
44
45
        return $crefoPayOmsCommandTransfer
46
            ->setRequest($requestTransfer);
47
    }
48
49
    /**
50
     * @param \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer
51
     *
52
     * @return \Generated\Shared\Transfer\CrefoPayApiRefundRequestTransfer
53
     */
54
    protected function createRefundRequestTransfer(CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer): CrefoPayApiRefundRequestTransfer
55
    {
56
        return (new CrefoPayApiRefundRequestTransfer())
57
            ->setMerchantID($this->config->getMerchantId())
58
            ->setStoreID($this->config->getStoreId())
59
            ->setOrderID($crefoPayOmsCommandTransfer->getPaymentCrefoPay()->getCrefoPayOrderId())
60
            ->setCaptureID($this->getCaptureId($crefoPayOmsCommandTransfer))
61
            ->setAmount($this->createAmountTransfer($crefoPayOmsCommandTransfer))
62
            ->setRefundDescription($this->getRefundDescription($crefoPayOmsCommandTransfer));
63
    }
64
65
    /**
66
     * @param \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer
67
     *
68
     * @throws \SprykerEco\Zed\CrefoPay\Business\Exception\InvalidItemsToRefundAggregationException
69
     *
70
     * @return string
71
     */
72
    protected function getCaptureId(CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer): string
73
    {
74
        $orderItemCaptureIds = array_unique(
75
            array_map(
76
                function (PaymentCrefoPayOrderItemTransfer $paymentCrefoPayOrderItemTransfer) {
77
                    return $paymentCrefoPayOrderItemTransfer->getCaptureId();
78
                },
79
                $crefoPayOmsCommandTransfer
80
                    ->getPaymentCrefoPayOrderItemCollection()
81
                    ->getCrefoPayOrderItems()
82
                    ->getArrayCopy()
83
            )
84
        );
85
86
        if (count($orderItemCaptureIds) !== 1) {
87
            throw new InvalidItemsToRefundAggregationException(static::INVALID_ITEMS_AGGREGATION_MESSAGE);
88
        }
89
90
        return reset($orderItemCaptureIds);
91
    }
92
93
    /**
94
     * @param \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer
95
     *
96
     * @return \Generated\Shared\Transfer\CrefoPayApiAmountTransfer
97
     */
98
    protected function createAmountTransfer(CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer): CrefoPayApiAmountTransfer
99
    {
100
        return (new CrefoPayApiAmountTransfer())
101
            ->setAmount($crefoPayOmsCommandTransfer->getRefund()->getAmount());
102
    }
103
104
    /**
105
     * @param \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer
106
     *
107
     * @return string|null
108
     */
109
    protected function getRefundDescription(CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer): ?string
110
    {
111
        return $crefoPayOmsCommandTransfer->getRefund()->getComment() ?? $this->config->getRefundDescription();
112
    }
113
}
114