Passed
Pull Request — dev (#9)
by Andrey
07:30 queued 03:53
created

getOmsStatusByName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Apache OSL-2
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\AmazonPay\Communication\Plugin\Oms\Command;
9
10
use ArrayObject;
11
use Generated\Shared\Transfer\AddressTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfer\AddressTransfer 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\AmazonpayCallTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfer\AmazonpayCallTransfer 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\ItemTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfer\ItemTransfer 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 IteratorAggregate;
15
use Orm\Zed\AmazonPay\Persistence\SpyPaymentAmazonpay;
1 ignored issue
show
Bug introduced by
The type Orm\Zed\AmazonPay\Persistence\SpyPaymentAmazonpay 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\Oms\Persistence\SpyOmsOrderItemStateQuery;
1 ignored issue
show
Bug introduced by
The type Orm\Zed\Oms\Persistence\SpyOmsOrderItemStateQuery 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\Sales\Persistence\SpySalesOrder;
1 ignored issue
show
Bug introduced by
The type Orm\Zed\Sales\Persistence\SpySalesOrder 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\SpySalesOrderAddress;
1 ignored issue
show
Bug introduced by
The type Orm\Zed\Sales\Persistence\SpySalesOrderAddress 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 Orm\Zed\Sales\Persistence\SpySalesOrderItem;
1 ignored issue
show
Bug introduced by
The type Orm\Zed\Sales\Persistence\SpySalesOrderItem 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...
20
use Spryker\Shared\Shipment\ShipmentConstants;
21
use Spryker\Zed\Kernel\Communication\AbstractPlugin;
22
use Spryker\Zed\Oms\Dependency\Plugin\Command\CommandByOrderInterface;
23
24
/**
25
 * @method \SprykerEco\Zed\AmazonPay\Business\AmazonPayFacadeInterface getFacade()
26
 * @method \SprykerEco\Zed\AmazonPay\Communication\AmazonPayCommunicationFactory getFactory()
27
 */
28
abstract class AbstractAmazonpayCommandPlugin extends AbstractPlugin implements CommandByOrderInterface
29
{
30
    /**
31
     * @var bool
32
     */
33
    protected $wasShippingCharged;
34
35
    /**
36
     * @return string
37
     */
38
    protected function getAffectingRequestedAmountItemsStateFlag()
39
    {
40
        return '';
41
    }
42
43
    /**
44
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrder $orderEntity
45
     * @param \ArrayObject|\Generated\Shared\Transfer\ItemTransfer[] $itemTransfers
46
     *
47
     * @return int
48
     */
49
    protected function getRequestedAmountByOrderAndItems(SpySalesOrder $orderEntity, ArrayObject $itemTransfers)
50
    {
51
        $subtotal = $this->getPriceToPay($itemTransfers);
52
53
        if (!$this->wasShippingCharged
54
            && $this->getFactory()
55
                ->createRequestAmountCalculator()
56
                ->shouldChargeShipping($orderEntity, $this->getAffectingRequestedAmountItemsStateFlag())) {
57
            $subtotal += $this->getShipmentPrice($orderEntity);
58
            $this->wasShippingCharged = true;
59
        }
60
61
        return $subtotal;
62
    }
63
64
    /**
65
     * @param \ArrayObject $itemTransfers
66
     *
67
     * @return int
68
     */
69
    protected function getPriceToPay(ArrayObject $itemTransfers)
70
    {
71
        $subtotal = 0;
72
73
        foreach ($itemTransfers as $itemTransfer) {
74
            $subtotal += $itemTransfer->getUnitPriceToPayAggregation();
75
        }
76
77
        return $subtotal;
78
    }
79
80
    /**
81
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrder $orderEntity
82
     *
83
     * @return int
84
     */
85
    protected function getShipmentPrice(SpySalesOrder $orderEntity)
86
    {
87
        return $this->getExpenseByType($orderEntity, ShipmentConstants::SHIPMENT_EXPENSE_TYPE)
88
            ->getPriceToPayAggregation();
89
    }
90
91
    /**
92
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrder $orderEntity
93
     * @param string $type
94
     *
95
     * @return \Orm\Zed\Sales\Persistence\SpySalesExpense|null
1 ignored issue
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...
96
     */
97
    protected function getExpenseByType(SpySalesOrder $orderEntity, $type)
98
    {
99
        foreach ($orderEntity->getExpenses() as $expense) {
100
            if ($expense->getType() === $type) {
101
                return $expense;
102
            }
103
        }
104
105
        return null;
106
    }
107
108
    /**
109
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrderItem[] $salesOrderItems
110
     *
111
     * @return \Generated\Shared\Transfer\AmazonpayCallTransfer[]
112
     */
113 View Code Duplication
    protected function groupSalesOrderItemsByAuthId(array $salesOrderItems)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
    {
115
        $groups = [];
116
117
        foreach ($salesOrderItems as $salesOrderItem) {
118
            $payment = $this->getPaymentDetails($salesOrderItem);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $payment is correct as $this->getPaymentDetails($salesOrderItem) targeting SprykerEco\Zed\AmazonPay...in::getPaymentDetails() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
119
120
            if (!$payment) {
121
                continue;
122
            }
123
124
            $groupData = $groups[$payment->getAuthorizationReferenceId()] ?? $this->createAmazonpayCallTransfer($payment);
0 ignored issues
show
Bug introduced by
$payment of type void is incompatible with the type Orm\Zed\AmazonPay\Persistence\SpyPaymentAmazonpay expected by parameter $payment of SprykerEco\Zed\AmazonPay...AmazonpayCallTransfer(). ( Ignorable by Annotation )

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

124
            $groupData = $groups[$payment->getAuthorizationReferenceId()] ?? $this->createAmazonpayCallTransfer(/** @scrutinizer ignore-type */ $payment);
Loading history...
125
126
            $groupData->addItem(
127
                $this->mapSalesOrderItemToItemTransfer($salesOrderItem)
128
            );
129
130
            $groups[$payment->getAuthorizationReferenceId()] = $groupData;
131
        }
132
133
        return $groups;
134
    }
135
136
    /**
137
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrderItem[] $salesOrderItems
138
     *
139
     * @return \Generated\Shared\Transfer\AmazonpayCallTransfer[]
140
     */
141 View Code Duplication
    protected function groupSalesOrderItemsByCaptureId(array $salesOrderItems)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
    {
143
        $groups = [];
144
145
        foreach ($salesOrderItems as $salesOrderItem) {
146
            $payment = $this->getPaymentDetails($salesOrderItem);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $payment is correct as $this->getPaymentDetails($salesOrderItem) targeting SprykerEco\Zed\AmazonPay...in::getPaymentDetails() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
147
148
            if (!$payment) {
149
                continue;
150
            }
151
152
            $groupData = $groups[$payment->getAmazonCaptureId()] ?? $this->createAmazonpayCallTransfer($payment);
0 ignored issues
show
Bug introduced by
$payment of type void is incompatible with the type Orm\Zed\AmazonPay\Persistence\SpyPaymentAmazonpay expected by parameter $payment of SprykerEco\Zed\AmazonPay...AmazonpayCallTransfer(). ( Ignorable by Annotation )

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

152
            $groupData = $groups[$payment->getAmazonCaptureId()] ?? $this->createAmazonpayCallTransfer(/** @scrutinizer ignore-type */ $payment);
Loading history...
153
154
            $groupData->addItem(
155
                $this->mapSalesOrderItemToItemTransfer($salesOrderItem)
156
            );
157
158
            $groups[$payment->getAmazonCaptureId()] = $groupData;
159
        }
160
161
        return $groups;
162
    }
163
164
    /**
165
     * @param \Orm\Zed\AmazonPay\Persistence\SpyPaymentAmazonpay $payment
166
     *
167
     * @return \Generated\Shared\Transfer\AmazonpayCallTransfer
168
     */
169
    protected function createAmazonpayCallTransfer(SpyPaymentAmazonpay $payment)
170
    {
171
        $amazonPayment = $this->getFactory()
172
            ->createPaymentAmazonpayConverter()
173
            ->mapEntityToTransfer($payment);
174
175
        $amazonpayCallTransfer = new AmazonpayCallTransfer();
176
        $amazonpayCallTransfer->setAmazonpayPayment($amazonPayment);
177
178
        return $amazonpayCallTransfer;
179
    }
180
181
    /**
182
     * @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonpayCallTransfer
183
     * @param array $salesOrderItems
184
     *
185
     * @return void
186
     */
187
    protected function populateItems(AmazonpayCallTransfer $amazonpayCallTransfer, array $salesOrderItems)
188
    {
189
        $amazonpayCallTransfer->setItems(
190
            new ArrayObject(
191
                array_map([$this, 'mapSalesOrderItemToItemTransfer'], $salesOrderItems)
192
            )
193
        );
194
    }
195
196
    /**
197
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrderItem $salesOrderItem
198
     *
199
     * @return null|\Orm\Zed\AmazonPay\Persistence\SpyPaymentAmazonpay
200
     */
201
    protected function getPaymentDetails(SpySalesOrderItem $salesOrderItem)
202
    {
203
        /** @var \Orm\Zed\AmazonPay\Persistence\SpyPaymentAmazonpaySalesOrderItem $payment */
204
        $payment = $salesOrderItem->getSpyPaymentAmazonpaySalesOrderItems()->getLast();
205
206
        if (!$payment) {
207
            return null;
208
        }
209
210
        return $payment->getSpyPaymentAmazonpay();
211
    }
212
213
    /**
214
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrderItem $salesOrderItemEntity
215
     *
216
     * @return \Generated\Shared\Transfer\ItemTransfer
217
     */
218
    protected function mapSalesOrderItemToItemTransfer(SpySalesOrderItem $salesOrderItemEntity)
219
    {
220
        $itemTransfer = (new ItemTransfer())
221
            ->fromArray($salesOrderItemEntity->toArray(), true);
222
223
        $itemTransfer->setUnitGrossPrice($salesOrderItemEntity->getGrossPrice());
224
        $itemTransfer->setUnitNetPrice($salesOrderItemEntity->getNetPrice());
225
        $itemTransfer->setUnitPrice($salesOrderItemEntity->getPrice());
226
        $itemTransfer->setUnitPriceToPayAggregation($salesOrderItemEntity->getPriceToPayAggregation());
227
        $itemTransfer->setUnitSubtotalAggregation($salesOrderItemEntity->getSubtotalAggregation());
228
        $itemTransfer->setUnitProductOptionPriceAggregation($salesOrderItemEntity->getProductOptionPriceAggregation());
229
        $itemTransfer->setUnitExpensePriceAggregation($salesOrderItemEntity->getExpensePriceAggregation());
230
        $itemTransfer->setUnitTaxAmount($salesOrderItemEntity->getTaxAmount());
231
        $itemTransfer->setUnitTaxAmountFullAggregation($salesOrderItemEntity->getTaxAmountFullAggregation());
232
        $itemTransfer->setUnitDiscountAmountAggregation($salesOrderItemEntity->getDiscountAmountAggregation());
233
        $itemTransfer->setUnitDiscountAmountFullAggregation($salesOrderItemEntity->getDiscountAmountFullAggregation());
234
        $itemTransfer->setRefundableAmount($salesOrderItemEntity->getRefundableAmount());
235
236
        return $itemTransfer;
237
    }
238
239
    /**
240
     * @param \IteratorAggregate|\Orm\Zed\Sales\Persistence\SpySalesOrderItem[] $salesOrderItems
241
     * @param string $statusName
242
     *
243
     * @return void
244
     */
245
    protected function setOrderItemsStatus(IteratorAggregate $salesOrderItems, $statusName)
246
    {
247
        $statusEntity = $this->getOmsStatusByName($statusName);
248
249
        foreach ($salesOrderItems as $salesOrderItem) {
250
            $salesOrderItem->setFkOmsOrderItemState($statusEntity->getIdOmsOrderItemState());
251
            $salesOrderItem->save();
252
        }
253
    }
254
255
    /**
256
     * @param string $statusName
257
     *
258
     * @return \Orm\Zed\Oms\Persistence\SpyOmsOrderItemState
1 ignored issue
show
Bug introduced by
The type Orm\Zed\Oms\Persistence\SpyOmsOrderItemState 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...
259
     */
260
    protected function getOmsStatusByName($statusName)
261
    {
262
        $statusEntity = SpyOmsOrderItemStateQuery::create()->filterByName($statusName)
263
            ->findOneOrCreate();
264
        $statusEntity->save();
265
266
        return $statusEntity;
267
    }
268
269
    /**
270
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrderAddress $address
271
     *
272
     * @return \Generated\Shared\Transfer\AddressTransfer
273
     */
274
    protected function buildAddressTransfer(SpySalesOrderAddress $address)
275
    {
276
        return (new AddressTransfer())
277
            ->fromArray($address->toArray(), true)
278
            ->fromArray($address->getCountry()->toArray(), true);
279
    }
280
281
    /**
282
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrderItem[] $salesOrderItems
283
     *
284
     * @return \Orm\Zed\AmazonPay\Persistence\SpyPaymentAmazonpay
285
     */
286
    protected function getPayment(array $salesOrderItems)
287
    {
288
        return $this->getPaymentDetails($salesOrderItems[0]);
1 ignored issue
show
Bug introduced by
Are you sure the usage of $this->getPaymentDetails($salesOrderItems[0]) targeting SprykerEco\Zed\AmazonPay...in::getPaymentDetails() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
289
    }
290
}
291