Completed
Push — feature/ECO-1185-eco-ci ( 4da4ac...5d4add )
by Andrey
13:41 queued 10:25
created

handleTransactional()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 1
dl 0
loc 19
rs 9.4285
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 Exception;
11
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...
12
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...
13
use Propel\Runtime\Collection\ObjectCollection;
14
use Propel\Runtime\Propel;
15
use Spryker\Zed\PropelOrm\Business\Transaction\DatabaseTransactionHandlerTrait;
16
use SprykerEco\Shared\AmazonPay\AmazonPayConfigInterface;
17
use SprykerEco\Zed\AmazonPay\Business\Payment\Handler\Ipn\Logger\IpnRequestLoggerInterface;
18
use SprykerEco\Zed\AmazonPay\Dependency\Facade\AmazonPayToOmsInterface;
19
use SprykerEco\Zed\AmazonPay\Persistence\AmazonPayQueryContainerInterface;
20
21
abstract class IpnAbstractTransferRequestHandler implements IpnRequestHandlerInterface
22
{
23
    use DatabaseTransactionHandlerTrait;
0 ignored issues
show
Deprecated Code introduced by
The trait Spryker\Zed\PropelOrm\Bu...TransactionHandlerTrait has been deprecated: use \Spryker\Zed\Kernel\Persistence\EntityManager\TransactionTrait instead ( Ignorable by Annotation )

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

23
    use /** @scrutinizer ignore-deprecated */ DatabaseTransactionHandlerTrait;

This trait has been deprecated. The supplier of the trait has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the trait will be removed and what other trait to use instead.

Loading history...
24
25
    /**
26
     * @var \SprykerEco\Zed\AmazonPay\Dependency\Facade\AmazonPayToOmsInterface
27
     */
28
    protected $omsFacade;
29
30
    /**
31
     * @var \SprykerEco\Zed\AmazonPay\Persistence\AmazonPayQueryContainerInterface
32
     */
33
    protected $queryContainer;
34
35
    /**
36
     * @var \SprykerEco\Zed\AmazonPay\Business\Payment\Handler\Ipn\Logger\IpnRequestLoggerInterface
37
     */
38
    protected $ipnRequestLogger;
39
40
    /**
41
     * @var \SprykerEco\Shared\AmazonPay\AmazonPayConfigInterface
42
     */
43
    protected $config;
44
45
    /**
46
     * @param \SprykerEco\Zed\AmazonPay\Dependency\Facade\AmazonPayToOmsInterface $omsFacade
47
     * @param \SprykerEco\Zed\AmazonPay\Persistence\AmazonPayQueryContainerInterface $queryContainer
48
     * @param \SprykerEco\Zed\AmazonPay\Business\Payment\Handler\Ipn\Logger\IpnRequestLoggerInterface $ipnRequestLogger
49
     * @param \SprykerEco\Shared\AmazonPay\AmazonPayConfigInterface $config
50
     */
51
    public function __construct(
52
        AmazonPayToOmsInterface $omsFacade,
53
        AmazonPayQueryContainerInterface $queryContainer,
54
        IpnRequestLoggerInterface $ipnRequestLogger,
55
        AmazonPayConfigInterface $config
56
    ) {
57
        $this->omsFacade = $omsFacade;
58
        $this->queryContainer = $queryContainer;
59
        $this->ipnRequestLogger = $ipnRequestLogger;
60
        $this->config = $config;
61
    }
62
63
    /**
64
     * @param \Generated\Shared\Transfer\AmazonpayIpnPaymentRequestTransfer $paymentRequestTransfer
65
     *
66
     * @return void
67
     */
68
    public function handle(AmazonpayIpnPaymentRequestTransfer $paymentRequestTransfer)
69
    {
70
        $repeatable = 2;
71
        while ($repeatable-- > 0) {
72
            try {
73
                $this->handleDatabaseTransaction(function () use ($paymentRequestTransfer) {
74
                    if ($this->config->getEnableIsolateLevelRead()) {
75
                        Propel::getConnection()->exec('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;');
76
                    }
77
                    Propel::disableInstancePooling();
78
79
                    $this->handleTransactional($paymentRequestTransfer);
80
                });
81
82
                break;
83
            } catch (Exception $e) {
1 ignored issue
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
84
            }
85
        }
86
    }
87
88
    /**
89
     * @param \Generated\Shared\Transfer\AmazonpayIpnPaymentRequestTransfer $paymentRequestTransfer
90
     *
91
     * @return void
92
     */
93
    protected function handleTransactional(AmazonpayIpnPaymentRequestTransfer $paymentRequestTransfer)
94
    {
95
        $paymentEntity = $this->retrievePaymentEntity($paymentRequestTransfer);
96
97
        if ($paymentEntity === null) {
98
            return;
99
        }
100
101
        $paymentEntity->setStatus($this->getStatusName());
102
        $paymentEntity->save();
103
104
        $this->omsFacade->triggerEvent(
105
            $this->getOmsEventId(),
106
            $this->getAffectedSalesOrderItems($paymentEntity),
107
            []
108
        );
109
        $this->omsFacade->checkConditions();
110
111
        $this->ipnRequestLogger->log($paymentRequestTransfer, $paymentEntity);
112
    }
113
114
    /**
115
     * @param \Orm\Zed\AmazonPay\Persistence\SpyPaymentAmazonpay $paymentEntity
116
     *
117
     * @return \Propel\Runtime\Collection\ObjectCollection | \Orm\Zed\Sales\Persistence\SpySalesOrderItem[]
118
     */
119
    protected function getAffectedSalesOrderItems(SpyPaymentAmazonpay $paymentEntity)
120
    {
121
        $items = new ObjectCollection();
122
123
        foreach ($paymentEntity->getSpyPaymentAmazonpaySalesOrderItems() as $amazonpaySalesOrderItem) {
124
            $items[] = $amazonpaySalesOrderItem->getSpySalesOrderItem();
125
        }
126
127
        return $items;
128
    }
129
130
    /**
131
     * @param \Generated\Shared\Transfer\AmazonpayIpnPaymentRequestTransfer $paymentRequestTransfer
132
     *
133
     * @return \Orm\Zed\AmazonPay\Persistence\SpyPaymentAmazonpay|null
134
     */
135
    abstract protected function retrievePaymentEntity(AmazonpayIpnPaymentRequestTransfer $paymentRequestTransfer);
136
137
    /**
138
     * @return string
139
     */
140
    abstract protected function getOmsEventId();
141
142
    /**
143
     * @return string
144
     */
145
    abstract protected function getStatusName();
146
}
147