Passed
Push — feature/eco-3656/eco-3658-enab... ( ee0b63...39446b )
by
unknown
04:19
created

quoteAlreadyHasShipment()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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