Passed
Push — feature/eco-3047/eco-3049-invo... ( 1d76dc...51d32b )
by Aleksey
10:05
created

ResponseFromHeidelpay::getConnectorInfo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 2
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\Adapter\Mapper;
9
10
use ArrayObject;
11
use Generated\Shared\Transfer\HeidelpayBankCountryTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfe...lpayBankCountryTransfer 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\HeidelpayBankTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfer\HeidelpayBankTransfer 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\HeidelpayResponseConfigTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfe...yResponseConfigTransfer 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\HeidelpayResponseErrorTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfe...ayResponseErrorTransfer 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\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...
16
use Heidelpay\PhpPaymentApi\Exceptions\PaymentFormUrlException;
17
use Heidelpay\PhpPaymentApi\ParameterGroups\ConfigParameterGroup;
18
use Heidelpay\PhpPaymentApi\Response;
19
use SprykerEco\Zed\Heidelpay\Dependency\Service\HeidelpayToUtilEncodingServiceInterface;
20
21
class ResponseFromHeidelpay implements ResponseFromHeidelpayInterface
22
{
23
    public const RESPONSE_PARAMETER_GROUP_PROCESSING = 'processing';
24
    public const RESPONSE_PARAMETER_GROUP_ACCOUNT = 'account';
25
    public const RESPONSE_PARAMETER_GROUP_ADDRESS = 'address';
26
    public const RESPONSE_PARAMETER_GROUP_CONFIG = 'config';
27
    public const RESPONSE_PARAMETER_GROUP_CONTACT = 'contact';
28
    public const RESPONSE_PARAMETER_GROUP_CRITERION = 'criterion';
29
    public const RESPONSE_PARAMETER_GROUP_FRONTEND = 'frontend';
30
    public const RESPONSE_PARAMETER_GROUP_IDENTIFICATION = 'identification';
31
    public const RESPONSE_PARAMETER_GROUP_NAME = 'name';
32
    public const RESPONSE_PARAMETER_GROUP_PAYMENT = 'payment';
33
    public const RESPONSE_PARAMETER_GROUP_PRESENTATION = 'presentation';
34
    public const RESPONSE_PARAMETER_GROUP_REQUEST = 'request';
35
    public const RESPONSE_PARAMETER_GROUP_SECURITY = 'security';
36
    public const RESPONSE_PARAMETER_GROUP_TRANSACTION = 'transaction';
37
    public const RESPONSE_PARAMETER_GROUP_USER = 'user';
38
39
    /**
40
     * @var \SprykerEco\Zed\Heidelpay\Dependency\Service\HeidelpayToUtilEncodingServiceInterface
41
     */
42
    protected $utilEncoding;
43
44
    /**
45
     * @param \SprykerEco\Zed\Heidelpay\Dependency\Service\HeidelpayToUtilEncodingServiceInterface $utilEncoding
46
     */
47
    public function __construct(HeidelpayToUtilEncodingServiceInterface $utilEncoding)
48
    {
49
        $this->utilEncoding = $utilEncoding;
50
    }
51
52
    /**
53
     * @param \Heidelpay\PhpPaymentApi\Response $apiResponse
54
     * @param \Generated\Shared\Transfer\HeidelpayResponseTransfer $responseTransfer
55
     *
56
     * @return void
57
     */
58
    public function map(Response $apiResponse, HeidelpayResponseTransfer $responseTransfer): void
59
    {
60
        $responseTransfer->setIsPending($apiResponse->isPending())
61
            ->setIsSuccess($apiResponse->isSuccess())
62
            ->setIdSalesOrder((int)$apiResponse->getIdentification()->getTransactionId())
63
            ->setIsError($apiResponse->isError())
64
            ->setProcessingCode($apiResponse->getProcessing()->code)
65
            ->setIdTransactionUnique($apiResponse->getIdentification()->getUniqueId())
66
            ->setResultCode($apiResponse->getProcessing()->getResult())
67
            ->setLegalText($apiResponse->getConfig()->getOptinText())
68
            ->setConnectorInvoiceAccountInfo($this->getConnectorInfo($apiResponse));
69
70
        $this->mapPaymentFormUrl($apiResponse, $responseTransfer);
71
        $this->mapError($apiResponse, $responseTransfer);
72
        $this->mapConfig($apiResponse, $responseTransfer);
73
74
        $this->addResponseFullPayload($apiResponse, $responseTransfer);
75
    }
76
77
    /**
78
     * @param \Heidelpay\PhpPaymentApi\Response $apiResponse
79
     * @param \Generated\Shared\Transfer\HeidelpayResponseTransfer $responseTransfer
80
     *
81
     * @return void
82
     */
83
    protected function mapError(Response $apiResponse, HeidelpayResponseTransfer $responseTransfer): void
84
    {
85
        if (!$apiResponse->isError()) {
86
            return;
87
        }
88
89
        $errorResponse = $apiResponse->getError();
90
        $errorTransfer = (new HeidelpayResponseErrorTransfer())
91
            ->setCode($errorResponse['code'])
92
            ->setInternalMessage($errorResponse['message']);
93
94
        $responseTransfer
95
            ->setIsError(true)
96
            ->setError($errorTransfer);
97
    }
98
99
    /**
100
     * @param \Heidelpay\PhpPaymentApi\Response $apiResponse
101
     * @param \Generated\Shared\Transfer\HeidelpayResponseTransfer $responseTransfer
102
     *
103
     * @return void
104
     */
105
    protected function mapConfig(Response $apiResponse, HeidelpayResponseTransfer $responseTransfer): void
106
    {
107
        $responseConfig = $apiResponse->getConfig();
108
        $configTransfer = new HeidelpayResponseConfigTransfer();
109
110
        $this->mapBanks($responseConfig, $configTransfer);
111
        $this->mapBankCountries($responseConfig, $configTransfer);
112
113
        $responseTransfer->setConfig($configTransfer);
114
    }
115
116
    /**
117
     * @param \Heidelpay\PhpPaymentApi\ParameterGroups\ConfigParameterGroup $config
118
     * @param \Generated\Shared\Transfer\HeidelpayResponseConfigTransfer $configTransfer
119
     *
120
     * @return void
121
     */
122
    protected function mapBanks(ConfigParameterGroup $config, HeidelpayResponseConfigTransfer $configTransfer): void
123
    {
124
        /** @var string[] $banks */
125
        $banks = $config->getBrands();
126
127
        if (empty($banks)) {
128
            return;
129
        }
130
131
        $bankTransfersList = [];
132
        foreach ($banks as $code => $name) {
133
            $bankTransfersList[] = (new HeidelpayBankTransfer())
134
                ->setCode($code)
135
                ->setName($name);
136
        }
137
138
        $configTransfer->setBankBrands(new ArrayObject($bankTransfersList));
139
    }
140
141
    /**
142
     * @param \Heidelpay\PhpPaymentApi\ParameterGroups\ConfigParameterGroup $config
143
     * @param \Generated\Shared\Transfer\HeidelpayResponseConfigTransfer $configTransfer
144
     *
145
     * @return void
146
     */
147
    protected function mapBankCountries(ConfigParameterGroup $config, HeidelpayResponseConfigTransfer $configTransfer): void
148
    {
149
        /** @var string[] $bankCountries */
150
        $bankCountries = $config->getBankCountry();
151
152
        if (empty($bankCountries)) {
153
            return;
154
        }
155
156
        $countryTransfersList = [];
157
        foreach ($bankCountries as $code => $name) {
158
            $countryTransfersList[] = (new HeidelpayBankCountryTransfer())
159
                ->setCode($code)
160
                ->setName($name);
161
        }
162
163
        $configTransfer->setBankCountries(new ArrayObject($countryTransfersList));
164
    }
165
166
    /**
167
     * @param \Heidelpay\PhpPaymentApi\Response $apiResponse
168
     * @param \Generated\Shared\Transfer\HeidelpayResponseTransfer $responseTransfer
169
     *
170
     * @return void
171
     */
172
    protected function addResponseFullPayload(Response $apiResponse, HeidelpayResponseTransfer $responseTransfer): void
173
    {
174
        $responseTransfer->setPayload(
175
            $this->getJsonEncodedPayloadFromApiResponse($apiResponse)
176
        );
177
    }
178
179
    /**
180
     * @param \Heidelpay\PhpPaymentApi\Response $apiResponse
181
     *
182
     * @return string
183
     */
184
    protected function getJsonEncodedPayloadFromApiResponse(Response $apiResponse): string
185
    {
186
        $payload = [
187
            static::RESPONSE_PARAMETER_GROUP_PROCESSING => get_object_vars($apiResponse->getProcessing()),
188
            static::RESPONSE_PARAMETER_GROUP_ACCOUNT => get_object_vars($apiResponse->getAccount()),
189
            static::RESPONSE_PARAMETER_GROUP_ADDRESS => get_object_vars($apiResponse->getAddress()),
190
            static::RESPONSE_PARAMETER_GROUP_CONFIG => get_object_vars($apiResponse->getConfig()),
191
            static::RESPONSE_PARAMETER_GROUP_CONTACT => get_object_vars($apiResponse->getContact()),
192
            static::RESPONSE_PARAMETER_GROUP_CRITERION => get_object_vars($apiResponse->getCriterion()),
193
            static::RESPONSE_PARAMETER_GROUP_FRONTEND => get_object_vars($apiResponse->getFrontend()),
194
            static::RESPONSE_PARAMETER_GROUP_IDENTIFICATION => get_object_vars($apiResponse->getIdentification()),
195
            static::RESPONSE_PARAMETER_GROUP_NAME => get_object_vars($apiResponse->getName()),
196
            static::RESPONSE_PARAMETER_GROUP_PAYMENT => get_object_vars($apiResponse->getPayment()),
197
            static::RESPONSE_PARAMETER_GROUP_PRESENTATION => get_object_vars($apiResponse->getPresentation()),
198
            static::RESPONSE_PARAMETER_GROUP_REQUEST => get_object_vars($apiResponse->getRequest()),
199
            static::RESPONSE_PARAMETER_GROUP_SECURITY => get_object_vars($apiResponse->getSecurity()),
200
            static::RESPONSE_PARAMETER_GROUP_TRANSACTION => get_object_vars($apiResponse->getTransaction()),
201
            static::RESPONSE_PARAMETER_GROUP_USER => get_object_vars($apiResponse->getUser()),
202
        ];
203
204
        return $this->utilEncoding->encodeJson($payload);
205
    }
206
207
    /**
208
     * @param \Heidelpay\PhpPaymentApi\Response $apiResponse
209
     * @param \Generated\Shared\Transfer\HeidelpayResponseTransfer $responseTransfer
210
     *
211
     * @return void
212
     */
213
    protected function mapPaymentFormUrl(
214
        Response $apiResponse,
215
        HeidelpayResponseTransfer $responseTransfer
216
    ): void {
217
        try {
218
            /** @var string $paymentFormUrl */
219
            $paymentFormUrl = $apiResponse->getPaymentFormUrl();
220
            $responseTransfer->setPaymentFormUrl($paymentFormUrl);
221
        } catch (PaymentFormUrlException $exception) {
222
            $responseTransfer->setPaymentFormUrl(null);
223
        }
224
    }
225
226
    /**
227
     * @param \Heidelpay\PhpPaymentApi\Response $apiResponse
228
     *
229
     * @return string|null
230
     */
231
    protected function getConnectorInfo(Response $apiResponse): ?string
232
    {
233
        if ($apiResponse->getConnector()->getAccountIBan() === null) {
0 ignored issues
show
introduced by
The condition $apiResponse->getConnect...tAccountIBan() === null is always false.
Loading history...
234
            return null;
235
        }
236
237
        return $apiResponse->getConnector()->toJson();
238
    }
239
}
240