Passed
Push — feature/eco-3656/eco-3658-enab... ( 4d2e5e...c70d81 )
by
unknown
06:33
created

updateComputopSalesOrderItem()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 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\Dependency\Facade\ComputopToOmsFacadeInterface;
18
use SprykerEco\Zed\Computop\Persistence\ComputopEntityManagerInterface;
19
use SprykerEco\Zed\Computop\Persistence\ComputopRepositoryInterface;
20
21
class PayPalExpressResponseSaver implements InitResponseSaverInterface
22
{
23
    use TransactionTrait;
24
25
    /**
26
     * @var \SprykerEco\Zed\Computop\Dependency\Facade\ComputopToOmsFacadeInterface
27
     */
28
    protected $omsFacade;
29
30
    /**
31
     * @var \SprykerEco\Zed\Computop\ComputopConfig
32
     */
33
    protected $computopConfig;
34
35
    /**
36
     * @var \SprykerEco\Zed\Computop\Persistence\ComputopRepositoryInterface
37
     */
38
    protected $computopRepository;
39
40
    /**
41
     * @var \SprykerEco\Zed\Computop\Persistence\ComputopEntityManagerInterface
42
     */
43
    protected $computopEntityManager;
44
45
    /**
46
     * @param \SprykerEco\Zed\Computop\Dependency\Facade\ComputopToOmsFacadeInterface $omsFacade
47
     * @param \SprykerEco\Zed\Computop\ComputopConfig $computopConfig
48
     * @param \SprykerEco\Zed\Computop\Persistence\ComputopRepositoryInterface $computopRepository
49
     * @param \SprykerEco\Zed\Computop\Persistence\ComputopEntityManagerInterface $computopEntityManager
50
     */
51
    public function __construct(
52
        ComputopToOmsFacadeInterface $omsFacade,
53
        ComputopConfig $computopConfig,
54
        ComputopRepositoryInterface $computopRepository,
55
        ComputopEntityManagerInterface $computopEntityManager
56
    ) {
57
        $this->omsFacade = $omsFacade;
58
        $this->computopConfig = $computopConfig;
59
        $this->computopRepository = $computopRepository;
60
        $this->computopEntityManager = $computopEntityManager;
61
    }
62
63
    /**
64
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
65
     *
66
     * @return \Generated\Shared\Transfer\QuoteTransfer
67
     */
68
    public function save(QuoteTransfer $quoteTransfer): QuoteTransfer
69
    {
70
        $computopPayPalExpressInitResponseTransfer = $quoteTransfer->getPayment()
71
            ->getComputopPayPalExpress()
72
            ->getPayPalExpressInitResponse();
73
74
        $computopPaymentComputopTransfer = $this->computopRepository
75
            ->getComputopPaymentByComputopTransId(
76
                $computopPayPalExpressInitResponseTransfer->getHeader()->getTransId()
77
            );
78
79
        if ($computopPayPalExpressInitResponseTransfer->getHeader()->getIsSuccess()) {
80
            $this->getTransactionHandler()->handleTransaction(
81
                function () use ($computopPaymentComputopTransfer, $computopPayPalExpressInitResponseTransfer) {
82
                    $this->executeSavePaymentComputopDataTransaction($computopPaymentComputopTransfer, $computopPayPalExpressInitResponseTransfer);
83
                }
84
            );
85
        }
86
87
        return $quoteTransfer;
88
    }
89
90
    /**
91
     * @param \Generated\Shared\Transfer\ComputopPaymentComputopTransfer $computopPaymentComputopTransfer
92
     * @param \Generated\Shared\Transfer\ComputopPayPalExpressInitResponseTransfer $computopPayPalExpressInitResponseTransfer
93
     *
94
     * @return void
95
     */
96
    protected function executeSavePaymentComputopDataTransaction(
97
        ComputopPaymentComputopTransfer $computopPaymentComputopTransfer,
98
        ComputopPayPalExpressInitResponseTransfer $computopPayPalExpressInitResponseTransfer
99
    ): void {
100
        $this->savePaymentComputopEntity($computopPaymentComputopTransfer, $computopPayPalExpressInitResponseTransfer);
101
        $this->savePaymentComputopDetailEntity($computopPaymentComputopTransfer, $computopPayPalExpressInitResponseTransfer);
102
        $this->savePaymentComputopOrderItemsEntities($computopPaymentComputopTransfer);
103
    }
104
105
    /**
106
     * @param \Generated\Shared\Transfer\ComputopPaymentComputopTransfer $computopPaymentComputopTransfer
107
     * @param \Generated\Shared\Transfer\ComputopPayPalExpressInitResponseTransfer $computopPayPalExpressInitResponseTransfer
108
     *
109
     * @return void
110
     */
111
    protected function savePaymentComputopEntity(
112
        ComputopPaymentComputopTransfer $computopPaymentComputopTransfer,
113
        ComputopPayPalExpressInitResponseTransfer $computopPayPalExpressInitResponseTransfer
114
    ): void {
115
        $computopPaymentComputopTransfer->setPayId($computopPayPalExpressInitResponseTransfer->getHeader()->getPayId());
116
        $computopPaymentComputopTransfer->setXId($computopPayPalExpressInitResponseTransfer->getHeader()->getXId());
117
118
        $this->computopEntityManager->saveComputopPayment($computopPaymentComputopTransfer);
119
    }
120
121
    /**
122
     * @param \Generated\Shared\Transfer\ComputopPaymentComputopTransfer $computopPaymentComputopTransfer
123
     * @param \Generated\Shared\Transfer\ComputopPayPalExpressInitResponseTransfer $computopPayPalExpressInitResponseTransfer
124
     *
125
     * @return void
126
     */
127
    protected function savePaymentComputopDetailEntity(
128
        ComputopPaymentComputopTransfer $computopPaymentComputopTransfer,
129
        ComputopPayPalExpressInitResponseTransfer $computopPayPalExpressInitResponseTransfer
130
    ): void {
131
        $computopPaymentComputopDetailTransfer = $this->computopRepository
132
            ->getComputopPaymentDetail($computopPaymentComputopTransfer);
133
134
        $computopPaymentComputopDetailTransfer->fromArray($computopPayPalExpressInitResponseTransfer->toArray(), true);
135
136
        $this->computopEntityManager->saveComputopPaymentDetail($computopPaymentComputopDetailTransfer);
137
    }
138
139
    /**
140
     * @param \Generated\Shared\Transfer\ComputopPaymentComputopTransfer $computopPaymentComputopTransfer
141
     *
142
     * @return void
143
     */
144
    protected function savePaymentComputopOrderItemsEntities(
145
        ComputopPaymentComputopTransfer $computopPaymentComputopTransfer
146
    ): void {
147
        $salesOrderItemsCollectionTransfer = $this->computopRepository
148
            ->getComputopSalesOrderItemsCollection($computopPaymentComputopTransfer);
149
150
        $computopPaymentComputopOrderItemsCollection = $this->computopRepository
151
            ->getComputopPaymentComputopOrderItemsCollection($computopPaymentComputopTransfer);
152
153
        foreach ($salesOrderItemsCollectionTransfer->getComputopSalesOrderItems() as $computopSalesOrderItem) {
154
            $this->updateComputopSalesOrderItem(
155
                $computopPaymentComputopOrderItemsCollection,
156
                $computopSalesOrderItem
157
            );
158
        }
159
    }
160
161
    /**
162
     * @param \Generated\Shared\Transfer\ComputopPaymentComputopOrderItemCollectionTransfer $computopPaymentComputopOrderItemsCollection
163
     * @param \Generated\Shared\Transfer\ComputopSalesOrderItemTransfer $computopSalesOrderItem
164
     *
165
     * @return void
166
     */
167
    protected function updateComputopSalesOrderItem(
168
        ComputopPaymentComputopOrderItemCollectionTransfer $computopPaymentComputopOrderItemsCollection,
169
        ComputopSalesOrderItemTransfer $computopSalesOrderItem
170
    ): void {
171
        foreach ($computopPaymentComputopOrderItemsCollection->getComputopPaymentComputopOrderItems() as $computopPaymentComputopOrderItem) {
172
            if ($computopSalesOrderItem->getIdSalesOrderItem() !== $computopPaymentComputopOrderItem->getFkSalesOrderItem()) {
173
                continue;
174
            }
175
            $computopPaymentComputopOrderItem->setStatus($this->computopConfig->getOmsStatusInitialized());
176
            $this->computopEntityManager->updateComputopPaymentComputopOrderItem($computopPaymentComputopOrderItem);
177
        }
178
    }
179
}
180