CancelCommandHandler::getMessageTransfer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
rs 10
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\Oms\Command;
9
10
use Generated\Shared\Transfer\ComputopApiHeaderPaymentTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...piHeaderPaymentTransfer 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\MessageTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\MessageTransfer 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\OrderTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\OrderTransfer 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 SprykerEco\Zed\Computop\Business\Oms\Command\Manager\CancelManagerInterface;
14
use SprykerEco\Zed\Computop\Business\Payment\Handler\PostPlace\HandlerInterface;
15
use SprykerEco\Zed\Computop\Dependency\Facade\ComputopToMessengerFacadeInterface;
16
17
class CancelCommandHandler extends AbstractCommandHandler
18
{
19
    /**
20
     * @var \SprykerEco\Zed\Computop\Business\Payment\Handler\PostPlace\HandlerInterface
21
     */
22
    protected $inquirePaymentHandler;
23
24
    /**
25
     * @var \SprykerEco\Zed\Computop\Business\Payment\Handler\PostPlace\HandlerInterface
26
     */
27
    protected $reversePaymentHandler;
28
29
    /**
30
     * @var \SprykerEco\Zed\Computop\Business\Oms\Command\Manager\CancelManagerInterface
31
     */
32
    protected $manager;
33
34
    /**
35
     * @var \SprykerEco\Zed\Computop\Dependency\Facade\ComputopToMessengerFacadeInterface
36
     */
37
    protected $messengerFacade;
38
39
    /**
40
     * @param \SprykerEco\Zed\Computop\Business\Payment\Handler\PostPlace\HandlerInterface $inquirePaymentHandler
41
     * @param \SprykerEco\Zed\Computop\Business\Payment\Handler\PostPlace\HandlerInterface $reversePaymentHandler
42
     * @param \SprykerEco\Zed\Computop\Business\Oms\Command\Manager\CancelManagerInterface $cancelItemManager
43
     * @param \SprykerEco\Zed\Computop\Dependency\Facade\ComputopToMessengerFacadeInterface $messengerFacade
44
     */
45
    public function __construct(
46
        HandlerInterface $inquirePaymentHandler,
47
        HandlerInterface $reversePaymentHandler,
48
        CancelManagerInterface $cancelItemManager,
49
        ComputopToMessengerFacadeInterface $messengerFacade
50
    ) {
51
        $this->inquirePaymentHandler = $inquirePaymentHandler;
52
        $this->reversePaymentHandler = $reversePaymentHandler;
53
        $this->manager = $cancelItemManager;
54
        $this->messengerFacade = $messengerFacade;
55
    }
56
57
    /**
58
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrderItem[] $orderItems
59
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
60
     *
61
     * @return array|\Spryker\Shared\Kernel\Transfer\TransferInterface
62
     */
63
    public function handle(array $orderItems, OrderTransfer $orderTransfer)
64
    {
65
        if ($this->isAllOrderCancellation($orderItems, $orderTransfer)) {
66
            $computopHeaderPayment = $this->createComputopHeaderPayment($orderTransfer);
67
68
            return $this->cancelOrderAuthorization($orderItems, $orderTransfer, $computopHeaderPayment);
69
        }
70
71
        return $this->cancelOrderItems($orderItems);
72
    }
73
74
    /**
75
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrderItem[] $orderItemsToCancel
76
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
77
     *
78
     * @return bool
79
     */
80
    protected function isAllOrderCancellation(array $orderItemsToCancel, OrderTransfer $orderTransfer)
81
    {
82
        $allOrderItemsCount = count($orderTransfer->getItems());
83
        $cancelledOrderItemsCount = count($this->manager->getCanceledItems($orderTransfer));
84
        $orderItemsToCancelCount = count($orderItemsToCancel);
85
86
        return ($orderItemsToCancelCount + $cancelledOrderItemsCount) === $allOrderItemsCount;
87
    }
88
89
    /**
90
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrderItem[] $orderItems
91
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
92
     * @param \Generated\Shared\Transfer\ComputopApiHeaderPaymentTransfer $computopHeaderPayment
93
     *
94
     * @return array|\Spryker\Shared\Kernel\Transfer\TransferInterface
95
     */
96
    protected function cancelOrderAuthorization(array $orderItems, OrderTransfer $orderTransfer, ComputopApiHeaderPaymentTransfer $computopHeaderPayment)
97
    {
98
        $responseTransfer = $this->inquirePaymentHandler->handle($orderTransfer, $computopHeaderPayment);
99
100
        if ($responseTransfer->getIsAuthLast()) {
0 ignored issues
show
Bug introduced by
The method getIsAuthLast() 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

100
        if ($responseTransfer->/** @scrutinizer ignore-call */ getIsAuthLast()) {

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...
101
            return $this->reverseOrderAuthorizationRequest($orderTransfer, $computopHeaderPayment);
102
        }
103
104
        return $this->cancelOrderItems($orderItems);
105
    }
106
107
    /**
108
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
109
     * @param \Generated\Shared\Transfer\ComputopApiHeaderPaymentTransfer $computopHeaderPayment
110
     *
111
     * @return \Spryker\Shared\Kernel\Transfer\TransferInterface
112
     */
113
    protected function reverseOrderAuthorizationRequest(OrderTransfer $orderTransfer, ComputopApiHeaderPaymentTransfer $computopHeaderPayment)
114
    {
115
        $computopResponseTransfer = $this->reversePaymentHandler->handle($orderTransfer, $computopHeaderPayment);
116
        if ($computopResponseTransfer->getHeader()->getIsSuccess()) {
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

116
        if ($computopResponseTransfer->/** @scrutinizer ignore-call */ getHeader()->getIsSuccess()) {

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...
117
            $this->setInfoMessage('Authorization was reverted');
118
119
            return $computopResponseTransfer;
120
        }
121
122
        $this->setErrorMessage('Authorization was not reverted. Please check logs');
123
124
        return $computopResponseTransfer;
125
    }
126
127
    /**
128
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrderItem[] $orderItems
129
     *
130
     * @return array
131
     */
132
    protected function cancelOrderItems(array $orderItems)
133
    {
134
        $this->manager->changeComputopItemsStatus($orderItems);
135
136
        $this->setInfoMessage('Item(s) was(were) changed');
137
138
        return $orderItems;
139
    }
140
141
    /**
142
     * @param string $messageValue
143
     *
144
     * @return void
145
     */
146
    protected function setInfoMessage($messageValue)
147
    {
148
        $message = $this->getMessageTransfer($messageValue);
149
150
        $this
151
            ->messengerFacade
152
            ->addInfoMessage($message);
153
    }
154
155
    /**
156
     * @param string $messageValue
157
     *
158
     * @return void
159
     */
160
    protected function setErrorMessage($messageValue)
161
    {
162
        $messageTransfer = $this->getMessageTransfer($messageValue);
163
164
        $this
165
            ->messengerFacade
166
            ->addErrorMessage($messageTransfer);
167
    }
168
169
    /**
170
     * @param string $messageValue
171
     *
172
     * @return \Generated\Shared\Transfer\MessageTransfer
173
     */
174
    protected function getMessageTransfer($messageValue)
175
    {
176
        $messageTransfer = new MessageTransfer();
177
        $messageTransfer->setValue($messageValue);
178
179
        return $messageTransfer;
180
    }
181
}
182