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

NotificationExpander   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 256
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
eloc 97
dl 0
loc 256
c 1
b 0
f 0
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A addCustomerData() 0 13 2
A addPaymentData() 0 18 2
A __construct() 0 10 1
A addTransactionData() 0 16 2
A expandWithNotificationData() 0 14 1
A addProcessingData() 0 18 2
A convertAmountToInt() 0 3 1
A addConnectorAccountData() 0 15 3
A addIdentificationData() 0 16 2
A executeExpanderPlugins() 0 9 2
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\Processor\Notification\Expander;
9
10
use Generated\Shared\Transfer\HeidelpayNotificationTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfe...payNotificationTransfer 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 Spryker\Shared\Money\Dependency\Plugin\MoneyPluginInterface;
12
use SprykerEco\Zed\Heidelpay\Business\Processor\Notification\Converter\NotificationXmlConverterInterface;
13
use SprykerEco\Zed\Heidelpay\Dependency\Service\HeidelpayToUtilEncodingServiceInterface;
14
15
class NotificationExpander implements NotificationExpanderInterface
16
{
17
    protected const KEY_ATTRIBUTES = '@attributes';
18
    protected const KEY_IDENTIFICATION = 'Identification';
19
    protected const KEY_PROCESSING = 'Processing';
20
    protected const KEY_PAYMENT = 'Payment';
21
    protected const KEY_CLEARING = 'Clearing';
22
    protected const KEY_ACCOUNT = 'Account';
23
    protected const KEY_CONNECTOR = 'Connector';
24
    protected const KEY_CUSTOMER = 'Customer';
25
    protected const KEY_TRANSACTION_SOURCE = 'source';
26
    protected const KEY_CHANNEL = 'channel';
27
    protected const KEY_RESPONSE = 'response';
28
    protected const KEY_MODE = 'mode';
29
    protected const KEY_TRANSACTION_ID = 'TransactionID';
30
    protected const KEY_UNIQUE_ID = 'UniqueID';
31
    protected const KEY_SHORT_ID = 'ShortID';
32
    protected const KEY_IDENTIFICATION_SOURCE = 'Source';
33
    protected const KEY_CODE = 'code';
34
    protected const KEY_TIMESTAMP = 'Timestamp';
35
    protected const KEY_RESULT = 'Result';
36
    protected const KEY_STATUS = 'Status';
37
    protected const KEY_REASON = 'Reason';
38
    protected const KEY_RETURN = 'Return';
39
    protected const KEY_AMOUNT = 'Amount';
40
    protected const KEY_CURRENCY = 'Currency';
41
    protected const KEY_DESCRIPTOR = 'Descriptor';
42
43
    /**
44
     * @var \SprykerEco\Zed\Heidelpay\Business\Processor\Notification\Converter\NotificationXmlConverterInterface
45
     */
46
    protected $xmlConverter;
47
48
    /**
49
     * @var \SprykerEco\Zed\Heidelpay\Dependency\Service\HeidelpayToUtilEncodingServiceInterface
50
     */
51
    protected $utilEncodingService;
52
53
    /**
54
     * @var \Spryker\Shared\Money\Dependency\Plugin\MoneyPluginInterface
55
     */
56
    protected $moneyPlugin;
57
58
    /**
59
     * @var \SprykerEco\Zed\Heidelpay\Dependency\Plugin\HeidelpayNotificationExpanderPluginInterface[]
60
     */
61
    protected $notificationExpanderPlugins;
62
63
    /**
64
     * @param \SprykerEco\Zed\Heidelpay\Business\Processor\Notification\Converter\NotificationXmlConverterInterface $xmlConverter
65
     * @param \SprykerEco\Zed\Heidelpay\Dependency\Service\HeidelpayToUtilEncodingServiceInterface $utilEncodingService
66
     * @param \Spryker\Shared\Money\Dependency\Plugin\MoneyPluginInterface $moneyPlugin
67
     * @param \SprykerEco\Zed\Heidelpay\Dependency\Plugin\HeidelpayNotificationExpanderPluginInterface[] $notificationExpanderPlugins
68
     */
69
    public function __construct(
70
        NotificationXmlConverterInterface $xmlConverter,
71
        HeidelpayToUtilEncodingServiceInterface $utilEncodingService,
72
        MoneyPluginInterface $moneyPlugin,
73
        array $notificationExpanderPlugins = []
74
    ) {
75
        $this->xmlConverter = $xmlConverter;
76
        $this->utilEncodingService = $utilEncodingService;
77
        $this->moneyPlugin = $moneyPlugin;
78
        $this->notificationExpanderPlugins = $notificationExpanderPlugins;
79
    }
80
81
    /**
82
     * @param \Generated\Shared\Transfer\HeidelpayNotificationTransfer $notificationTransfer
83
     *
84
     * @return \Generated\Shared\Transfer\HeidelpayNotificationTransfer
85
     */
86
    public function expandWithNotificationData(HeidelpayNotificationTransfer $notificationTransfer): HeidelpayNotificationTransfer
87
    {
88
        $notificationData = $this->xmlConverter->convert($notificationTransfer->getNotificationBody());
89
90
        $notificationTransfer = $this->addTransactionData($notificationTransfer, $notificationData);
91
        $notificationTransfer = $this->addIdentificationData($notificationTransfer, $notificationData);
92
        $notificationTransfer = $this->addProcessingData($notificationTransfer, $notificationData);
93
        $notificationTransfer = $this->addPaymentData($notificationTransfer, $notificationData);
94
        $notificationTransfer = $this->addConnectorAccountData($notificationTransfer, $notificationData);
95
        $notificationTransfer = $this->addCustomerData($notificationTransfer, $notificationData);
96
97
        $notificationTransfer = $this->executeExpanderPlugins($notificationTransfer, $notificationData);
98
99
        return $notificationTransfer;
100
    }
101
102
    /**
103
     * @param \Generated\Shared\Transfer\HeidelpayNotificationTransfer $notificationTransfer
104
     * @param array $notificationData
105
     *
106
     * @return \Generated\Shared\Transfer\HeidelpayNotificationTransfer
107
     */
108
    protected function addTransactionData(
109
        HeidelpayNotificationTransfer $notificationTransfer,
110
        array $notificationData
111
    ): HeidelpayNotificationTransfer {
112
        if (!array_key_exists(static::KEY_ATTRIBUTES, $notificationData)) {
113
            return $notificationTransfer;
114
        }
115
116
        $transactionData = $notificationData[static::KEY_ATTRIBUTES];
117
        $notificationTransfer
118
            ->setTransactionSource($transactionData[static::KEY_TRANSACTION_SOURCE])
119
            ->setTransactionChannel($transactionData[static::KEY_CHANNEL])
120
            ->setTransactionResponseType($transactionData[static::KEY_RESPONSE])
121
            ->setTransactionMode($transactionData[static::KEY_MODE]);
122
123
        return $notificationTransfer;
124
    }
125
126
    /**
127
     * @param \Generated\Shared\Transfer\HeidelpayNotificationTransfer $notificationTransfer
128
     * @param array $notificationData
129
     *
130
     * @return \Generated\Shared\Transfer\HeidelpayNotificationTransfer
131
     */
132
    protected function addIdentificationData(
133
        HeidelpayNotificationTransfer $notificationTransfer,
134
        array $notificationData
135
    ): HeidelpayNotificationTransfer {
136
        if (!array_key_exists(static::KEY_IDENTIFICATION, $notificationData)) {
137
            return $notificationTransfer;
138
        }
139
140
        $identificationData = $notificationData[static::KEY_IDENTIFICATION];
141
        $notificationTransfer
142
            ->setTransactionId($identificationData[static::KEY_TRANSACTION_ID])
143
            ->setUniqueId($identificationData[static::KEY_UNIQUE_ID])
144
            ->setShortId($identificationData[static::KEY_SHORT_ID])
145
            ->setIdentificationSource($identificationData[static::KEY_IDENTIFICATION_SOURCE]);
146
147
        return $notificationTransfer;
148
    }
149
150
    /**
151
     * @param \Generated\Shared\Transfer\HeidelpayNotificationTransfer $notificationTransfer
152
     * @param array $notificationData
153
     *
154
     * @return \Generated\Shared\Transfer\HeidelpayNotificationTransfer
155
     */
156
    protected function addProcessingData(
157
        HeidelpayNotificationTransfer $notificationTransfer,
158
        array $notificationData
159
    ): HeidelpayNotificationTransfer {
160
        if (!array_key_exists(static::KEY_PROCESSING, $notificationData)) {
161
            return $notificationTransfer;
162
        }
163
164
        $processingData = $notificationData[static::KEY_PROCESSING];
165
        $notificationTransfer
166
            ->setResultCode($processingData[static::KEY_ATTRIBUTES][static::KEY_CODE])
167
            ->setResultTimestamp($processingData[static::KEY_TIMESTAMP])
168
            ->setResult($processingData[static::KEY_RESULT])
169
            ->setResultStatus($processingData[static::KEY_STATUS])
170
            ->setResultReason($processingData[static::KEY_REASON])
171
            ->setResultReturn($processingData[static::KEY_RETURN]);
172
173
        return $notificationTransfer;
174
    }
175
176
    /**
177
     * @param \Generated\Shared\Transfer\HeidelpayNotificationTransfer $notificationTransfer
178
     * @param array $notificationData
179
     *
180
     * @return \Generated\Shared\Transfer\HeidelpayNotificationTransfer
181
     */
182
    protected function addPaymentData(
183
        HeidelpayNotificationTransfer $notificationTransfer,
184
        array $notificationData
185
    ): HeidelpayNotificationTransfer {
186
        if (!array_key_exists(static::KEY_PAYMENT, $notificationData)) {
187
            return $notificationTransfer;
188
        }
189
190
        $paymentData = $notificationData[static::KEY_PAYMENT];
191
        $notificationTransfer
192
            ->setPaymentCode($paymentData[static::KEY_ATTRIBUTES][static::KEY_CODE])
193
            ->setCurrency($paymentData[static::KEY_CLEARING][static::KEY_CURRENCY])
194
            ->setPaymentDescriptor($paymentData[static::KEY_CLEARING][static::KEY_DESCRIPTOR])
195
            ->setAmount(
196
                $this->convertAmountToInt($paymentData[static::KEY_CLEARING][static::KEY_AMOUNT])
197
            );
198
199
        return $notificationTransfer;
200
    }
201
202
    /**
203
     * @param \Generated\Shared\Transfer\HeidelpayNotificationTransfer $notificationTransfer
204
     * @param array $notificationData
205
     *
206
     * @return \Generated\Shared\Transfer\HeidelpayNotificationTransfer
207
     */
208
    protected function addConnectorAccountData(
209
        HeidelpayNotificationTransfer $notificationTransfer,
210
        array $notificationData
211
    ): HeidelpayNotificationTransfer {
212
        if (!array_key_exists(static::KEY_CONNECTOR, $notificationData)
213
            || !array_key_exists(static::KEY_ACCOUNT, $notificationData[static::KEY_CONNECTOR])
214
        ) {
215
            return $notificationTransfer;
216
        }
217
218
        $notificationTransfer->setAccount(
219
            $this->utilEncodingService->encodeJson($notificationData[static::KEY_CONNECTOR][static::KEY_ACCOUNT])
220
        );
221
222
        return $notificationTransfer;
223
    }
224
225
    /**
226
     * @param \Generated\Shared\Transfer\HeidelpayNotificationTransfer $notificationTransfer
227
     * @param array $notificationData
228
     *
229
     * @return \Generated\Shared\Transfer\HeidelpayNotificationTransfer
230
     */
231
    protected function addCustomerData(
232
        HeidelpayNotificationTransfer $notificationTransfer,
233
        array $notificationData
234
    ): HeidelpayNotificationTransfer {
235
        if (!array_key_exists(static::KEY_CUSTOMER, $notificationData)) {
236
            return $notificationTransfer;
237
        }
238
239
        $notificationTransfer->setCustomer(
240
            $this->utilEncodingService->encodeJson($notificationData[static::KEY_CUSTOMER])
241
        );
242
243
        return $notificationTransfer;
244
    }
245
246
    /**
247
     * @param \Generated\Shared\Transfer\HeidelpayNotificationTransfer $notificationTransfer
248
     * @param string[][] $notificationData
249
     *
250
     * @return \Generated\Shared\Transfer\HeidelpayNotificationTransfer
251
     */
252
    protected function executeExpanderPlugins(
253
        HeidelpayNotificationTransfer $notificationTransfer,
254
        array $notificationData
255
    ): HeidelpayNotificationTransfer {
256
        foreach ($this->notificationExpanderPlugins as $notificationExpanderPlugin) {
257
            $notificationTransfer = $notificationExpanderPlugin->expand($notificationTransfer, $notificationData);
258
        }
259
260
        return $notificationTransfer;
261
    }
262
263
    /**
264
     * @param string $amount
265
     *
266
     * @return int
267
     */
268
    protected function convertAmountToInt(string $amount): int
269
    {
270
        return $this->moneyPlugin->convertDecimalToInteger((float)$amount);
271
    }
272
}
273