Passed
Pull Request — dev (#9)
by Andrey
08:25 queued 04:31
created

ShipmentDataQuoteUpdater   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 95
rs 10
c 0
b 0
f 0
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A updateExpenses() 0 15 3
A createShippingExpenseTransfer() 0 9 1
A update() 0 8 1
A getShipmentMethodByQuote() 0 12 3
A __construct() 0 3 1
A getAvailableShipmentMethods() 0 3 1
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\Business\Quote;
9
10
use ArrayObject;
11
use Generated\Shared\Transfer\ExpenseTransfer;
1 ignored issue
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\QuoteTransfer;
1 ignored issue
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\ShipmentMethodTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfer\ShipmentMethodTransfer 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 Spryker\Shared\Shipment\ShipmentConstants;
15
use SprykerEco\Zed\AmazonPay\Dependency\Facade\AmazonPayToShipmentInterface;
16
17
class ShipmentDataQuoteUpdater implements QuoteUpdaterInterface
18
{
19
    /**
20
     * @var \SprykerEco\Zed\AmazonPay\Dependency\Facade\AmazonPayToShipmentInterface
21
     */
22
    protected $shipmentFacade;
23
24
    /**
25
     * @param \SprykerEco\Zed\AmazonPay\Dependency\Facade\AmazonPayToShipmentInterface $shipmentFacade
26
     */
27
    public function __construct(AmazonPayToShipmentInterface $shipmentFacade)
28
    {
29
        $this->shipmentFacade = $shipmentFacade;
30
    }
31
32
    /**
33
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
34
     *
35
     * @return \Generated\Shared\Transfer\QuoteTransfer
36
     */
37
    public function update(QuoteTransfer $quoteTransfer)
38
    {
39
        $shipmentMethodTransfer = $this->getShipmentMethodByQuote($quoteTransfer);
40
41
        $quoteTransfer->getShipment()->setMethod($shipmentMethodTransfer);
42
        $quoteTransfer = $this->updateExpenses($quoteTransfer);
43
44
        return $quoteTransfer;
45
    }
46
47
    /**
48
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
49
     *
50
     * @return \Generated\Shared\Transfer\ShipmentMethodTransfer|null
51
     */
52
    protected function getShipmentMethodByQuote(QuoteTransfer $quoteTransfer)
53
    {
54
        $shipmentMethodsTransfer = $this->getAvailableShipmentMethods($quoteTransfer);
55
        $idShipmentMethod = (int)$quoteTransfer->getShipment()->getShipmentSelection();
56
57
        foreach ($shipmentMethodsTransfer->getMethods() as $shipmentMethodTransfer) {
58
            if ($shipmentMethodTransfer->getIdShipmentMethod() === $idShipmentMethod) {
59
                return $shipmentMethodTransfer;
60
            }
61
        }
62
63
        return null;
64
    }
65
66
    /**
67
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
68
     *
69
     * @return \Generated\Shared\Transfer\ShipmentMethodsTransfer
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfer\ShipmentMethodsTransfer 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...
70
     */
71
    protected function getAvailableShipmentMethods(QuoteTransfer $quoteTransfer)
72
    {
73
        return $this->shipmentFacade->getAvailableShipmentMethods($quoteTransfer);
74
    }
75
76
    /**
77
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
78
     *
79
     * @return \Generated\Shared\Transfer\QuoteTransfer
80
     */
81
    protected function updateExpenses(QuoteTransfer $quoteTransfer)
82
    {
83
        $expenseTransfer = $this->createShippingExpenseTransfer($quoteTransfer->getShipment()->getMethod());
84
85
        $otherExpenseCollection = new ArrayObject();
86
        foreach ($quoteTransfer->getExpenses() as $expense) {
87
            if ($expense->getType() !== ShipmentConstants::SHIPMENT_EXPENSE_TYPE) {
88
                $otherExpenseCollection->append($expense);
89
            }
90
        }
91
92
        $quoteTransfer->setExpenses($otherExpenseCollection);
93
        $quoteTransfer->addExpense($expenseTransfer);
94
95
        return $quoteTransfer;
96
    }
97
98
    /**
99
     * @param \Generated\Shared\Transfer\ShipmentMethodTransfer $shipmentMethodTransfer
100
     *
101
     * @return \Generated\Shared\Transfer\ExpenseTransfer
102
     */
103
    protected function createShippingExpenseTransfer(ShipmentMethodTransfer $shipmentMethodTransfer)
104
    {
105
        $shipmentExpenseTransfer = new ExpenseTransfer();
106
        $shipmentExpenseTransfer->fromArray($shipmentMethodTransfer->toArray(), true);
107
        $shipmentExpenseTransfer->setType(ShipmentConstants::SHIPMENT_EXPENSE_TYPE);
108
        $shipmentExpenseTransfer->setUnitGrossPrice($shipmentMethodTransfer->getStoreCurrencyPrice());
109
        $shipmentExpenseTransfer->setQuantity(1);
110
111
        return $shipmentExpenseTransfer;
112
    }
113
}
114