Passed
Push — feature/eco-3656/eco-3658-enab... ( c68c7e...4d2e5e )
by
unknown
20:30
created

savePaymentComputopDetailEntity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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