Passed
Pull Request — feature/eco-3656/dev-paypal-ex... (#40)
by
unknown
13:38 queued 06:09
created

PayPalExpressResponseSaver   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
eloc 42
dl 0
loc 148
rs 10
c 2
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A savePaymentComputopDetailEntity() 0 10 1
A savePaymentComputopEntity() 0 8 1
A __construct() 0 8 1
A save() 0 20 2
A updateComputopSalesOrderItem() 0 10 3
A executeSavePaymentComputopDataTransaction() 0 7 1
A savePaymentComputopOrderItemsEntities() 0 13 2
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->getPayment()
62
            ->getComputopPayPalExpress()
63
            ->getPayPalExpressInitResponse();
64
65
        $computopPaymentComputopTransfer = $this->computopRepository
66
            ->getComputopPaymentByComputopTransId(
67
                $computopPayPalExpressInitResponseTransfer->getHeader()->getTransId()
68
            );
69
70
        if ($computopPayPalExpressInitResponseTransfer->getHeader()->getIsSuccess()) {
71
            $this->getTransactionHandler()->handleTransaction(
72
                function () use ($computopPaymentComputopTransfer, $computopPayPalExpressInitResponseTransfer) {
73
                    $this->executeSavePaymentComputopDataTransaction($computopPaymentComputopTransfer, $computopPayPalExpressInitResponseTransfer);
74
                }
75
            );
76
        }
77
78
        return $quoteTransfer;
79
    }
80
81
    /**
82
     * @param \Generated\Shared\Transfer\ComputopPaymentComputopTransfer $computopPaymentComputopTransfer
83
     * @param \Generated\Shared\Transfer\ComputopPayPalExpressInitResponseTransfer $computopPayPalExpressInitResponseTransfer
84
     *
85
     * @return void
86
     */
87
    protected function executeSavePaymentComputopDataTransaction(
88
        ComputopPaymentComputopTransfer $computopPaymentComputopTransfer,
89
        ComputopPayPalExpressInitResponseTransfer $computopPayPalExpressInitResponseTransfer
90
    ): void {
91
        $this->savePaymentComputopEntity($computopPaymentComputopTransfer, $computopPayPalExpressInitResponseTransfer);
92
        $this->savePaymentComputopDetailEntity($computopPaymentComputopTransfer, $computopPayPalExpressInitResponseTransfer);
93
        $this->savePaymentComputopOrderItemsEntities($computopPaymentComputopTransfer);
94
    }
95
96
    /**
97
     * @param \Generated\Shared\Transfer\ComputopPaymentComputopTransfer $computopPaymentComputopTransfer
98
     * @param \Generated\Shared\Transfer\ComputopPayPalExpressInitResponseTransfer $computopPayPalExpressInitResponseTransfer
99
     *
100
     * @return void
101
     */
102
    protected function savePaymentComputopEntity(
103
        ComputopPaymentComputopTransfer $computopPaymentComputopTransfer,
104
        ComputopPayPalExpressInitResponseTransfer $computopPayPalExpressInitResponseTransfer
105
    ): void {
106
        $computopPaymentComputopTransfer->setPayId($computopPayPalExpressInitResponseTransfer->getHeader()->getPayId());
107
        $computopPaymentComputopTransfer->setXId($computopPayPalExpressInitResponseTransfer->getHeader()->getXId());
108
109
        $this->computopEntityManager->saveComputopPayment($computopPaymentComputopTransfer);
110
    }
111
112
    /**
113
     * @param \Generated\Shared\Transfer\ComputopPaymentComputopTransfer $computopPaymentComputopTransfer
114
     * @param \Generated\Shared\Transfer\ComputopPayPalExpressInitResponseTransfer $computopPayPalExpressInitResponseTransfer
115
     *
116
     * @return void
117
     */
118
    protected function savePaymentComputopDetailEntity(
119
        ComputopPaymentComputopTransfer $computopPaymentComputopTransfer,
120
        ComputopPayPalExpressInitResponseTransfer $computopPayPalExpressInitResponseTransfer
121
    ): void {
122
        $computopPaymentComputopDetailTransfer = $this->computopRepository
123
            ->getComputopPaymentDetail($computopPaymentComputopTransfer);
124
125
        $computopPaymentComputopDetailTransfer->fromArray($computopPayPalExpressInitResponseTransfer->toArray(), true);
126
127
        $this->computopEntityManager->updateComputopPaymentDetail($computopPaymentComputopDetailTransfer);
128
    }
129
130
    /**
131
     * @param \Generated\Shared\Transfer\ComputopPaymentComputopTransfer $computopPaymentComputopTransfer
132
     *
133
     * @return void
134
     */
135
    protected function savePaymentComputopOrderItemsEntities(
136
        ComputopPaymentComputopTransfer $computopPaymentComputopTransfer
137
    ): void {
138
        $salesOrderItemsCollectionTransfer = $this->computopRepository
139
            ->getComputopSalesOrderItemsCollection($computopPaymentComputopTransfer);
140
141
        $computopPaymentComputopOrderItemsCollection = $this->computopRepository
142
            ->getComputopPaymentComputopOrderItemsCollection($computopPaymentComputopTransfer);
143
144
        foreach ($salesOrderItemsCollectionTransfer->getComputopSalesOrderItems() as $computopSalesOrderItem) {
145
            $this->updateComputopSalesOrderItem(
146
                $computopPaymentComputopOrderItemsCollection,
147
                $computopSalesOrderItem
148
            );
149
        }
150
    }
151
152
    /**
153
     * @param \Generated\Shared\Transfer\ComputopPaymentComputopOrderItemCollectionTransfer $computopPaymentComputopOrderItemsCollection
154
     * @param \Generated\Shared\Transfer\ComputopSalesOrderItemTransfer $computopSalesOrderItem
155
     *
156
     * @return void
157
     */
158
    protected function updateComputopSalesOrderItem(
159
        ComputopPaymentComputopOrderItemCollectionTransfer $computopPaymentComputopOrderItemsCollection,
160
        ComputopSalesOrderItemTransfer $computopSalesOrderItem
161
    ): void {
162
        foreach ($computopPaymentComputopOrderItemsCollection->getComputopPaymentComputopOrderItems() as $computopPaymentComputopOrderItem) {
163
            if ($computopSalesOrderItem->getIdSalesOrderItem() !== $computopPaymentComputopOrderItem->getFkSalesOrderItem()) {
164
                continue;
165
            }
166
            $computopPaymentComputopOrderItem->setStatus($this->computopConfig->getOmsStatusInitialized());
167
            $this->computopEntityManager->updateComputopPaymentComputopOrderItem($computopPaymentComputopOrderItem);
168
        }
169
    }
170
}
171