Completed
Pull Request — master (#4)
by Andrey
10:46 queued 03:14
created

ShipmentDataQuoteUpdater::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 10
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\Business\Quote;
9
10
use ArrayObject;
11
use Generated\Shared\Transfer\ExpenseTransfer;
12
use Generated\Shared\Transfer\QuoteTransfer;
13
use Generated\Shared\Transfer\ShipmentMethodTransfer;
14
use Spryker\Shared\Shipment\ShipmentConstants;
15
use SprykerEco\Zed\Amazonpay\Dependency\Facade\AmazonpayToShipmentInterface;
16
17
class ShipmentDataQuoteUpdater implements QuoteUpdaterInterface
18
{
19
20
    /**
21
     * @var \SprykerEco\Zed\Amazonpay\Dependency\Facade\AmazonpayToShipmentInterface
22
     */
23
    protected $shipmentFacade;
24
25
    /**
26
     * @param \SprykerEco\Zed\Amazonpay\Dependency\Facade\AmazonpayToShipmentInterface $shipmentFacade
27
     */
28
    public function __construct(AmazonpayToShipmentInterface $shipmentFacade)
29
    {
30
        $this->shipmentFacade = $shipmentFacade;
31
    }
32
33
    /**
34
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
35
     *
36
     * @return \Generated\Shared\Transfer\QuoteTransfer
37
     */
38
    public function update(QuoteTransfer $quoteTransfer)
39
    {
40
        $shipmentMethodTransfer = $this->shipmentFacade->getShipmentMethodTransferById(
41
            (int)$quoteTransfer->getShipment()->getShipmentSelection()
42
        );
43
44
        $quoteTransfer->getShipment()->setMethod($shipmentMethodTransfer);
45
        $quoteTransfer = $this->updateExpenses($quoteTransfer);
46
47
        return $quoteTransfer;
48
    }
49
50
    /**
51
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
52
     *
53
     * @return \Generated\Shared\Transfer\QuoteTransfer
54
     */
55
    protected function updateExpenses(QuoteTransfer $quoteTransfer)
56
    {
57
        $expenseTransfer = $this->createShippingExpenseTransfer($quoteTransfer->getShipment()->getMethod());
58
59
        $otherExpenseCollection = new ArrayObject();
60
        foreach ($quoteTransfer->getExpenses() as $expense) {
61
            if ($expense->getType() !== ShipmentConstants::SHIPMENT_EXPENSE_TYPE) {
62
                $otherExpenseCollection->append($expense);
63
            }
64
        }
65
66
        $quoteTransfer->setExpenses($otherExpenseCollection);
67
        $quoteTransfer->addExpense($expenseTransfer);
68
69
        return $quoteTransfer;
70
    }
71
72
    /**
73
     * @param \Generated\Shared\Transfer\ShipmentMethodTransfer $shipmentMethodTransfer
74
     *
75
     * @return \Generated\Shared\Transfer\ExpenseTransfer
76
     */
77
    protected function createShippingExpenseTransfer(ShipmentMethodTransfer $shipmentMethodTransfer)
78
    {
79
        $shipmentExpenseTransfer = new ExpenseTransfer();
80
        $shipmentExpenseTransfer->fromArray($shipmentMethodTransfer->toArray(), true);
81
        $shipmentExpenseTransfer->setType(ShipmentConstants::SHIPMENT_EXPENSE_TYPE);
82
        $shipmentExpenseTransfer->setUnitGrossPrice($shipmentMethodTransfer->getDefaultPrice());
83
        $shipmentExpenseTransfer->setQuantity(1);
84
85
        return $shipmentExpenseTransfer;
86
    }
87
88
}
89