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

PayPalExpressCompleteResponseSaver::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 22
rs 9.8666
c 1
b 0
f 0
cc 1
nc 1
nop 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\Complete;
9
10
use Generated\Shared\Transfer\ComputopApiPayPalExpressCompleteResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...ompleteResponseTransfer 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\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...
12
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...
13
use Orm\Zed\Computop\Persistence\SpyPaymentComputop;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Computop\Persistence\SpyPaymentComputop 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\ComputopQueryContainerInterface;
19
use SprykerEco\Zed\Computop\Persistence\ComputopRepositoryInterface;
20
21
class PayPalExpressCompleteResponseSaver implements CompleteResponseSaverInterface
22
{
23
    use TransactionTrait;
24
25
    /**
26
     * @var \SprykerEco\Zed\Computop\Persistence\ComputopQueryContainerInterface $queryContainer
27
     */
28
    protected $queryContainer;
29
30
    /**
31
     * @var \SprykerEco\Zed\Computop\Dependency\Facade\ComputopToOmsFacadeInterface
32
     */
33
    protected $omsFacade;
34
35
    /**
36
     * @var \Orm\Zed\Computop\Persistence\SpyPaymentComputop
37
     */
38
    protected $paymentEntity;
39
40
    /**
41
     * @var \SprykerEco\Zed\Computop\ComputopConfig
42
     */
43
    protected $computopConfig;
44
45
    /**
46
     * @var ComputopEntityManagerInterface
47
     */
48
    protected $computopEntityManager;
49
50
    /**
51
     * @var ComputopRepositoryInterface
52
     */
53
    protected $computopRepository;
54
55
    /**
56
     * @param \SprykerEco\Zed\Computop\Dependency\Facade\ComputopToOmsFacadeInterface $omsFacade
57
     * @param \SprykerEco\Zed\Computop\ComputopConfig $computopConfig
58
     * @param ComputopEntityManagerInterface $computopEntityManager
59
     * @param ComputopRepositoryInterface $computopRepository
60
     */
61
    public function __construct(
62
        ComputopToOmsFacadeInterface $omsFacade,
63
        ComputopConfig $computopConfig,
64
        ComputopEntityManagerInterface $computopEntityManager,
65
        ComputopRepositoryInterface $computopRepository
66
    )
67
    {
68
        $this->omsFacade = $omsFacade;
69
        $this->computopConfig = $computopConfig;
70
        $this->computopEntityManager = $computopEntityManager;
71
        $this->computopRepository = $computopRepository;
72
    }
73
74
    /**
75
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
76
     *
77
     * @return \Generated\Shared\Transfer\QuoteTransfer
78
     */
79
    public function save(QuoteTransfer $quoteTransfer): QuoteTransfer
80
    {
81
        $payPalExpressCompleteResponseTransfer = $quoteTransfer
82
            ->getPayment()
83
            ->getComputopPayPalExpress()
84
            ->getPayPalExpressCompleteResponse();
85
86
        $computopPaymentTransfer = $this->
87
        computopRepository
88
            ->getComputopPaymentByComputopTransId($payPalExpressCompleteResponseTransfer->getHeader()->getTransId());
89
90
91
        $this->setPaymentEntity($payPalExpressCompleteResponseTransfer->getHeader()->getTransId());
92
93
94
        $this->getTransactionHandler()->handleTransaction(
95
            function () use ($payPalExpressCompleteResponseTransfer, $computopPaymentTransfer): void {
96
                $this->savePaymentComputopOrderItemsEntities($payPalExpressCompleteResponseTransfer, $computopPaymentTransfer);
0 ignored issues
show
Unused Code introduced by
The call to SprykerEco\Zed\Computop\...topOrderItemsEntities() has too many arguments starting with $computopPaymentTransfer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

96
                $this->/** @scrutinizer ignore-call */ 
97
                       savePaymentComputopOrderItemsEntities($payPalExpressCompleteResponseTransfer, $computopPaymentTransfer);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
97
            }
98
        );
99
100
        return $quoteTransfer;
101
    }
102
103
    /**
104
     * @param string $transactionId
105
     *
106
     * @return void
107
     */
108
    protected function setPaymentEntity(string $transactionId): void
109
    {
110
        $this->paymentEntity = $this->queryContainer->queryPaymentByTransactionId($transactionId)->findOne();
111
    }
112
113
    /**
114
     * @return \Orm\Zed\Computop\Persistence\SpyPaymentComputop
115
     */
116
    protected function getPaymentEntity(): SpyPaymentComputop
117
    {
118
        return $this->paymentEntity;
119
    }
120
121
    /**
122
     * @param \Generated\Shared\Transfer\ComputopPayPalExpressInitResponseTransfer $responseTransfer
123
     *
124
     * @return void
125
     */
126
    protected function savePaymentComputopEntity(ComputopPayPalExpressInitResponseTransfer $responseTransfer): void
127
    {
128
        $paymentEntity = $this->getPaymentEntity();
129
        $paymentEntity->setPayId($responseTransfer->getHeader()->getPayId());
130
        $paymentEntity->setXId($responseTransfer->getHeader()->getXId());
131
        $paymentEntity->save();
132
    }
133
134
    /**
135
     * @param \Generated\Shared\Transfer\ComputopPayPalExpressInitResponseTransfer $responseTransfer
136
     *
137
     * @return void
138
     */
139
    protected function savePaymentComputopDetailEntity(ComputopPayPalExpressInitResponseTransfer $responseTransfer): void
140
    {
141
        $paymentEntityDetails = $this->getPaymentEntity()->getSpyPaymentComputopDetail();
142
        $paymentEntityDetails->fromArray($responseTransfer->toArray());
143
        $paymentEntityDetails->save();
144
    }
145
146
    /**
147
     * @param \Generated\Shared\Transfer\ComputopApiPayPalExpressCompleteResponseTransfer $completeResponseTransfer
148
     *
149
     * @return void
150
     */
151
    protected function savePaymentComputopOrderItemsEntities(ComputopApiPayPalExpressCompleteResponseTransfer $completeResponseTransfer): void
152
    {
153
        $orderItems = $this
154
            ->queryContainer
155
            ->getSpySalesOrderItemsById($this->getPaymentEntity()->getFkSalesOrder())
156
            ->find();
157
158
        foreach ($orderItems as $selectedItem) {
159
            foreach ($this->getPaymentEntity()->getSpyPaymentComputopOrderItems() as $item) {
160
                if ($item->getFkSalesOrderItem() !== $selectedItem->getIdSalesOrderItem()) {
161
                    continue;
162
                }
163
                $item->setStatus($this->computopConfig->getOmsStatusInitialized());
164
                $item->setIsPaymentConfirmed($completeResponseTransfer->getHeader()->getIsSuccess());
165
                $item->save();
166
            }
167
        }
168
    }
169
}
170