Completed
Push — master ( 2a18cf...56a686 )
by Oleksandr
14s queued 11s
created

updatePaymentHeidelpayWithExternalResponse()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 3
nc 2
nop 1
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\Heidelpay\Business\Payment\Transaction\Handler;
9
10
use Generated\Shared\Transfer\HeidelpayExternalPaymentResponseTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfe...PaymentResponseTransfer 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\HeidelpayPaymentProcessingResponseTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfe...cessingResponseTransfer 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\HeidelpayResponseTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfe...idelpayResponseTransfer 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 SprykerEco\Zed\Heidelpay\Business\Payment\PaymentWriterInterface;
14
use SprykerEco\Zed\Heidelpay\Business\Payment\Request\ExternalPaymentResponseBuilderInterface;
15
use SprykerEco\Zed\Heidelpay\Business\Payment\Transaction\Exception\ExternalResponseNotSupportedException;
16
use SprykerEco\Zed\Heidelpay\Business\Payment\Transaction\ExternalResponseTransactionInterface;
17
use SprykerEco\Zed\Heidelpay\Business\Payment\Type\PaymentWithExternalResponseInterface;
18
19
class ExternalResponseTransactionHandler implements ExternalResponseTransactionHandlerInterface
20
{
21
    public const ERROR_MESSAGE_EXTERNAL_RESPONSE_TRANSACTION_NOT_SUPPORTED =
22
        'Attempt to call external response transaction on payment method \'%s\' ' .
23
        'that does not support it';
24
25
    /**
26
     * @var \SprykerEco\Zed\Heidelpay\Business\Payment\Transaction\ExternalResponseTransactionInterface
27
     */
28
    protected $transaction;
29
30
    /**
31
     * @var \SprykerEco\Zed\Heidelpay\Business\Payment\Type\PaymentWithExternalResponseInterface[]
32
     */
33
    protected $paymentMethodAdapterCollection;
34
35
    /**
36
     * @var \SprykerEco\Zed\Heidelpay\Business\Payment\PaymentWriterInterface
37
     */
38
    protected $paymentWriter;
39
40
    /**
41
     * @var \SprykerEco\Zed\Heidelpay\Business\Payment\Request\ExternalPaymentResponseBuilderInterface
42
     */
43
    protected $externalPaymentResponseBuilder;
44
45
    /**
46
     * @param \SprykerEco\Zed\Heidelpay\Business\Payment\Transaction\ExternalResponseTransactionInterface $transaction
47
     * @param \SprykerEco\Zed\Heidelpay\Business\Payment\Type\PaymentWithExternalResponseInterface[] $paymentMethodAdapterCollection
48
     * @param \SprykerEco\Zed\Heidelpay\Business\Payment\Request\ExternalPaymentResponseBuilderInterface $externalPaymentResponseBuilder
49
     * @param \SprykerEco\Zed\Heidelpay\Business\Payment\PaymentWriterInterface $paymentWriter
50
     */
51
    public function __construct(
52
        ExternalResponseTransactionInterface $transaction,
53
        array $paymentMethodAdapterCollection,
54
        ExternalPaymentResponseBuilderInterface $externalPaymentResponseBuilder,
55
        PaymentWriterInterface $paymentWriter
56
    ) {
57
        $this->transaction = $transaction;
58
        $this->paymentMethodAdapterCollection = $paymentMethodAdapterCollection;
59
        $this->externalPaymentResponseBuilder = $externalPaymentResponseBuilder;
60
        $this->paymentWriter = $paymentWriter;
61
    }
62
63
    /**
64
     * @param array $externalResponseArray
65
     *
66
     * @return \Generated\Shared\Transfer\HeidelpayPaymentProcessingResponseTransfer
67
     */
68
    public function processExternalPaymentResponse(array $externalResponseArray): HeidelpayPaymentProcessingResponseTransfer
69
    {
70
        $externalResponseTransfer = $this->buildExternalResponseTransfer($externalResponseArray);
71
        $transactionResultTransfer = $this->executeTransaction($externalResponseTransfer);
72
        $this->updatePaymentHeidelpayWithExternalResponse($transactionResultTransfer);
73
74
        return $this->buildPaymentProcessingResponse($transactionResultTransfer);
75
    }
76
77
    /**
78
     * @param \Generated\Shared\Transfer\HeidelpayResponseTransfer $responseTransfer
79
     *
80
     * @return void
81
     */
82
    protected function updatePaymentHeidelpayWithExternalResponse(HeidelpayResponseTransfer $responseTransfer): void
83
    {
84
        if ($responseTransfer->getIdPaymentReference() === null
85
            && $responseTransfer->getIdTransactionUnique() === null
86
        ) {
87
            return;
88
        }
89
90
        $this->paymentWriter->updateHeidelpayPaymentWithResponse($responseTransfer);
91
    }
92
93
    /**
94
     * @param array $externalResponseArray
95
     *
96
     * @return \Generated\Shared\Transfer\HeidelpayExternalPaymentResponseTransfer
97
     */
98
    protected function buildExternalResponseTransfer(array $externalResponseArray): HeidelpayExternalPaymentResponseTransfer
99
    {
100
        $externalResponseTransfer = $this->externalPaymentResponseBuilder
101
            ->buildExternalResponseTransfer($externalResponseArray);
102
103
        return $externalResponseTransfer;
104
    }
105
106
    /**
107
     * @param \Generated\Shared\Transfer\HeidelpayExternalPaymentResponseTransfer $externalResponseTransfer
108
     *
109
     * @return \Generated\Shared\Transfer\HeidelpayResponseTransfer
110
     */
111
    protected function executeTransaction(HeidelpayExternalPaymentResponseTransfer $externalResponseTransfer): HeidelpayResponseTransfer
112
    {
113
        $paymentAdapter = $this->getPaymentMethodAdapter($externalResponseTransfer);
114
        $responseTransfer = $this->transaction->executeTransaction($externalResponseTransfer, $paymentAdapter);
115
116
        return $responseTransfer;
117
    }
118
119
    /**
120
     * @param \Generated\Shared\Transfer\HeidelpayExternalPaymentResponseTransfer $externalResponseTransfer
121
     *
122
     * @throws \SprykerEco\Zed\Heidelpay\Business\Payment\Transaction\Exception\ExternalResponseNotSupportedException
123
     *
124
     * @return \SprykerEco\Zed\Heidelpay\Business\Payment\Type\PaymentWithExternalResponseInterface
125
     */
126
    protected function getPaymentMethodAdapter(HeidelpayExternalPaymentResponseTransfer $externalResponseTransfer): PaymentWithExternalResponseInterface
127
    {
128
        $paymentMethodCode = $externalResponseTransfer->getPaymentMethod();
129
130
        if (!isset($this->paymentMethodAdapterCollection[$paymentMethodCode])) {
131
            throw new ExternalResponseNotSupportedException(
132
                sprintf(static::ERROR_MESSAGE_EXTERNAL_RESPONSE_TRANSACTION_NOT_SUPPORTED, $paymentMethodCode)
133
            );
134
        }
135
136
        return $this->paymentMethodAdapterCollection[$paymentMethodCode];
137
    }
138
139
    /**
140
     * @param \Generated\Shared\Transfer\HeidelpayResponseTransfer $transactionResultTransfer
141
     *
142
     * @return \Generated\Shared\Transfer\HeidelpayPaymentProcessingResponseTransfer
143
     */
144
    protected function buildPaymentProcessingResponse(HeidelpayResponseTransfer $transactionResultTransfer): HeidelpayPaymentProcessingResponseTransfer
145
    {
146
        return (new HeidelpayPaymentProcessingResponseTransfer())
147
            ->fromArray(
148
                $transactionResultTransfer->toArray(),
149
                true
150
            );
151
    }
152
}
153