Passed
Pull Request — master (#4)
by Andrey
06:53 queued 03:48
created

AbstractAmazonpayTransaction   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 164
rs 10
c 0
b 0
f 0
wmc 13

9 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 14 1
A generateOperationReferenceId() 0 3 1
A assignAmazonpayPaymentToItemsIfNew() 0 7 2
A __construct() 0 11 1
A createPaymentEntity() 0 3 1
A loadPaymentEntity() 0 19 3
A allowPartialProcessing() 0 3 1
A isPartialProcessing() 0 5 2
A duplicatePaymentEntity() 0 12 1
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\Transaction;
9
10
use Generated\Shared\Transfer\AmazonpayCallTransfer;
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 SprykerEco\Shared\Amazonpay\AmazonpayConfigInterface;
13
use SprykerEco\Zed\Amazonpay\Business\Api\Adapter\CallAdapterInterface;
14
use SprykerEco\Zed\Amazonpay\Business\Converter\AmazonpayTransferToEntityConverterInterface;
15
use SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Transaction\Logger\TransactionLoggerInterface;
16
use SprykerEco\Zed\Amazonpay\Persistence\AmazonpayQueryContainerInterface;
17
18
abstract class AbstractAmazonpayTransaction extends AbstractTransaction implements AmazonpayTransactionInterface
19
{
20
21
    /**
22
     * @var \SprykerEco\Zed\Amazonpay\Persistence\AmazonpayQueryContainerInterface
23
     */
24
    protected $amazonpayQueryContainer;
25
26
    /**
27
     * @var \SprykerEco\Zed\Amazonpay\Business\Converter\AmazonpayTransferToEntityConverterInterface
28
     */
29
    protected $converter;
30
31
    /**
32
     * @var \Orm\Zed\Amazonpay\Persistence\SpyPaymentAmazonpay
33
     */
34
    protected $paymentEntity;
35
36
    /**
37
     * @var \Generated\Shared\Transfer\AmazonpayResponseTransfer
38
     */
39
    protected $apiResponse;
40
41
    /**
42
     * @param \SprykerEco\Zed\Amazonpay\Business\Api\Adapter\CallAdapterInterface $executionAdapter
43
     * @param \SprykerEco\Shared\Amazonpay\AmazonpayConfigInterface $config
44
     * @param \SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Transaction\Logger\TransactionLoggerInterface $transactionLogger
45
     * @param \SprykerEco\Zed\Amazonpay\Persistence\AmazonpayQueryContainerInterface $amazonpayQueryContainer
46
     * @param \SprykerEco\Zed\Amazonpay\Business\Converter\AmazonpayTransferToEntityConverterInterface $converter
47
     */
48
    public function __construct(
49
        CallAdapterInterface $executionAdapter,
50
        AmazonpayConfigInterface $config,
51
        TransactionLoggerInterface $transactionLogger,
52
        AmazonpayQueryContainerInterface $amazonpayQueryContainer,
53
        AmazonpayTransferToEntityConverterInterface $converter
54
    ) {
55
        parent::__construct($executionAdapter, $config, $transactionLogger);
56
57
        $this->amazonpayQueryContainer = $amazonpayQueryContainer;
58
        $this->converter = $converter;
59
    }
60
61
    /**
62
     * @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonpayCallTransfer
63
     *
64
     * @return string
65
     */
66
    protected function generateOperationReferenceId(AmazonpayCallTransfer $amazonpayCallTransfer)
67
    {
68
        return uniqid($amazonpayCallTransfer->getAmazonpayPayment()->getOrderReferenceId(), false);
69
    }
70
71
    /**
72
     * @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonpayCallTransfer
73
     *
74
     * @return \Generated\Shared\Transfer\AmazonpayCallTransfer
75
     */
76
    public function execute(AmazonpayCallTransfer $amazonpayCallTransfer)
77
    {
78
        $this->apiResponse = $this->executionAdapter->call($amazonpayCallTransfer);
79
        $amazonpayCallTransfer->getAmazonpayPayment()
80
            ->fromArray($this->apiResponse->modifiedToArray(), true)
81
            ->setResponseHeader($this->apiResponse->getHeader());
82
83
        $this->transactionsLogger->log(
84
            $amazonpayCallTransfer->getAmazonpayPayment()->getOrderReferenceId(),
85
            $this->apiResponse->getHeader()
86
        );
87
        $this->paymentEntity = $this->loadPaymentEntity($amazonpayCallTransfer);
88
89
        return $amazonpayCallTransfer;
90
    }
91
92
    /**
93
     * @param \Orm\Zed\Amazonpay\Persistence\SpyPaymentAmazonpay $entity
94
     * @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonpayCallTransfer
95
     *
96
     * @return void
97
     */
98
    protected function assignAmazonpayPaymentToItemsIfNew(SpyPaymentAmazonpay $entity, AmazonpayCallTransfer $amazonpayCallTransfer)
99
    {
100
        foreach ($amazonpayCallTransfer->getItems() as $itemTransfer) {
101
            $paymentForItemEntity = $this->amazonpayQueryContainer->queryBySalesOrderItemId($itemTransfer->getIdSalesOrderItem())
102
                ->findOneOrCreate();
103
            $paymentForItemEntity->setSpyPaymentAmazonpay($entity);
104
            $paymentForItemEntity->save();
105
        }
106
    }
107
108
    /**
109
     * @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonpayCallTransfer
110
     *
111
     * @return \Orm\Zed\Amazonpay\Persistence\SpyPaymentAmazonpay|null
112
     */
113
    protected function loadPaymentEntity(AmazonpayCallTransfer $amazonpayCallTransfer)
114
    {
115
        $paymentEntity = null;
116
117
        if ($amazonpayCallTransfer->getItems()->count()) {
118
            $paymentEntity = $this->amazonpayQueryContainer->queryPaymentBySalesOrderItemId(
119
                $amazonpayCallTransfer->getItems()[0]->getIdSalesOrderItem()
120
            )
121
                ->findOne();
122
        }
123
124
        if ($paymentEntity === null) {
125
            $paymentEntity = $this->amazonpayQueryContainer->queryPaymentByOrderReferenceId(
126
                $amazonpayCallTransfer->getAmazonpayPayment()->getOrderReferenceId()
127
            )
128
                ->findOne();
129
        }
130
131
        return $paymentEntity;
132
    }
133
134
    /**
135
     * @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonpayCallTransfer
136
     *
137
     * @return \Orm\Zed\Amazonpay\Persistence\SpyPaymentAmazonpay
138
     */
139
    protected function createPaymentEntity(AmazonpayCallTransfer $amazonpayCallTransfer)
140
    {
141
        return $this->converter->mapTransferToEntity($amazonpayCallTransfer->getAmazonpayPayment());
142
    }
143
144
    /**
145
     * @param \Orm\Zed\Amazonpay\Persistence\SpyPaymentAmazonpay $paymentAmazonpay
146
     *
147
     * @return \Orm\Zed\Amazonpay\Persistence\SpyPaymentAmazonpay
148
     */
149
    protected function duplicatePaymentEntity(SpyPaymentAmazonpay $paymentAmazonpay)
150
    {
151
        $newPaymentAmazonpay = new SpyPaymentAmazonpay();
152
153
        $paymentAmazonpay->setAuthorizationReferenceId(null);
154
        $newPaymentAmazonpay->fromArray($paymentAmazonpay->toArray());
155
        $paymentAmazonpay->setAmazonAuthorizationId(null);
156
        $paymentAmazonpay->save();
157
158
        $newPaymentAmazonpay->setIdPaymentAmazonpay(null);
159
160
        return $newPaymentAmazonpay;
161
    }
162
163
    /**
164
     * @param \Orm\Zed\Amazonpay\Persistence\SpyPaymentAmazonpay $paymentAmazonpay
165
     * @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonpayCallTransfer
166
     *
167
     * @return bool
168
     */
169
    protected function isPartialProcessing(SpyPaymentAmazonpay $paymentAmazonpay, AmazonpayCallTransfer $amazonpayCallTransfer)
170
    {
171
        return $this->allowPartialProcessing()
172
            && $amazonpayCallTransfer->getItems()->count()
173
                !== $paymentAmazonpay->getSpyPaymentAmazonpaySalesOrderItems()->count();
174
    }
175
176
    /**
177
     * @return bool
178
     */
179
    protected function allowPartialProcessing()
180
    {
181
        return true;
182
    }
183
184
}
185