Passed
Push — feature/eco-2516-suite-compati... ( a91e93...c982bd )
by Volodymyr
04:13
created

OrderManager::createOrderExpenseTransfer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 9
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\Order;
9
10
use ArrayObject;
11
use Generated\Shared\Transfer\ExpenseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\ExpenseTransfer 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\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...
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 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...
15
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...
16
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...
17
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...
18
use Orm\Zed\Sales\Persistence\SpySalesExpense;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Sales\Persistence\SpySalesExpense 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...
19
use Spryker\Zed\Kernel\Persistence\EntityManager\TransactionTrait;
20
use SprykerEco\Shared\Computop\ComputopConfig as ComputopSharedConfig;
21
use SprykerEco\Zed\Computop\Business\Exception\ComputopMethodMapperException;
22
use SprykerEco\Zed\Computop\Business\Order\Mapper\MapperInterface;
23
use SprykerEco\Zed\Computop\ComputopConfig;
24
25
class OrderManager implements OrderManagerInterface
26
{
27
    use TransactionTrait;
28
29
    /**
30
     * @var \SprykerEco\Zed\Computop\Business\Order\OrderManagerInterface
31
     */
32
    protected $mappers;
33
34
    /**
35
     * @var \SprykerEco\Zed\Computop\Business\Order\OrderManagerInterface
36
     */
37
    protected $activeMapper;
38
39
    /**
40
     * @var \Spryker\Shared\Kernel\Transfer\TransferInterface
41
     */
42
    protected $computopTransfer;
43
44
    /**
45
     * @var \Spryker\Shared\Kernel\Transfer\TransferInterface
46
     */
47
    protected $computopResponseTransfer;
48
49
    /**
50
     * @var \SprykerEco\Zed\Computop\ComputopConfig
51
     */
52
    protected $config;
53
54
    /**
55
     * @var string
56
     */
57
    protected $methodName;
58
59
    /**
60
     * @param \SprykerEco\Zed\Computop\ComputopConfig $config
61
     */
62
    public function __construct(ComputopConfig $config)
63
    {
64
        $this->config = $config;
65
    }
66
67
    /**
68
     * @param \SprykerEco\Zed\Computop\Business\Order\Mapper\MapperInterface $mapper
69
     *
70
     * @return void
71
     */
72
    public function registerMapper(MapperInterface $mapper)
73
    {
74
        $this->mappers[$mapper->getMethodName()] = $mapper;
75
    }
76
77
    /**
78
     * @param string $methodName
79
     *
80
     * @throws \SprykerEco\Zed\Computop\Business\Exception\ComputopMethodMapperException
81
     *
82
     * @return \SprykerEco\Zed\Computop\Business\Order\Mapper\MapperInterface
83
     */
84
    protected function getMethodMapper($methodName)
85
    {
86
        if (isset($this->mappers[$methodName]) === false) {
87
            throw new ComputopMethodMapperException('The method mapper is not registered.');
88
        }
89
90
        return $this->mappers[$methodName];
91
    }
92
93
    /**
94
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
95
     * @param \Generated\Shared\Transfer\SaveOrderTransfer $saveOrderTransfer
96
     *
97
     * @return void
98
     */
99
    public function saveOrderPayment(QuoteTransfer $quoteTransfer, SaveOrderTransfer $saveOrderTransfer)
100
    {
101
        if ($quoteTransfer->getPayment()->getPaymentProvider() !== ComputopSharedConfig::PROVIDER_NAME) {
102
            return;
103
        }
104
105
        $this->methodName = $quoteTransfer->getPayment()->getPaymentMethod();
106
        $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...
107
        $this->computopTransfer = $this->activeMapper->getComputopTransfer($quoteTransfer->getPayment());
108
        $this->computopResponseTransfer = $this->activeMapper->getComputopResponseTransfer($quoteTransfer->getPayment());
109
110
        $paymentEntity = $this->savePaymentForOrder(
111
            $quoteTransfer->getPayment(),
112
            $saveOrderTransfer
113
        );
114
115
        $this->savePaymentDetailForOrder(
116
            $quoteTransfer->getPayment(),
117
            $paymentEntity
118
        );
119
120
        $this->savePaymentForOrderItems(
121
            $saveOrderTransfer->getOrderItems(),
122
            $paymentEntity->getIdPaymentComputop()
123
        );
124
125
        $this->saveExpenses($quoteTransfer, $saveOrderTransfer);
0 ignored issues
show
Bug introduced by
The method saveExpenses() does not exist on SprykerEco\Zed\Computop\...ness\Order\OrderManager. ( Ignorable by Annotation )

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

125
        $this->/** @scrutinizer ignore-call */ 
126
               saveExpenses($quoteTransfer, $saveOrderTransfer);

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...
126
    }
127
128
    /**
129
     * @param \Generated\Shared\Transfer\PaymentTransfer $paymentTransfer
130
     * @param \Generated\Shared\Transfer\SaveOrderTransfer $saveOrderTransfer
131
     *
132
     * @return \Orm\Zed\Computop\Persistence\SpyPaymentComputop
133
     */
134
    protected function savePaymentForOrder(PaymentTransfer $paymentTransfer, SaveOrderTransfer $saveOrderTransfer)
135
    {
136
        $paymentEntity = new SpyPaymentComputop();
137
138
        $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

138
        $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...
139
        $paymentEntity->setPaymentMethod($paymentTransfer->getPaymentMethod());
140
        $paymentEntity->setReference($saveOrderTransfer->getOrderReference());
141
        $paymentEntity->setFkSalesOrder($saveOrderTransfer->getIdSalesOrder());
142
        $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

142
        $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...
143
        $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

143
        $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...
144
        $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

144
        $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...
145
146
        if ($this->isPaymentMethodEasyCredit()) {
147
            $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

147
            $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...
148
        }
149
150
        $paymentEntity->save();
151
152
        return $paymentEntity;
153
    }
154
155
    /**
156
     * @param \Generated\Shared\Transfer\PaymentTransfer $paymentTransfer
157
     * @param \Orm\Zed\Computop\Persistence\SpyPaymentComputop $paymentEntity
158
     *
159
     * @return \Orm\Zed\Computop\Persistence\SpyPaymentComputopDetail
160
     */
161
    protected function savePaymentDetailForOrder(PaymentTransfer $paymentTransfer, SpyPaymentComputop $paymentEntity)
162
    {
163
        $paymentDetailEntity = new SpyPaymentComputopDetail();
164
165
        $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

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