OrderManager::saveOrderPayment()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
c 0
b 0
f 0
dl 0
loc 24
rs 9.7666
cc 2
nc 2
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\Order;
9
10
use ArrayObject;
11
use Generated\Shared\Transfer\PaymentTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\PaymentTransfer 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 Generated\Shared\Transfer\SaveOrderTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\SaveOrderTransfer 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 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...
15
use Orm\Zed\Computop\Persistence\SpyPaymentComputopDetail;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Computop\Persist...pyPaymentComputopDetail 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 Orm\Zed\Computop\Persistence\SpyPaymentComputopOrderItem;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Computop\Persist...aymentComputopOrderItem 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...
17
use Spryker\Zed\Kernel\Persistence\EntityManager\TransactionTrait;
18
use SprykerEco\Shared\Computop\ComputopConfig as ComputopSharedConfig;
19
use SprykerEco\Zed\Computop\Business\Exception\ComputopMethodMapperException;
20
use SprykerEco\Zed\Computop\Business\Order\Mapper\MapperInterface;
21
use SprykerEco\Zed\Computop\ComputopConfig;
22
23
class OrderManager implements OrderManagerInterface
24
{
25
    use TransactionTrait;
26
27
    /**
28
     * @var \SprykerEco\Zed\Computop\Business\Order\OrderManagerInterface
29
     */
30
    protected $mappers;
31
32
    /**
33
     * @var \SprykerEco\Zed\Computop\Business\Order\OrderManagerInterface
34
     */
35
    protected $activeMapper;
36
37
    /**
38
     * @var \Spryker\Shared\Kernel\Transfer\TransferInterface
39
     */
40
    protected $computopTransfer;
41
42
    /**
43
     * @var \Spryker\Shared\Kernel\Transfer\TransferInterface
44
     */
45
    protected $computopResponseTransfer;
46
47
    /**
48
     * @var \SprykerEco\Zed\Computop\ComputopConfig
49
     */
50
    protected $config;
51
52
    /**
53
     * @var string
54
     */
55
    protected $methodName;
56
57
    /**
58
     * @param \SprykerEco\Zed\Computop\ComputopConfig $config
59
     */
60
    public function __construct(ComputopConfig $config)
61
    {
62
        $this->config = $config;
63
    }
64
65
    /**
66
     * @param \SprykerEco\Zed\Computop\Business\Order\Mapper\MapperInterface $mapper
67
     *
68
     * @return void
69
     */
70
    public function registerMapper(MapperInterface $mapper)
71
    {
72
        $this->mappers[$mapper->getMethodName()] = $mapper;
73
    }
74
75
    /**
76
     * @param string $methodName
77
     *
78
     * @throws \SprykerEco\Zed\Computop\Business\Exception\ComputopMethodMapperException
79
     *
80
     * @return \SprykerEco\Zed\Computop\Business\Order\Mapper\MapperInterface
81
     */
82
    protected function getMethodMapper($methodName)
83
    {
84
        if (isset($this->mappers[$methodName]) === false) {
85
            throw new ComputopMethodMapperException('The method mapper is not registered.');
86
        }
87
88
        return $this->mappers[$methodName];
89
    }
90
91
    /**
92
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
93
     * @param \Generated\Shared\Transfer\SaveOrderTransfer $saveOrderTransfer
94
     *
95
     * @return void
96
     */
97
    public function saveOrderPayment(QuoteTransfer $quoteTransfer, SaveOrderTransfer $saveOrderTransfer)
98
    {
99
        if ($quoteTransfer->getPayment()->getPaymentProvider() !== ComputopSharedConfig::PROVIDER_NAME) {
100
            return;
101
        }
102
103
        $this->methodName = $quoteTransfer->getPayment()->getPaymentMethod();
104
        $this->activeMapper = $this->getMethodMapper($quoteTransfer->getPayment()->getPaymentMethod());
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMethodMapper($...()->getPaymentMethod()) of type SprykerEco\Zed\Computop\...\Mapper\MapperInterface is incompatible with the declared type SprykerEco\Zed\Computop\...r\OrderManagerInterface of property $activeMapper.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
105
        $this->computopTransfer = $this->activeMapper->getComputopTransfer($quoteTransfer->getPayment());
106
        $this->computopResponseTransfer = $this->activeMapper->getComputopResponseTransfer($quoteTransfer->getPayment());
107
108
        $paymentEntity = $this->savePaymentForOrder(
109
            $quoteTransfer->getPayment(),
110
            $saveOrderTransfer
111
        );
112
113
        $this->savePaymentDetailForOrder(
114
            $quoteTransfer->getPayment(),
115
            $paymentEntity
116
        );
117
118
        $this->savePaymentForOrderItems(
119
            $saveOrderTransfer->getOrderItems(),
120
            $paymentEntity->getIdPaymentComputop()
121
        );
122
    }
123
124
    /**
125
     * @param \Generated\Shared\Transfer\PaymentTransfer $paymentTransfer
126
     * @param \Generated\Shared\Transfer\SaveOrderTransfer $saveOrderTransfer
127
     *
128
     * @return \Orm\Zed\Computop\Persistence\SpyPaymentComputop
129
     */
130
    protected function savePaymentForOrder(PaymentTransfer $paymentTransfer, SaveOrderTransfer $saveOrderTransfer)
131
    {
132
        $paymentEntity = new SpyPaymentComputop();
133
134
        $paymentEntity->setClientIp($this->computopTransfer->getClientIp());
0 ignored issues
show
Bug introduced by
The method getClientIp() does not exist on Spryker\Shared\Kernel\Transfer\TransferInterface. ( Ignorable by Annotation )

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

134
        $paymentEntity->setClientIp($this->computopTransfer->/** @scrutinizer ignore-call */ getClientIp());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
135
        $paymentEntity->setPaymentMethod($paymentTransfer->getPaymentMethod());
136
        $paymentEntity->setReference($saveOrderTransfer->getOrderReference());
137
        $paymentEntity->setFkSalesOrder($saveOrderTransfer->getIdSalesOrder());
138
        $paymentEntity->setTransId($this->computopTransfer->getTransId());
0 ignored issues
show
Bug introduced by
The method getTransId() does not exist on Spryker\Shared\Kernel\Transfer\TransferInterface. ( Ignorable by Annotation )

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

138
        $paymentEntity->setTransId($this->computopTransfer->/** @scrutinizer ignore-call */ getTransId());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
139
        $paymentEntity->setPayId($this->computopTransfer->getPayId());
0 ignored issues
show
Bug introduced by
The method getPayId() does not exist on Spryker\Shared\Kernel\Transfer\TransferInterface. ( Ignorable by Annotation )

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

139
        $paymentEntity->setPayId($this->computopTransfer->/** @scrutinizer ignore-call */ getPayId());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
140
        $paymentEntity->setReqId($this->computopTransfer->getReqId());
0 ignored issues
show
Bug introduced by
The method getReqId() does not exist on Spryker\Shared\Kernel\Transfer\TransferInterface. ( Ignorable by Annotation )

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

140
        $paymentEntity->setReqId($this->computopTransfer->/** @scrutinizer ignore-call */ getReqId());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
141
142
        if ($this->isPaymentMethodEasyCredit()) {
143
            $paymentEntity->setXId($this->computopResponseTransfer->getHeader()->getXId());
0 ignored issues
show
Bug introduced by
The method getHeader() does not exist on Spryker\Shared\Kernel\Transfer\TransferInterface. ( Ignorable by Annotation )

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

143
            $paymentEntity->setXId($this->computopResponseTransfer->/** @scrutinizer ignore-call */ getHeader()->getXId());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
144
        }
145
146
        $paymentEntity->save();
147
148
        return $paymentEntity;
149
    }
150
151
    /**
152
     * @param \Generated\Shared\Transfer\PaymentTransfer $paymentTransfer
153
     * @param \Orm\Zed\Computop\Persistence\SpyPaymentComputop $paymentEntity
154
     *
155
     * @return \Orm\Zed\Computop\Persistence\SpyPaymentComputopDetail
156
     */
157
    protected function savePaymentDetailForOrder(PaymentTransfer $paymentTransfer, SpyPaymentComputop $paymentEntity)
158
    {
159
        $paymentDetailEntity = new SpyPaymentComputopDetail();
160
161
        $paymentDetailEntity->fromArray($this->activeMapper->getPaymentDetailsArray($paymentTransfer));
0 ignored issues
show
Bug introduced by
The method getPaymentDetailsArray() does not exist on SprykerEco\Zed\Computop\...r\OrderManagerInterface. ( Ignorable by Annotation )

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

161
        $paymentDetailEntity->fromArray($this->activeMapper->/** @scrutinizer ignore-call */ getPaymentDetailsArray($paymentTransfer));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
162
        $paymentDetailEntity->setIdPaymentComputop($paymentEntity->getIdPaymentComputop());
163
164
        if ($this->isPaymentMethodEasyCredit()) {
165
            $paymentDetailEntity->fromArray($this->computopResponseTransfer->toArray());
166
        }
167
168
        $paymentDetailEntity->save();
169
170
        return $paymentDetailEntity;
171
    }
172
173
    /**
174
     * @param \ArrayObject|\Generated\Shared\Transfer\ItemTransfer[] $orderItemTransfers
175
     * @param int $idPayment
176
     *
177
     * @return void
178
     */
179
    protected function savePaymentForOrderItems(ArrayObject $orderItemTransfers, $idPayment)
180
    {
181
        foreach ($orderItemTransfers as $orderItemTransfer) {
182
            $paymentOrderItemEntity = new SpyPaymentComputopOrderItem();
183
184
            $paymentOrderItemEntity
185
                ->setFkPaymentComputop($idPayment)
186
                ->setFkSalesOrderItem($orderItemTransfer->getIdSalesOrderItem());
187
            $paymentOrderItemEntity->setStatus($this->config->getOmsStatusNew());
188
189
            if ($this->isPaymentMethodEasyCredit()) {
190
                $paymentOrderItemEntity->setStatus($this->config->getOmsStatusInitialized());
191
            }
192
193
            $paymentOrderItemEntity->save();
194
        }
195
    }
196
197
    /**
198
     * @return bool
199
     */
200
    protected function isPaymentMethodEasyCredit(): bool
201
    {
202
        return $this->methodName === ComputopSharedConfig::PAYMENT_METHOD_EASY_CREDIT;
203
    }
204
}
205