Completed
Push — feature/eco-3656/eco-3658-enab... ( ab05e4...16d9cc )
by
unknown
06:10
created

PayPalExpressCompleteResponseSaver   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 35
dl 0
loc 119
rs 10
c 1
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A save() 0 11 1
A setPaymentEntity() 0 3 1
A getPaymentEntity() 0 3 1
A __construct() 0 9 1
A savePaymentComputopEntity() 0 6 1
A savePaymentComputopDetailEntity() 0 5 1
A savePaymentComputopOrderItemsEntities() 0 15 4
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\ComputopPayPalExpressCompleteResponseTransfer;
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\ComputopQueryContainerInterface;
18
19
class PayPalExpressCompleteResponseSaver implements CompleteResponseSaverInterface
20
{
21
    use TransactionTrait;
22
23
    /**
24
     * @var \SprykerEco\Zed\Computop\Persistence\ComputopQueryContainerInterface $queryContainer
25
     */
26
    protected $queryContainer;
27
28
    /**
29
     * @var \SprykerEco\Zed\Computop\Dependency\Facade\ComputopToOmsFacadeInterface
30
     */
31
    protected $omsFacade;
32
33
    /**
34
     * @var \Orm\Zed\Computop\Persistence\SpyPaymentComputop
35
     */
36
    protected $paymentEntity;
37
38
    /**
39
     * @var \SprykerEco\Zed\Computop\ComputopConfig
40
     */
41
    protected $config;
42
43
    /**
44
     * @param \SprykerEco\Zed\Computop\Persistence\ComputopQueryContainerInterface $queryContainer
45
     * @param \SprykerEco\Zed\Computop\Dependency\Facade\ComputopToOmsFacadeInterface $omsFacade
46
     * @param \SprykerEco\Zed\Computop\ComputopConfig $config
47
     */
48
    public function __construct(
49
        ComputopQueryContainerInterface $queryContainer,
50
        ComputopToOmsFacadeInterface $omsFacade,
51
        ComputopConfig $config
52
    )
53
    {
54
        $this->queryContainer = $queryContainer;
55
        $this->omsFacade = $omsFacade;
56
        $this->config = $config;
57
    }
58
59
    /**
60
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
61
     *
62
     * @return \Generated\Shared\Transfer\QuoteTransfer
63
     */
64
    public function save(QuoteTransfer $quoteTransfer): QuoteTransfer
65
    {
66
        $responseTransfer = $quoteTransfer->getPayment()->getComputopPayPalExpress()->getPayPalExpressCompleteResponse();
67
        $this->setPaymentEntity($responseTransfer->getHeader()->getTransId());
68
        $this->getTransactionHandler()->handleTransaction(
69
            function () use ($responseTransfer) {
70
                $this->savePaymentComputopOrderItemsEntities($responseTransfer);
71
            }
72
        );
73
74
        return $quoteTransfer;
75
    }
76
77
    /**
78
     * @param string $transactionId
79
     *
80
     * @return void
81
     */
82
    protected function setPaymentEntity(string $transactionId): void
83
    {
84
        $this->paymentEntity = $this->queryContainer->queryPaymentByTransactionId($transactionId)->findOne();
85
    }
86
87
    /**
88
     * @return \Orm\Zed\Computop\Persistence\SpyPaymentComputop
89
     */
90
    protected function getPaymentEntity(): SpyPaymentComputop
91
    {
92
        return $this->paymentEntity;
93
    }
94
95
    /**
96
     * @param \Generated\Shared\Transfer\ComputopPayPalExpressInitResponseTransfer $responseTransfer
97
     *
98
     * @return void
99
     */
100
    protected function savePaymentComputopEntity(ComputopPayPalExpressInitResponseTransfer $responseTransfer): void
101
    {
102
        $paymentEntity = $this->getPaymentEntity();
103
        $paymentEntity->setPayId($responseTransfer->getHeader()->getPayId());
104
        $paymentEntity->setXId($responseTransfer->getHeader()->getXId());
105
        $paymentEntity->save();
106
    }
107
108
    /**
109
     * @param \Generated\Shared\Transfer\ComputopPayPalExpressInitResponseTransfer $responseTransfer
110
     *
111
     * @return void
112
     */
113
    protected function savePaymentComputopDetailEntity(ComputopPayPalExpressInitResponseTransfer $responseTransfer)
114
    {
115
        $paymentEntityDetails = $this->getPaymentEntity()->getSpyPaymentComputopDetail();
116
        $paymentEntityDetails->fromArray($responseTransfer->toArray());
117
        $paymentEntityDetails->save();
118
    }
119
120
    /**
121
     * @return void
122
     */
123
    protected function savePaymentComputopOrderItemsEntities(ComputopPayPalExpressCompleteResponseTransfer $completeResponseTransfer)
124
    {
125
        $orderItems = $this
126
            ->queryContainer
127
            ->getSpySalesOrderItemsById($this->getPaymentEntity()->getFkSalesOrder())
128
            ->find();
129
130
        foreach ($orderItems as $selectedItem) {
131
            foreach ($this->getPaymentEntity()->getSpyPaymentComputopOrderItems() as $item) {
132
                if ($item->getFkSalesOrderItem() !== $selectedItem->getIdSalesOrderItem()) {
133
                    continue;
134
                }
135
                $item->setStatus($this->config->getOmsStatusInitialized());
136
                $item->setIsPaymentConfirmed($completeResponseTransfer->getIsSuccess());
137
                $item->save();
138
            }
139
        }
140
    }
141
}
142