Passed
Push — bugfix/ECO-1031-recent-review ( 143816...453a5f )
by Andrey
03:59
created

IpnAbstractTransferRequestHandler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 105
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
B handle() 0 30 4
A __construct() 0 8 1
A getAffectedSalesOrderItems() 0 9 2
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 Generated\Shared\Transfer\AmazonpayIpnPaymentRequestTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfe...nPaymentRequestTransfer 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 Orm\Zed\AmazonPay\Persistence\SpyPaymentAmazonpay;
1 ignored issue
show
Bug introduced by
The type Orm\Zed\AmazonPay\Persistence\SpyPaymentAmazonpay 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 Propel\Runtime\Collection\ObjectCollection;
13
use Propel\Runtime\Propel;
14
use Spryker\Zed\PropelOrm\Business\Transaction\DatabaseTransactionHandlerTrait;
15
use SprykerEco\Zed\AmazonPay\Business\Payment\Handler\Ipn\Logger\IpnRequestLoggerInterface;
16
use SprykerEco\Zed\AmazonPay\Dependency\Facade\AmazonPayToOmsInterface;
17
use SprykerEco\Zed\AmazonPay\Persistence\AmazonPayQueryContainerInterface;
18
19
abstract class IpnAbstractTransferRequestHandler implements IpnRequestHandlerInterface
20
{
21
    use DatabaseTransactionHandlerTrait;
22
23
    /**
24
     * @var \SprykerEco\Zed\AmazonPay\Dependency\Facade\AmazonPayToOmsInterface $omsFacade
25
     */
26
    protected $omsFacade;
27
28
    /**
29
     * @var \SprykerEco\Zed\AmazonPay\Persistence\AmazonPayQueryContainerInterface $queryContainer
30
     */
31
    protected $queryContainer;
32
33
    /**
34
     * @var \SprykerEco\Zed\AmazonPay\Business\Payment\Handler\Ipn\Logger\IpnRequestLoggerInterface $ipnRequestLogger
35
     */
36
    protected $ipnRequestLogger;
37
38
    /**
39
     * @param \SprykerEco\Zed\AmazonPay\Dependency\Facade\AmazonPayToOmsInterface $omsFacade
40
     * @param \SprykerEco\Zed\AmazonPay\Persistence\AmazonPayQueryContainerInterface $queryContainer
41
     * @param \SprykerEco\Zed\AmazonPay\Business\Payment\Handler\Ipn\Logger\IpnRequestLoggerInterface $ipnRequestLogger
42
     */
43
    public function __construct(
44
        AmazonPayToOmsInterface $omsFacade,
45
        AmazonPayQueryContainerInterface $queryContainer,
46
        IpnRequestLoggerInterface $ipnRequestLogger
47
    ) {
48
        $this->omsFacade = $omsFacade;
49
        $this->queryContainer = $queryContainer;
50
        $this->ipnRequestLogger = $ipnRequestLogger;
51
    }
52
53
    /**
54
     * @param \Generated\Shared\Transfer\AmazonpayIpnPaymentRequestTransfer $paymentRequestTransfer
55
     *
56
     * @return void
57
     */
58
    public function handle(AmazonpayIpnPaymentRequestTransfer $paymentRequestTransfer)
59
    {
60
        $repeatable = 2;
61
        while ($repeatable-- > 0) {
62
            try {
63
                $this->handleDatabaseTransaction(function () use ($paymentRequestTransfer) {
64
                    Propel::getConnection()->exec('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;');
65
                    Propel::disableInstancePooling();
66
67
                    $paymentEntity = $this->retrievePaymentEntity($paymentRequestTransfer);
68
69
                    if ($paymentEntity === null) {
70
                        return;
71
                    }
72
73
                    $paymentEntity->setStatus($this->getStatusName());
74
                    $paymentEntity->save();
75
76
                    $this->omsFacade->triggerEvent(
77
                        $this->getOmsEventId(),
78
                        $this->getAffectedSalesOrderItems($paymentEntity),
79
                        []
80
                    );
81
                    $this->omsFacade->checkConditions();
82
83
                    $this->ipnRequestLogger->log($paymentRequestTransfer, $paymentEntity);
84
                });
85
86
                break;
87
            } catch (\Exception $e) {
1 ignored issue
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
88
            }
89
        }
90
    }
91
92
    /**
93
     * @param \Orm\Zed\AmazonPay\Persistence\SpyPaymentAmazonpay $paymentEntity
94
     *
95
     * @return \Propel\Runtime\Collection\ObjectCollection | \Orm\Zed\Sales\Persistence\SpySalesOrderItem[]
96
     */
97
    protected function getAffectedSalesOrderItems(SpyPaymentAmazonpay $paymentEntity)
98
    {
99
        $items = new ObjectCollection();
100
101
        foreach ($paymentEntity->getSpyPaymentAmazonpaySalesOrderItems() as $amazonpaySalesOrderItem) {
102
            $items[] = $amazonpaySalesOrderItem->getSpySalesOrderItem();
103
        }
104
105
        return $items;
106
    }
107
108
    /**
109
     * @param \Generated\Shared\Transfer\AmazonpayIpnPaymentRequestTransfer $paymentRequestTransfer
110
     *
111
     * @return \Orm\Zed\AmazonPay\Persistence\SpyPaymentAmazonpay|null
112
     */
113
    abstract protected function retrievePaymentEntity(AmazonpayIpnPaymentRequestTransfer $paymentRequestTransfer);
114
115
    /**
116
     * @return string
117
     */
118
    abstract protected function getOmsEventId();
119
120
    /**
121
     * @return string
122
     */
123
    abstract protected function getStatusName();
124
}
125