Passed
Pull Request — master (#45)
by
unknown
03:36
created

PayPalExpressResponseSaver   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 46
c 1
b 0
f 0
dl 0
loc 156
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A savePaymentComputopDetailEntity() 0 14 2
A savePaymentComputopEntity() 0 8 1
A savePaymentComputopOrderItemEntities() 0 13 2
A __construct() 0 8 1
A save() 0 24 3
A updateComputopSalesOrderItemCollection() 0 10 3
A executeSavePaymentComputopDataTransaction() 0 7 1
1
<?php
2
3
/**
4
 * MIT License
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\Computop\Business\Payment\Handler\Saver\Init;
9
10
use Generated\Shared\Transfer\ComputopPaymentComputopOrderItemCollectionTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...rItemCollectionTransfer 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 Generated\Shared\Transfer\ComputopPaymentComputopTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...PaymentComputopTransfer 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 Generated\Shared\Transfer\ComputopPayPalExpressInitResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...essInitResponseTransfer 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 Generated\Shared\Transfer\ComputopSalesOrderItemTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...pSalesOrderItemTransfer 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...
14
use Generated\Shared\Transfer\QuoteTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\QuoteTransfer 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...
15
use Spryker\Zed\Kernel\Persistence\EntityManager\TransactionTrait;
16
use SprykerEco\Zed\Computop\ComputopConfig;
17
use SprykerEco\Zed\Computop\Persistence\ComputopEntityManagerInterface;
18
use SprykerEco\Zed\Computop\Persistence\ComputopRepositoryInterface;
19
20
class PayPalExpressResponseSaver implements InitResponseSaverInterface
21
{
22
    use TransactionTrait;
23
24
    /**
25
     * @var \SprykerEco\Zed\Computop\ComputopConfig
26
     */
27
    protected $computopConfig;
28
29
    /**
30
     * @var \SprykerEco\Zed\Computop\Persistence\ComputopRepositoryInterface
31
     */
32
    protected $computopRepository;
33
34
    /**
35
     * @var \SprykerEco\Zed\Computop\Persistence\ComputopEntityManagerInterface
36
     */
37
    protected $computopEntityManager;
38
39
    /**
40
     * @param \SprykerEco\Zed\Computop\ComputopConfig $computopConfig
41
     * @param \SprykerEco\Zed\Computop\Persistence\ComputopRepositoryInterface $computopRepository
42
     * @param \SprykerEco\Zed\Computop\Persistence\ComputopEntityManagerInterface $computopEntityManager
43
     */
44
    public function __construct(
45
        ComputopConfig $computopConfig,
46
        ComputopRepositoryInterface $computopRepository,
47
        ComputopEntityManagerInterface $computopEntityManager
48
    ) {
49
        $this->computopConfig = $computopConfig;
50
        $this->computopRepository = $computopRepository;
51
        $this->computopEntityManager = $computopEntityManager;
52
    }
53
54
    /**
55
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
56
     *
57
     * @return \Generated\Shared\Transfer\QuoteTransfer
58
     */
59
    public function save(QuoteTransfer $quoteTransfer): QuoteTransfer
60
    {
61
        $computopPayPalExpressInitResponseTransfer = $quoteTransfer->getPaymentOrFail()
62
            ->getComputopPayPalExpressOrFail()
63
            ->getPayPalExpressInitResponseOrFail();
64
65
        $computopPaymentComputopTransfer = $this->computopRepository
66
            ->findComputopPaymentByComputopTransId(
67
                $computopPayPalExpressInitResponseTransfer->getHeader()->getTransId(),
68
            );
69
70
        if ($computopPaymentComputopTransfer === null) {
71
            return $quoteTransfer;
72
        }
73
74
        if ($computopPayPalExpressInitResponseTransfer->getHeader()->getIsSuccess()) {
75
            $this->getTransactionHandler()->handleTransaction(
76
                function () use ($computopPaymentComputopTransfer, $computopPayPalExpressInitResponseTransfer) {
77
                    $this->executeSavePaymentComputopDataTransaction($computopPaymentComputopTransfer, $computopPayPalExpressInitResponseTransfer);
78
                },
79
            );
80
        }
81
82
        return $quoteTransfer;
83
    }
84
85
    /**
86
     * @param \Generated\Shared\Transfer\ComputopPaymentComputopTransfer $computopPaymentComputopTransfer
87
     * @param \Generated\Shared\Transfer\ComputopPayPalExpressInitResponseTransfer $computopPayPalExpressInitResponseTransfer
88
     *
89
     * @return void
90
     */
91
    protected function executeSavePaymentComputopDataTransaction(
92
        ComputopPaymentComputopTransfer $computopPaymentComputopTransfer,
93
        ComputopPayPalExpressInitResponseTransfer $computopPayPalExpressInitResponseTransfer
94
    ): void {
95
        $this->savePaymentComputopEntity($computopPaymentComputopTransfer, $computopPayPalExpressInitResponseTransfer);
96
        $this->savePaymentComputopDetailEntity($computopPaymentComputopTransfer, $computopPayPalExpressInitResponseTransfer);
97
        $this->savePaymentComputopOrderItemEntities($computopPaymentComputopTransfer);
98
    }
99
100
    /**
101
     * @param \Generated\Shared\Transfer\ComputopPaymentComputopTransfer $computopPaymentComputopTransfer
102
     * @param \Generated\Shared\Transfer\ComputopPayPalExpressInitResponseTransfer $computopPayPalExpressInitResponseTransfer
103
     *
104
     * @return void
105
     */
106
    protected function savePaymentComputopEntity(
107
        ComputopPaymentComputopTransfer $computopPaymentComputopTransfer,
108
        ComputopPayPalExpressInitResponseTransfer $computopPayPalExpressInitResponseTransfer
109
    ): void {
110
        $computopPaymentComputopTransfer->setPayId($computopPayPalExpressInitResponseTransfer->getHeader()->getPayId());
111
        $computopPaymentComputopTransfer->setXId($computopPayPalExpressInitResponseTransfer->getHeader()->getXId());
112
113
        $this->computopEntityManager->updateComputopPayment($computopPaymentComputopTransfer);
114
    }
115
116
    /**
117
     * @param \Generated\Shared\Transfer\ComputopPaymentComputopTransfer $computopPaymentComputopTransfer
118
     * @param \Generated\Shared\Transfer\ComputopPayPalExpressInitResponseTransfer $computopPayPalExpressInitResponseTransfer
119
     *
120
     * @return void
121
     */
122
    protected function savePaymentComputopDetailEntity(
123
        ComputopPaymentComputopTransfer $computopPaymentComputopTransfer,
124
        ComputopPayPalExpressInitResponseTransfer $computopPayPalExpressInitResponseTransfer
125
    ): void {
126
        $computopPaymentComputopDetailTransfer = $this->computopRepository
127
            ->findComputopPaymentDetail($computopPaymentComputopTransfer);
128
129
        if ($computopPaymentComputopDetailTransfer === null) {
130
            return;
131
        }
132
133
        $computopPaymentComputopDetailTransfer->fromArray($computopPayPalExpressInitResponseTransfer->toArray(), true);
134
135
        $this->computopEntityManager->updateComputopPaymentDetail($computopPaymentComputopDetailTransfer);
136
    }
137
138
    /**
139
     * @param \Generated\Shared\Transfer\ComputopPaymentComputopTransfer $computopPaymentComputopTransfer
140
     *
141
     * @return void
142
     */
143
    protected function savePaymentComputopOrderItemEntities(
144
        ComputopPaymentComputopTransfer $computopPaymentComputopTransfer
145
    ): void {
146
        $salesOrderItemCollectionTransfer = $this->computopRepository
147
            ->getComputopSalesOrderItemCollection($computopPaymentComputopTransfer);
148
149
        $computopPaymentComputopOrderItemCollection = $this->computopRepository
150
            ->getComputopPaymentComputopOrderItemCollection($computopPaymentComputopTransfer);
151
152
        foreach ($salesOrderItemCollectionTransfer->getComputopSalesOrderItems() as $computopSalesOrderItem) {
153
            $this->updateComputopSalesOrderItemCollection(
154
                $computopPaymentComputopOrderItemCollection,
155
                $computopSalesOrderItem,
156
            );
157
        }
158
    }
159
160
    /**
161
     * @param \Generated\Shared\Transfer\ComputopPaymentComputopOrderItemCollectionTransfer $computopPaymentComputopOrderItemCollection
162
     * @param \Generated\Shared\Transfer\ComputopSalesOrderItemTransfer $computopSalesOrderItem
163
     *
164
     * @return void
165
     */
166
    protected function updateComputopSalesOrderItemCollection(
167
        ComputopPaymentComputopOrderItemCollectionTransfer $computopPaymentComputopOrderItemCollection,
168
        ComputopSalesOrderItemTransfer $computopSalesOrderItem
169
    ): void {
170
        foreach ($computopPaymentComputopOrderItemCollection->getComputopPaymentComputopOrderItems() as $computopPaymentComputopOrderItem) {
171
            if ($computopSalesOrderItem->getIdSalesOrderItem() !== $computopPaymentComputopOrderItem->getFkSalesOrderItem()) {
172
                continue;
173
            }
174
            $computopPaymentComputopOrderItem->setStatus($this->computopConfig->getOmsStatusInitialized());
175
            $this->computopEntityManager->updateComputopPaymentComputopOrderItem($computopPaymentComputopOrderItem);
176
        }
177
    }
178
}
179