Completed
Push — feature/eco-3656/eco-3658-enab... ( ab05e4...16d9cc )
by
unknown
06:10
created

DefaultShippingMethodPlugin   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 33
dl 0
loc 107
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A expandQuoteWithShipmentGroups() 0 3 1
A quoteAlreadyHasShipment() 0 11 3
A expandQuote() 0 16 3
A getDefaultShipmentMethod() 0 15 4
A addShipmentToQuoteItems() 0 7 2
A initItemShipment() 0 7 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
     *
25
     * @api
26
     *
27
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
28
     *
29
     * @return \Generated\Shared\Transfer\QuoteTransfer
30
     */
31
    public function expandQuote(QuoteTransfer $quoteTransfer): QuoteTransfer
32
    {
33
        if (count($quoteTransfer->getItems()) === 0 || $this->quoteAlreadyHasShipment($quoteTransfer)) {
34
            return $quoteTransfer;
35
        }
36
37
        $defaultShipmentId = $this->getFactory()->getConfig()->getDefaultShipmentMethodId();
38
        $itemShipmentTransfer = $this->initItemShipment($defaultShipmentId);
39
        $this->addShipmentToQuoteItems($quoteTransfer, $itemShipmentTransfer);
40
        $defaultShipmentMethodTransfer = $this->getDefaultShipmentMethod($quoteTransfer, $defaultShipmentId);
41
        $itemShipmentTransfer->setMethod($defaultShipmentMethodTransfer);
42
43
        $quoteTransfer = $this->addShipmentToQuoteItems($quoteTransfer, $itemShipmentTransfer);
44
        $quoteTransfer = $this->expandQuoteWithShipmentGroups($quoteTransfer);
45
46
        return $quoteTransfer;
47
    }
48
49
    /**
50
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
51
     * @param \Generated\Shared\Transfer\ShipmentTransfer $shipmentTransfer
52
     *
53
     * @return \Generated\Shared\Transfer\QuoteTransfer
54
     */
55
    protected function addShipmentToQuoteItems(QuoteTransfer $quoteTransfer, ShipmentTransfer $shipmentTransfer): QuoteTransfer
56
    {
57
        foreach ($quoteTransfer->getItems() as $itemTransfer) {
58
            $itemTransfer->setShipment($shipmentTransfer);
59
        }
60
61
        return $quoteTransfer;
62
    }
63
64
    /**
65
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
66
     *
67
     * @return \Generated\Shared\Transfer\QuoteTransfer
68
     */
69
    protected function expandQuoteWithShipmentGroups(QuoteTransfer $quoteTransfer): QuoteTransfer
70
    {
71
        return $this->getFactory()->getShipmentClient()->expandQuoteWithShipmentGroups($quoteTransfer);
72
    }
73
74
    /**
75
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
76
     *
77
     * @return bool
78
     */
79
    protected function quoteAlreadyHasShipment(QuoteTransfer $quoteTransfer): bool
80
    {
81
        foreach ($quoteTransfer->getItems() as $itemTransfer) {
82
            $itemShipmentTransfer = $itemTransfer->getShipment();
83
            if ($itemShipmentTransfer !== null) {
84
                //at least one item already has shipment method
85
                return true;
86
            }
87
        }
88
89
        return false;
90
    }
91
92
    /**
93
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
94
     * @param int $idShipmentMethod
95
     *
96
     * @return \Generated\Shared\Transfer\ShipmentMethodTransfer
97
     */
98
    protected function getDefaultShipmentMethod(QuoteTransfer $quoteTransfer, int $idShipmentMethod): ShipmentMethodTransfer
99
    {
100
        $availableMethods = $this->getFactory()
101
            ->getShipmentClient()
102
            ->getAvailableMethodsByShipment($quoteTransfer)
103
            ->getShipmentMethods();
104
        foreach ($availableMethods as $availableMethod) {
105
            foreach ($availableMethod->getMethods() as $method) {
106
                if ($method->getIdShipmentMethod() === $idShipmentMethod) {
107
                    return $method;
108
                }
109
            }
110
        }
111
112
        return new ShipmentMethodTransfer();
113
    }
114
115
    /**
116
     * @param int $defaultShipmentId
117
     *
118
     * @return \Generated\Shared\Transfer\ShipmentTransfer
119
     */
120
    protected function initItemShipment(int $defaultShipmentId): ShipmentTransfer
121
    {
122
        $itemShipmentTransfer = new ShipmentTransfer();
123
        $itemShipmentTransfer->setShipmentSelection($defaultShipmentId);
124
        $itemShipmentTransfer->setShippingAddress(new AddressTransfer());
125
126
        return $itemShipmentTransfer;
127
    }
128
}
129