Passed
Pull Request — feature/eco-3656/dev-paypal-ex... (#40)
by
unknown
07:17
created

DefaultShippingMethodPlugin::expandQuote()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 17
rs 9.9
c 0
b 0
f 0
cc 3
nc 2
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\Client\Computop\Quote\Dependency\Plugin;
9
10
use Generated\Shared\Transfer\AddressTransfer;
0 ignored issues
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...
11
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...
12
use Generated\Shared\Transfer\ShipmentMethodTransfer;
0 ignored issues
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...
13
use Generated\Shared\Transfer\ShipmentTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\ShipmentTransfer 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\Client\Kernel\AbstractPlugin;
15
use Spryker\Client\Quote\Dependency\Plugin\QuoteTransferExpanderPluginInterface;
16
17
/**
18
 * @method \SprykerEco\Client\Computop\ComputopFactory getFactory()
19
 */
20
class DefaultShippingMethodPlugin extends AbstractPlugin implements QuoteTransferExpanderPluginInterface
21
{
22
    /**
23
     * {@inheritDoc}
24
     * - Expands provided `QuoteTransfer` with default shipping method.
25
     *
26
     * @api
27
     *
28
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
29
     *
30
     * @return \Generated\Shared\Transfer\QuoteTransfer
31
     */
32
    public function expandQuote(QuoteTransfer $quoteTransfer): QuoteTransfer
33
    {
34
        if (count($quoteTransfer->getItems()) === 0 || $this->quoteAlreadyHasShipment($quoteTransfer)) {
35
            return $quoteTransfer;
36
        }
37
38
        $quoteTransfer->setDefaultShipmentSelected(true);
39
        $defaultShipmentId = $this->getFactory()->getConfig()->getDefaultShipmentMethodId();
40
        $itemShipmentTransfer = $this->initItemShipment($defaultShipmentId);
41
        $this->addShipmentToQuoteItems($quoteTransfer, $itemShipmentTransfer);
42
        $defaultShipmentMethodTransfer = $this->getDefaultShipmentMethod($quoteTransfer, $defaultShipmentId);
43
        $itemShipmentTransfer->setMethod($defaultShipmentMethodTransfer);
44
45
        $quoteTransfer = $this->addShipmentToQuoteItems($quoteTransfer, $itemShipmentTransfer);
46
        $quoteTransfer = $this->expandQuoteWithShipmentGroups($quoteTransfer);
47
48
        return $quoteTransfer;
49
    }
50
51
    /**
52
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
53
     * @param \Generated\Shared\Transfer\ShipmentTransfer $shipmentTransfer
54
     *
55
     * @return \Generated\Shared\Transfer\QuoteTransfer
56
     */
57
    protected function addShipmentToQuoteItems(QuoteTransfer $quoteTransfer, ShipmentTransfer $shipmentTransfer): QuoteTransfer
58
    {
59
        foreach ($quoteTransfer->getItems() as $itemTransfer) {
60
            $itemTransfer->setShipment($shipmentTransfer);
61
        }
62
63
        return $quoteTransfer;
64
    }
65
66
    /**
67
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
68
     *
69
     * @return \Generated\Shared\Transfer\QuoteTransfer
70
     */
71
    protected function expandQuoteWithShipmentGroups(QuoteTransfer $quoteTransfer): QuoteTransfer
72
    {
73
        return $this->getFactory()->getShipmentClient()->expandQuoteWithShipmentGroups($quoteTransfer);
74
    }
75
76
    /**
77
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
78
     *
79
     * @return bool
80
     */
81
    protected function quoteAlreadyHasShipment(QuoteTransfer $quoteTransfer): bool
82
    {
83
        foreach ($quoteTransfer->getItems() as $itemTransfer) {
84
            $itemShipmentTransfer = $itemTransfer->getShipment();
85
            if ($itemShipmentTransfer !== null) {
86
                //at least one item already has shipment method
87
                return true;
88
            }
89
        }
90
91
        return false;
92
    }
93
94
    /**
95
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
96
     * @param int $idShipmentMethod
97
     *
98
     * @return \Generated\Shared\Transfer\ShipmentMethodTransfer
99
     */
100
    protected function getDefaultShipmentMethod(QuoteTransfer $quoteTransfer, int $idShipmentMethod): ShipmentMethodTransfer
101
    {
102
        $availableMethods = $this->getFactory()
103
            ->getShipmentClient()
104
            ->getAvailableMethodsByShipment($quoteTransfer)
105
            ->getShipmentMethods();
106
        foreach ($availableMethods as $availableMethod) {
107
            foreach ($availableMethod->getMethods() as $method) {
108
                if ($method->getIdShipmentMethod() === $idShipmentMethod) {
109
                    return $method;
110
                }
111
            }
112
        }
113
114
        return new ShipmentMethodTransfer();
115
    }
116
117
    /**
118
     * @param int $defaultShipmentId
119
     *
120
     * @return \Generated\Shared\Transfer\ShipmentTransfer
121
     */
122
    protected function initItemShipment(int $defaultShipmentId): ShipmentTransfer
123
    {
124
        $itemShipmentTransfer = new ShipmentTransfer();
125
        $itemShipmentTransfer->setShipmentSelection($defaultShipmentId);
126
        $itemShipmentTransfer->setShippingAddress(new AddressTransfer());
127
128
        return $itemShipmentTransfer;
129
    }
130
}
131