Completed
Push — master ( eaf845...272b76 )
by Oleksandr
13s queued 10s
created

CrefoPayCheckoutPostSaveHookSaver   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 114
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setOrderItemsStatus() 0 13 1
A getPaymentCrefoPayOrderItemCollectionTransfer() 0 8 1
A __construct() 0 8 1
A savePaymentEntities() 0 12 2
A getPaymentCrefoPayTransfer() 0 20 2
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Zed\CrefoPay\Business\Hook\Checkout\Saver;
9
10
use ArrayObject;
11
use Generated\Shared\Transfer\CrefoPayApiRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...foPayApiRequestTransfer 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\CrefoPayApiResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...oPayApiResponseTransfer 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\PaymentCrefoPayOrderItemCollectionTransfer;
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...
14
use Generated\Shared\Transfer\PaymentCrefoPayOrderItemTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...efoPayOrderItemTransfer 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 Generated\Shared\Transfer\PaymentCrefoPayTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\PaymentCrefoPayTransfer 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...
16
use SprykerEco\Zed\CrefoPay\Business\Reader\CrefoPayReaderInterface;
17
use SprykerEco\Zed\CrefoPay\Business\Writer\CrefoPayWriterInterface;
18
use SprykerEco\Zed\CrefoPay\CrefoPayConfig;
19
20
class CrefoPayCheckoutPostSaveHookSaver implements CrefoPayCheckoutHookSaverInterface
21
{
22
    /**
23
     * @var \SprykerEco\Zed\CrefoPay\Business\Reader\CrefoPayReaderInterface
24
     */
25
    protected $reader;
26
27
    /**
28
     * @var \SprykerEco\Zed\CrefoPay\Business\Writer\CrefoPayWriterInterface
29
     */
30
    protected $writer;
31
32
    /**
33
     * @var \SprykerEco\Zed\CrefoPay\CrefoPayConfig
34
     */
35
    protected $config;
36
37
    /**
38
     * @param \SprykerEco\Zed\CrefoPay\Business\Reader\CrefoPayReaderInterface $reader
39
     * @param \SprykerEco\Zed\CrefoPay\Business\Writer\CrefoPayWriterInterface $writer
40
     * @param \SprykerEco\Zed\CrefoPay\CrefoPayConfig $config
41
     */
42
    public function __construct(
43
        CrefoPayReaderInterface $reader,
44
        CrefoPayWriterInterface $writer,
45
        CrefoPayConfig $config
46
    ) {
47
        $this->reader = $reader;
48
        $this->writer = $writer;
49
        $this->config = $config;
50
    }
51
52
    /**
53
     * @param \Generated\Shared\Transfer\CrefoPayApiRequestTransfer $requestTransfer
54
     * @param \Generated\Shared\Transfer\CrefoPayApiResponseTransfer $responseTransfer
55
     *
56
     * @return void
57
     */
58
    public function savePaymentEntities(
59
        CrefoPayApiRequestTransfer $requestTransfer,
60
        CrefoPayApiResponseTransfer $responseTransfer
61
    ): void {
62
        if (!$responseTransfer->getIsSuccess()) {
63
            return;
64
        }
65
66
        $this->writer->updatePaymentEntities(
67
            $this->getPaymentCrefoPayOrderItemCollectionTransfer($requestTransfer),
68
            $this->getPaymentCrefoPayTransfer($requestTransfer, $responseTransfer),
69
            $responseTransfer->getCrefoPayApiLogId()
70
        );
71
    }
72
73
    /**
74
     * @param \Generated\Shared\Transfer\CrefoPayApiRequestTransfer $requestTransfer
75
     * @param \Generated\Shared\Transfer\CrefoPayApiResponseTransfer $responseTransfer
76
     *
77
     * @return \Generated\Shared\Transfer\PaymentCrefoPayTransfer|null
78
     */
79
    protected function getPaymentCrefoPayTransfer(
80
        CrefoPayApiRequestTransfer $requestTransfer,
81
        CrefoPayApiResponseTransfer $responseTransfer
82
    ): ?PaymentCrefoPayTransfer {
83
        $paymentCrefoPayTransfer = $this->reader
84
            ->getPaymentCrefoPayByCrefoPayOrderId(
85
                $requestTransfer->getReserveRequest()->getOrderID()
86
            );
87
88
        $paymentCrefoPayTransfer->setAuthorizedAmount(
89
            $requestTransfer->getReserveRequest()->getAmount()->getAmount()
90
        );
91
92
        if ($responseTransfer->getReserveResponse()->getAdditionalData() !== null) {
93
            $paymentCrefoPayTransfer->setAdditionalData(
94
                $responseTransfer->getReserveResponse()->getAdditionalData()->serialize()
95
            );
96
        }
97
98
        return $paymentCrefoPayTransfer;
99
    }
100
101
    /**
102
     * @param \Generated\Shared\Transfer\CrefoPayApiRequestTransfer $requestTransfer
103
     *
104
     * @return \Generated\Shared\Transfer\PaymentCrefoPayOrderItemCollectionTransfer
105
     */
106
    protected function getPaymentCrefoPayOrderItemCollectionTransfer(CrefoPayApiRequestTransfer $requestTransfer): PaymentCrefoPayOrderItemCollectionTransfer
107
    {
108
        $paymentCrefoPayOrderItemCollection = $this->reader
109
            ->getPaymentCrefoPayOrderItemCollectionByCrefoPayOrderId(
110
                $requestTransfer->getReserveRequest()->getOrderID()
111
            );
112
113
        return $this->setOrderItemsStatus($paymentCrefoPayOrderItemCollection);
114
    }
115
116
    /**
117
     * @param \Generated\Shared\Transfer\PaymentCrefoPayOrderItemCollectionTransfer $paymentCrefoPayOrderItemCollection
118
     *
119
     * @return \Generated\Shared\Transfer\PaymentCrefoPayOrderItemCollectionTransfer
120
     */
121
    protected function setOrderItemsStatus(
122
        PaymentCrefoPayOrderItemCollectionTransfer $paymentCrefoPayOrderItemCollection
123
    ): PaymentCrefoPayOrderItemCollectionTransfer {
124
        $status = $this->config->getOmsStatusReserved();
125
        $paymentCrefoPayOrderItems = array_map(
126
            function (PaymentCrefoPayOrderItemTransfer $paymentCrefoPayOrderItemTransfer) use ($status) {
127
                return $paymentCrefoPayOrderItemTransfer->setStatus($status);
128
            },
129
            $paymentCrefoPayOrderItemCollection->getCrefoPayOrderItems()->getArrayCopy()
130
        );
131
132
        return $paymentCrefoPayOrderItemCollection
133
            ->setCrefoPayOrderItems(new ArrayObject($paymentCrefoPayOrderItems));
134
    }
135
}
136