Passed
Branch feature/ECO-573-per-item-proce... (fe5bf4)
by Andrey
04:56
created

createConcreteIpnRequestHandler()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 4
nop 1
dl 0
loc 18
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Apache OSL-2
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Ipn;
9
10
use Spryker\Shared\Kernel\Transfer\AbstractTransfer;
11
use SprykerEco\Shared\Amazonpay\AmazonpayConstants;
12
use SprykerEco\Zed\Amazonpay\Business\Order\RefundOrderInterface;
13
use SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Ipn\Logger\IpnRequestLoggerInterface;
14
use SprykerEco\Zed\Amazonpay\Dependency\Facade\AmazonpayToOmsInterface;
15
use SprykerEco\Zed\Amazonpay\Persistence\AmazonpayQueryContainerInterface;
16
17
class IpnRequestFactory implements IpnRequestFactoryInterface
18
{
19
20
    /**
21
     * @var \SprykerEco\Zed\Amazonpay\Dependency\Facade\AmazonpayToOmsInterface
22
     */
23
    protected $omsFacade;
24
25
    /**
26
     * @var \SprykerEco\Zed\Amazonpay\Persistence\AmazonpayQueryContainerInterface
27
     */
28
    protected $amazonpayQueryContainer;
29
30
    /**
31
     * @var \SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Ipn\Logger\IpnRequestLoggerInterface
32
     */
33
    protected $ipnRequestLogger;
34
35
    /**
36
     * @var \SprykerEco\Zed\Amazonpay\Business\Order\RefundOrderInterface
37
     */
38
    protected $refundOrderModel;
39
40
    /**
41
     * @param \SprykerEco\Zed\Amazonpay\Dependency\Facade\AmazonpayToOmsInterface $omsFacade
42
     * @param \SprykerEco\Zed\Amazonpay\Persistence\AmazonpayQueryContainerInterface $amazonpayQueryContainer
43
     * @param \SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Ipn\Logger\IpnRequestLoggerInterface $ipnRequestLogger
44
     * @param \SprykerEco\Zed\Amazonpay\Business\Order\RefundOrderInterface $refundOrderModel
45
     */
46
    public function __construct(
47
        AmazonpayToOmsInterface $omsFacade,
48
        AmazonpayQueryContainerInterface $amazonpayQueryContainer,
49
        IpnRequestLoggerInterface $ipnRequestLogger,
50
        RefundOrderInterface $refundOrderModel
51
    ) {
52
        $this->omsFacade = $omsFacade;
53
        $this->amazonpayQueryContainer = $amazonpayQueryContainer;
54
        $this->ipnRequestLogger = $ipnRequestLogger;
55
        $this->refundOrderModel = $refundOrderModel;
56
    }
57
58
    /**
59
     * @param \Spryker\Shared\Kernel\Transfer\AbstractTransfer | \Generated\Shared\Transfer\AmazonpayIpnPaymentAuthorizeRequestTransfer $ipnRequest
60
     *
61
     * @throws \SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Ipn\IpnHandlerNotFoundException
62
     *
63
     * @return \SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Ipn\IpnRequestHandlerInterface
64
     */
65
    public function createConcreteIpnRequestHandler(AbstractTransfer $ipnRequest)
66
    {
67
        switch ($ipnRequest->getMessage()->getNotificationType()) {
0 ignored issues
show
Bug introduced by
The method getMessage() does not exist on Spryker\Shared\Kernel\Transfer\AbstractTransfer. It seems like you code against a sub-type of Spryker\Shared\Kernel\Transfer\AbstractTransfer such as Generated\Shared\Transfe...uthorizeRequestTransfer or Generated\Shared\Transfer\CustomerErrorTransfer or Generated\Shared\Transfer\CheckoutErrorTransfer or Generated\Shared\Transfer\CustomerTransfer or Generated\Shared\Transfe...ntRefundRequestTransfer or Generated\Shared\Transfe...tCaptureRequestTransfer or Generated\Shared\Transfer\CommentTransfer or Generated\Shared\Transfe...ResponseMessageTransfer or Generated\Shared\Transfe...nceNotificationTransfer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
        switch ($ipnRequest->/** @scrutinizer ignore-call */ getMessage()->getNotificationType()) {
Loading history...
68
            case AmazonpayConstants::IPN_REQUEST_TYPE_PAYMENT_AUTHORIZE:
69
                return $this->createIpnPaymentAuthorizeHandler($ipnRequest);
70
71
            case AmazonpayConstants::IPN_REQUEST_TYPE_PAYMENT_CAPTURE:
72
                return $this->createIpnPaymentCaptureHandler($ipnRequest);
73
74
            case AmazonpayConstants::IPN_REQUEST_TYPE_PAYMENT_REFUND:
75
                return $this->createIpnPaymentRefundHandler($ipnRequest);
76
77
            case AmazonpayConstants::IPN_REQUEST_TYPE_ORDER_REFERENCE_NOTIFICATION:
78
                return $this->createIpnOrderReferenceHandler($ipnRequest);
79
        }
80
81
        throw new IpnHandlerNotFoundException('Unknown IPN Notification type: ' .
82
            $ipnRequest->getMessage()->getNotificationType());
83
    }
84
85
    /**
86
     * @param \Spryker\Shared\Kernel\Transfer\AbstractTransfer | \Generated\Shared\Transfer\AmazonpayIpnPaymentAuthorizeRequestTransfer $ipnRequest
87
     *
88
     * @throws \SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Ipn\IpnHandlerNotFoundException
89
     *
90
     * @return \SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Ipn\IpnRequestHandlerInterface
91
     */
92 View Code Duplication
    protected function createIpnPaymentAuthorizeHandler(AbstractTransfer $ipnRequest)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
    {
94
        if ($ipnRequest->getAuthorizationDetails()->getAuthorizationStatus()->getIsSuspended()) {
0 ignored issues
show
Bug introduced by
The method getAuthorizationDetails() does not exist on Spryker\Shared\Kernel\Transfer\AbstractTransfer. It seems like you code against a sub-type of Spryker\Shared\Kernel\Transfer\AbstractTransfer such as Generated\Shared\Transfe...azonpayResponseTransfer or Generated\Shared\Transfer\AmazonpayPaymentTransfer or Generated\Shared\Transfe...uthorizeRequestTransfer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

94
        if ($ipnRequest->/** @scrutinizer ignore-call */ getAuthorizationDetails()->getAuthorizationStatus()->getIsSuspended()) {
Loading history...
95
            return $this->createIpnPaymentAuthorizeSuspendedHandler();
96
        }
97
98
        if ($ipnRequest->getAuthorizationDetails()->getAuthorizationStatus()->getIsDeclined()) {
99
            return $this->createIpnPaymentAuthorizeDeclineHandler();
100
        }
101
102
        if ($ipnRequest->getAuthorizationDetails()->getAuthorizationStatus()->getIsOpen()) {
103
            return $this->createIpnPaymentAuthorizeOpenHandler();
104
        }
105
106
        if ($ipnRequest->getAuthorizationDetails()->getAuthorizationStatus()->getIsClosed()) {
107
            return $this->createIpnPaymentAuthorizeClosedHandler();
108
        }
109
110
        throw new IpnHandlerNotFoundException('No IPN handler for auth payment and status ' .
111
            $ipnRequest->getAuthorizationDetails()->getAuthorizationStatus()->getState());
112
    }
113
114
    /**
115
     * @return \SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Ipn\IpnRequestHandlerInterface
116
     */
117
    protected function createIpnPaymentAuthorizeSuspendedHandler()
118
    {
119
        return new IpnPaymentAuthorizeSuspendedHandler(
120
            $this->omsFacade,
121
            $this->amazonpayQueryContainer,
122
            $this->ipnRequestLogger
123
        );
124
    }
125
126
    /**
127
     * @return \SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Ipn\IpnRequestHandlerInterface
128
     */
129
    protected function createIpnPaymentAuthorizeDeclineHandler()
130
    {
131
        return new IpnPaymentAuthorizeDeclineHandler(
132
            $this->omsFacade,
133
            $this->amazonpayQueryContainer,
134
            $this->ipnRequestLogger
135
        );
136
    }
137
138
    /**
139
     * @return \SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Ipn\IpnRequestHandlerInterface
140
     */
141
    protected function createIpnPaymentAuthorizeOpenHandler()
142
    {
143
        return new IpnPaymentAuthorizeOpenHandler(
144
            $this->omsFacade,
145
            $this->amazonpayQueryContainer,
146
            $this->ipnRequestLogger
147
        );
148
    }
149
150
    /**
151
     * @return \SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Ipn\IpnRequestHandlerInterface
152
     */
153
    protected function createIpnPaymentAuthorizeClosedHandler()
154
    {
155
        return new IpnPaymentAuthorizeClosedHandler(
156
            $this->omsFacade,
157
            $this->amazonpayQueryContainer,
158
            $this->ipnRequestLogger
159
        );
160
    }
161
162
    /**
163
     * @param \Spryker\Shared\Kernel\Transfer\AbstractTransfer | \Generated\Shared\Transfer\AmazonpayIpnPaymentCaptureRequestTransfer $ipnRequest
164
     *
165
     * @throws \SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Ipn\IpnHandlerNotFoundException
166
     *
167
     * @return \SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Ipn\IpnRequestHandlerInterface
168
     */
169
    protected function createIpnPaymentCaptureHandler(AbstractTransfer $ipnRequest)
170
    {
171
        if ($ipnRequest->getCaptureDetails()->getCaptureStatus()->getIsDeclined()) {
0 ignored issues
show
Bug introduced by
The method getCaptureDetails() does not exist on Spryker\Shared\Kernel\Transfer\AbstractTransfer. It seems like you code against a sub-type of Spryker\Shared\Kernel\Transfer\AbstractTransfer such as Generated\Shared\Transfe...azonpayResponseTransfer or Generated\Shared\Transfer\AmazonpayPaymentTransfer or Generated\Shared\Transfe...tCaptureRequestTransfer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

171
        if ($ipnRequest->/** @scrutinizer ignore-call */ getCaptureDetails()->getCaptureStatus()->getIsDeclined()) {
Loading history...
172
            return $this->createIpnPaymentCaptureDeclineHandler();
173
        }
174
175
        if ($ipnRequest->getCaptureDetails()->getCaptureStatus()->getIsCompleted()) {
176
            return $this->createIpnPaymentCaptureCompletedHandler();
177
        }
178
179
        if ($ipnRequest->getCaptureDetails()->getCaptureStatus()->getIsClosed()) {
180
            return $this->createIpnEmptyHandler();
181
        }
182
183
        throw new IpnHandlerNotFoundException('No IPN handler for capture and status ' . $ipnRequest->getCaptureDetails()->getCaptureStatus()->getState());
184
    }
185
186
    /**
187
     * @return \SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Ipn\IpnRequestHandlerInterface
188
     */
189
    protected function createIpnPaymentCaptureDeclineHandler()
190
    {
191
        return new IpnPaymentCaptureDeclineHandler(
192
            $this->omsFacade,
193
            $this->amazonpayQueryContainer,
194
            $this->ipnRequestLogger
195
        );
196
    }
197
198
    /**
199
     * @return \SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Ipn\IpnRequestHandlerInterface
200
     */
201
    protected function createIpnPaymentCaptureCompletedHandler()
202
    {
203
        return new IpnPaymentCaptureCompletedHandler(
204
            $this->omsFacade,
205
            $this->amazonpayQueryContainer,
206
            $this->ipnRequestLogger
207
        );
208
    }
209
210
    /**
211
     * @return \SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Ipn\IpnRequestHandlerInterface
212
     */
213
    protected function createIpnEmptyHandler()
214
    {
215
        return new IpnEmptyHandler();
216
    }
217
218
    /**
219
     * @param \Spryker\Shared\Kernel\Transfer\AbstractTransfer | \Generated\Shared\Transfer\AmazonpayIpnPaymentRefundRequestTransfer $ipnRequest
220
     *
221
     * @throws \SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Ipn\IpnHandlerNotFoundException
222
     *
223
     * @return \SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Ipn\IpnRequestHandlerInterface
224
     */
225
    protected function createIpnPaymentRefundHandler(AbstractTransfer $ipnRequest)
226
    {
227
        if ($ipnRequest->getRefundDetails()->getRefundStatus()->getIsDeclined()) {
0 ignored issues
show
Bug introduced by
The method getRefundDetails() does not exist on Spryker\Shared\Kernel\Transfer\AbstractTransfer. It seems like you code against a sub-type of Spryker\Shared\Kernel\Transfer\AbstractTransfer such as Generated\Shared\Transfe...azonpayResponseTransfer or Generated\Shared\Transfer\AmazonpayPaymentTransfer or Generated\Shared\Transfe...ntRefundRequestTransfer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

227
        if ($ipnRequest->/** @scrutinizer ignore-call */ getRefundDetails()->getRefundStatus()->getIsDeclined()) {
Loading history...
228
            return $this->createIpnPaymentRefundDeclineHandler();
229
        }
230
231
        if ($ipnRequest->getRefundDetails()->getRefundStatus()->getIsCompleted()) {
232
            return $this->createIpnPaymentRefundCompletedHandler();
233
        }
234
235
        throw new IpnHandlerNotFoundException('No IPN handler for capture and status ' . $ipnRequest->getRefundDetails()->getRefundStatus()->getState());
236
    }
237
238
    /**
239
     * @return \SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Ipn\IpnRequestHandlerInterface
240
     */
241
    protected function createIpnPaymentRefundDeclineHandler()
242
    {
243
        return new IpnPaymentRefundDeclineHandler(
244
            $this->omsFacade,
245
            $this->amazonpayQueryContainer,
246
            $this->ipnRequestLogger
247
        );
248
    }
249
250
    /**
251
     * @return \SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Ipn\IpnRequestHandlerInterface
252
     */
253
    protected function createIpnPaymentRefundCompletedHandler()
254
    {
255
        return new IpnPaymentRefundCompletedHandler(
256
            $this->omsFacade,
257
            $this->amazonpayQueryContainer,
258
            $this->ipnRequestLogger,
259
            $this->refundOrderModel
260
        );
261
    }
262
263
    /**
264
     * @param \Spryker\Shared\Kernel\Transfer\AbstractTransfer | \Generated\Shared\Transfer\AmazonpayIpnOrderReferenceNotificationTransfer $ipnRequest
265
     *
266
     * @throws \SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Ipn\IpnHandlerNotFoundException
267
     *
268
     * @return \SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Ipn\IpnRequestHandlerInterface
269
     */
270 View Code Duplication
    protected function createIpnOrderReferenceHandler(AbstractTransfer $ipnRequest)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
271
    {
272
        if ($ipnRequest->getOrderReferenceStatus()->getIsOpen()) {
0 ignored issues
show
Bug introduced by
The method getOrderReferenceStatus() does not exist on Spryker\Shared\Kernel\Transfer\AbstractTransfer. It seems like you code against a sub-type of Spryker\Shared\Kernel\Transfer\AbstractTransfer such as Generated\Shared\Transfe...azonpayResponseTransfer or Generated\Shared\Transfer\AmazonpayPaymentTransfer or Generated\Shared\Transfe...nceNotificationTransfer or Generated\Shared\Transfe...nceNotificationTransfer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

272
        if ($ipnRequest->/** @scrutinizer ignore-call */ getOrderReferenceStatus()->getIsOpen()) {
Loading history...
273
            return $this->createIpnOrderReferenceOpenHandler();
274
        }
275
276
        if ($ipnRequest->getOrderReferenceStatus()->getIsClosed()) {
277
            if ($ipnRequest->getOrderReferenceStatus()->getIsClosedByAmazon()) {
278
                return $this->createIpnOrderReferenceClosedHandler();
279
            }
280
281
            return $this->createIpnEmptyHandler();
282
        }
283
284
        if ($ipnRequest->getOrderReferenceStatus()->getIsSuspended()) {
285
            return $this->createIpnOrderReferenceSuspendedHandler();
286
        }
287
288
        if ($ipnRequest->getOrderReferenceStatus()->getIsCancelled()) {
289
            return $this->createIpnOrderReferenceCancelledHandler();
290
        }
291
292
        throw new IpnHandlerNotFoundException('No IPN handler for capture and status ' . $ipnRequest->getOrderReferenceStatus()->getState());
293
    }
294
295
    /**
296
     * @return \SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Ipn\IpnRequestHandlerInterface
297
     */
298
    protected function createIpnOrderReferenceOpenHandler()
299
    {
300
        return new IpnOrderReferenceOpenHandler(
301
            $this->omsFacade,
302
            $this->amazonpayQueryContainer,
303
            $this->ipnRequestLogger
304
        );
305
    }
306
307
    /**
308
     * @return \SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Ipn\IpnRequestHandlerInterface
309
     */
310
    protected function createIpnOrderReferenceClosedHandler()
311
    {
312
        return new IpnOrderReferenceClosedHandler(
313
            $this->omsFacade,
314
            $this->amazonpayQueryContainer,
315
            $this->ipnRequestLogger
316
        );
317
    }
318
319
    /**
320
     * @return \SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Ipn\IpnRequestHandlerInterface
321
     */
322
    protected function createIpnOrderReferenceSuspendedHandler()
323
    {
324
        return new IpnOrderReferenceSuspendedHandler(
325
            $this->omsFacade,
326
            $this->amazonpayQueryContainer,
327
            $this->ipnRequestLogger
328
        );
329
    }
330
331
    /**
332
     * @return \SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Ipn\IpnRequestHandlerInterface
333
     */
334
    protected function createIpnOrderReferenceCancelledHandler()
335
    {
336
        return new IpnOrderReferenceCancelledHandler(
337
            $this->omsFacade,
338
            $this->amazonpayQueryContainer,
339
            $this->ipnRequestLogger
340
        );
341
    }
342
343
}
344