Passed
Pull Request — master (#67)
by
unknown
09:59
created

PayoneRefundRequestSender::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 17
rs 10
cc 1
nc 1
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/**
4
 * MIT License
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\Payone\Business\Payment\RequestSender;
9
10
use Generated\Shared\Transfer\PayoneRefundTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\PayoneRefundTransfer 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\PayoneStandardParameterTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...andardParameterTransfer 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\RefundResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\RefundResponseTransfer 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 Orm\Zed\Payone\Persistence\SpyPaymentPayoneApiLog;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Payone\Persistence\SpyPaymentPayoneApiLog 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\Zed\Payone\Business\Api\Adapter\AdapterInterface;
15
use SprykerEco\Zed\Payone\Business\Api\Response\Container\RefundResponseContainer;
16
use SprykerEco\Zed\Payone\Business\Api\Response\Mapper\RefundResponseMapperInterface;
17
use SprykerEco\Zed\Payone\Business\Distributor\OrderPriceDistributorInterface;
18
use SprykerEco\Zed\Payone\Business\Payment\DataMapper\PayoneRequestProductDataMapperInterface;
19
use SprykerEco\Zed\Payone\Business\Payment\DataMapper\StandartParameterMapperInterface;
20
use SprykerEco\Zed\Payone\Business\Payment\PaymentMapperReaderInterface;
21
use SprykerEco\Zed\Payone\Persistence\PayoneQueryContainerInterface;
22
23
class PayoneRefundRequestSender extends AbstractPayoneRequestSender implements PayoneRefundRequestSenderInterface
24
{
25
    /**
26
     * @var \SprykerEco\Zed\Payone\Business\Api\Adapter\AdapterInterface
27
     */
28
    protected $executionAdapter;
29
30
    /**
31
     * @var \Generated\Shared\Transfer\PayoneStandardParameterTransfer
32
     */
33
    protected $standardParameter;
34
35
    /**
36
     * @var \SprykerEco\Zed\Payone\Business\Distributor\OrderPriceDistributorInterface
37
     */
38
    protected $orderPriceDistributor;
39
40
    /**
41
     * @var \SprykerEco\Zed\Payone\Business\Payment\DataMapper\StandartParameterMapperInterface
42
     */
43
    protected $standartParameterMapper;
44
45
    /**
46
     * @var \SprykerEco\Zed\Payone\Business\Payment\DataMapper\PayoneRequestProductDataMapperInterface
47
     */
48
    protected $payoneRequestProductDataMapper;
49
50
    /**
51
     * @var \SprykerEco\Zed\Payone\Business\Api\Response\Mapper\RefundResponseMapperInterface
52
     */
53
    protected $refundResponseMapper;
54
55
    /**
56
     * @param \SprykerEco\Zed\Payone\Business\Api\Adapter\AdapterInterface $executionAdapter
57
     * @param \SprykerEco\Zed\Payone\Persistence\PayoneQueryContainerInterface $queryContainer
58
     * @param \SprykerEco\Zed\Payone\Business\Payment\PaymentMapperReaderInterface $paymentMapperReader
59
     * @param \Generated\Shared\Transfer\PayoneStandardParameterTransfer $standardParameter
60
     * @param \SprykerEco\Zed\Payone\Business\Distributor\OrderPriceDistributorInterface $orderPriceDistributor
61
     * @param \SprykerEco\Zed\Payone\Business\Payment\DataMapper\StandartParameterMapperInterface $standartParameterMapper
62
     * @param \SprykerEco\Zed\Payone\Business\Payment\DataMapper\PayoneRequestProductDataMapperInterface $payoneRequestProductDataMapper
63
     * @param \SprykerEco\Zed\Payone\Business\Api\Response\Mapper\RefundResponseMapperInterface $refundResponseMapper
64
     */
65
    public function __construct(
66
        AdapterInterface $executionAdapter,
67
        PayoneQueryContainerInterface $queryContainer,
68
        PaymentMapperReaderInterface $paymentMapperReader,
69
        PayoneStandardParameterTransfer $standardParameter,
70
        OrderPriceDistributorInterface $orderPriceDistributor,
71
        StandartParameterMapperInterface $standartParameterMapper,
72
        PayoneRequestProductDataMapperInterface $payoneRequestProductDataMapper,
73
        RefundResponseMapperInterface $refundResponseMapper
74
    ) {
75
        parent::__construct($queryContainer, $paymentMapperReader);
76
        $this->executionAdapter = $executionAdapter;
77
        $this->standardParameter = $standardParameter;
78
        $this->orderPriceDistributor = $orderPriceDistributor;
79
        $this->standartParameterMapper = $standartParameterMapper;
80
        $this->payoneRequestProductDataMapper = $payoneRequestProductDataMapper;
81
        $this->refundResponseMapper = $refundResponseMapper;
82
    }
83
84
    /**
85
     * @param \Generated\Shared\Transfer\PayoneRefundTransfer $refundTransfer
86
     *
87
     * @return \Generated\Shared\Transfer\RefundResponseTransfer
88
     */
89
    public function refundPayment(PayoneRefundTransfer $refundTransfer): RefundResponseTransfer
90
    {
91
        $distributedPriceOrderTransfer = $this->orderPriceDistributor->distributeOrderPrice(
92
            $refundTransfer->getOrder()
93
        );
94
        $refundTransfer->setOrder($distributedPriceOrderTransfer);
95
96
        $payonePaymentTransfer = $refundTransfer->getPayment();
97
98
        $paymentEntity = $this->getPaymentEntity($payonePaymentTransfer->getFkSalesOrder());
99
        $paymentMethodMapper = $this->getPaymentMethodMapper($paymentEntity);
100
        $requestContainer = $paymentMethodMapper->mapPaymentToRefund($paymentEntity);
101
        $requestContainer->setAmount(0 - $paymentEntity->getSpyPaymentPayoneDetail()->getAmount());
102
        $requestContainer = $this->payoneRequestProductDataMapper->mapProductData($refundTransfer->getOrder(), $requestContainer);
103
104
        $this->standartParameterMapper->setStandardParameter($requestContainer, $this->standardParameter);
105
106
        $apiLogEntity = $this->initializeApiLog($paymentEntity, $requestContainer);
107
108
        $rawResponse = $this->executionAdapter->sendRequest($requestContainer);
109
        $responseContainer = new RefundResponseContainer($rawResponse);
110
111
        $this->updateApiLogAfterRefund($apiLogEntity, $responseContainer);
112
113
        $responseTransfer = $this->refundResponseMapper->getRefundResponseTransfer($responseContainer);
114
115
        return $responseTransfer;
116
    }
117
118
    /**
119
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayoneApiLog $apiLogEntity
120
     * @param \SprykerEco\Zed\Payone\Business\Api\Response\Container\RefundResponseContainer $responseContainer
121
     *
122
     * @return void
123
     */
124
    protected function updateApiLogAfterRefund(SpyPaymentPayoneApiLog $apiLogEntity, RefundResponseContainer $responseContainer): void
125
    {
126
        $apiLogEntity->setTransactionId($responseContainer->getTxid());
127
        $apiLogEntity->setStatus($responseContainer->getStatus());
128
        $apiLogEntity->setErrorMessageInternal($responseContainer->getErrormessage());
129
        $apiLogEntity->setErrorMessageUser($responseContainer->getCustomermessage());
130
        $apiLogEntity->setErrorCode($responseContainer->getErrorcode());
131
132
        $apiLogEntity->setRawResponse(json_encode($responseContainer->toArray()));
133
        $apiLogEntity->save();
134
    }
135
}
136