Passed
Push — feature/ECO-808-scrutinizer ( 956529...82fb0b )
by Andrey
05:28 queued 01:30
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
    protected function createIpnPaymentAuthorizeHandler(AbstractTransfer $ipnRequest)
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 new IpnPaymentAuthorizeSuspendedHandler(
96
                $this->omsFacade,
97
                $this->amazonpayQueryContainer,
98
                $this->ipnRequestLogger
99
            );
100
        }
101
102
        if ($ipnRequest->getAuthorizationDetails()->getAuthorizationStatus()->getIsDeclined()) {
103
            return new IpnPaymentAuthorizeDeclineHandler(
104
                $this->omsFacade,
105
                $this->amazonpayQueryContainer,
106
                $this->ipnRequestLogger
107
            );
108
        }
109
110
        if ($ipnRequest->getAuthorizationDetails()->getAuthorizationStatus()->getIsOpen()) {
111
            return new IpnPaymentAuthorizeOpenHandler(
112
                $this->omsFacade,
113
                $this->amazonpayQueryContainer,
114
                $this->ipnRequestLogger
115
            );
116
        }
117
118
        if ($ipnRequest->getAuthorizationDetails()->getAuthorizationStatus()->getIsClosed()) {
119
            return new IpnPaymentAuthorizeClosedHandler(
120
                $this->omsFacade,
121
                $this->amazonpayQueryContainer,
122
                $this->ipnRequestLogger
123
            );
124
        }
125
126
        throw new IpnHandlerNotFoundException('No IPN handler for auth payment and status ' .
127
            $ipnRequest->getAuthorizationDetails()->getAuthorizationStatus()->getState());
128
    }
129
130
    /**
131
     * @param \Spryker\Shared\Kernel\Transfer\AbstractTransfer | \Generated\Shared\Transfer\AmazonpayIpnPaymentCaptureRequestTransfer $ipnRequest
132
     *
133
     * @throws \SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Ipn\IpnHandlerNotFoundException
134
     *
135
     * @return \SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Ipn\IpnRequestHandlerInterface
136
     */
137
    protected function createIpnPaymentCaptureHandler(AbstractTransfer $ipnRequest)
138
    {
139
        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

139
        if ($ipnRequest->/** @scrutinizer ignore-call */ getCaptureDetails()->getCaptureStatus()->getIsDeclined()) {
Loading history...
140
            return new IpnPaymentCaptureDeclineHandler(
141
                $this->omsFacade,
142
                $this->amazonpayQueryContainer,
143
                $this->ipnRequestLogger
144
            );
145
        }
146
147
        if ($ipnRequest->getCaptureDetails()->getCaptureStatus()->getIsCompleted()) {
148
            return new IpnPaymentCaptureCompletedHandler(
149
                $this->omsFacade,
150
                $this->amazonpayQueryContainer,
151
                $this->ipnRequestLogger
152
            );
153
        }
154
155
        if ($ipnRequest->getCaptureDetails()->getCaptureStatus()->getIsClosed()) {
156
            return new IpnEmptyHandler();
157
        }
158
159
        throw new IpnHandlerNotFoundException('No IPN handler for capture and status ' .
160
            $ipnRequest->getCaptureDetails()->getCaptureStatus()->getState());
161
    }
162
163
    /**
164
     * @param \Spryker\Shared\Kernel\Transfer\AbstractTransfer | \Generated\Shared\Transfer\AmazonpayIpnPaymentRefundRequestTransfer $ipnRequest
165
     *
166
     * @throws \SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Ipn\IpnHandlerNotFoundException
167
     *
168
     * @return \SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Ipn\IpnRequestHandlerInterface
169
     */
170
    protected function createIpnPaymentRefundHandler(AbstractTransfer $ipnRequest)
171
    {
172
        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

172
        if ($ipnRequest->/** @scrutinizer ignore-call */ getRefundDetails()->getRefundStatus()->getIsDeclined()) {
Loading history...
173
            return new IpnPaymentRefundDeclineHandler(
174
                $this->omsFacade,
175
                $this->amazonpayQueryContainer,
176
                $this->ipnRequestLogger
177
            );
178
        }
179
180
        if ($ipnRequest->getRefundDetails()->getRefundStatus()->getIsCompleted()) {
181
            return new IpnPaymentRefundCompletedHandler(
182
                $this->omsFacade,
183
                $this->amazonpayQueryContainer,
184
                $this->ipnRequestLogger,
185
                $this->refundOrderModel
186
            );
187
        }
188
189
        throw new IpnHandlerNotFoundException('No IPN handler for payment refund and status ' .
190
            $ipnRequest->getRefundDetails()->getRefundStatus()->getState());
191
    }
192
193
    /**
194
     * @param \Spryker\Shared\Kernel\Transfer\AbstractTransfer | \Generated\Shared\Transfer\AmazonpayIpnOrderReferenceNotificationTransfer $ipnRequest
195
     *
196
     * @throws \SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Ipn\IpnHandlerNotFoundException
197
     *
198
     * @return \SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Ipn\IpnRequestHandlerInterface
199
     */
200
    protected function createIpnOrderReferenceHandler(AbstractTransfer $ipnRequest)
201
    {
202
        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

202
        if ($ipnRequest->/** @scrutinizer ignore-call */ getOrderReferenceStatus()->getIsOpen()) {
Loading history...
203
            return new IpnOrderReferenceOpenHandler(
204
                $this->omsFacade,
205
                $this->amazonpayQueryContainer,
206
                $this->ipnRequestLogger
207
            );
208
        }
209
210
        if ($ipnRequest->getOrderReferenceStatus()->getIsClosed()) {
211
            if ($ipnRequest->getOrderReferenceStatus()->getIsClosedByAmazon()) {
212
                return new IpnOrderReferenceClosedHandler(
213
                    $this->omsFacade,
214
                    $this->amazonpayQueryContainer,
215
                    $this->ipnRequestLogger
216
                );
217
            }
218
219
            return new IpnEmptyHandler(
220
                $this->omsFacade,
0 ignored issues
show
Unused Code introduced by
The call to SprykerEco\Zed\Amazonpay...yHandler::__construct() has too many arguments starting with $this->omsFacade. ( Ignorable by Annotation )

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

220
            return /** @scrutinizer ignore-call */ new IpnEmptyHandler(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
221
                $this->amazonpayQueryContainer,
222
                $this->ipnRequestLogger
223
            );
224
        }
225
226
        if ($ipnRequest->getOrderReferenceStatus()->getIsSuspended()) {
227
            return new IpnOrderReferenceSuspendedHandler(
228
                $this->omsFacade,
229
                $this->amazonpayQueryContainer,
230
                $this->ipnRequestLogger
231
            );
232
        }
233
234
        if ($ipnRequest->getOrderReferenceStatus()->getIsCancelled()) {
235
            return new IpnOrderReferenceCancelledHandler(
236
                $this->omsFacade,
237
                $this->amazonpayQueryContainer,
238
                $this->ipnRequestLogger
239
            );
240
        }
241
242
        throw new IpnHandlerNotFoundException('No IPN handler for order reference and status ' .
243
            $ipnRequest->getOrderReferenceStatus()->getState());
244
    }
245
246
}
247