Passed
Push — feature/eco-3656/eco-3658-enab... ( da021b...202423 )
by
unknown
04:29
created

getDefaultShipmentMethod()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 14
rs 10
cc 4
nc 4
nop 2
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\Zed\Computop\Business\DefaultShippingMethodQuoteExpander;
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 SprykerEco\Zed\Computop\ComputopConfig;
15
use SprykerEco\Zed\Computop\Dependency\Facade\ComputopToShipmentFacadeInterface;
16
17
class QuoteDefaultShippingMethodExpander implements QuoteDefaultShippingMethodExpanderInterface
18
{
19
    /**
20
     * @var \SprykerEco\Zed\Computop\ComputopConfig
21
     */
22
    protected $computopConfig;
23
24
    /**
25
     * @var \SprykerEco\Zed\Computop\Dependency\Facade\ComputopToShipmentFacadeInterface
26
     */
27
    protected $shipmentFacade;
28
29
    /**
30
     * @param \SprykerEco\Zed\Computop\ComputopConfig $computopConfig
31
     * @param \SprykerEco\Zed\Computop\Dependency\Facade\ComputopToShipmentFacadeInterface $shipmentFacade
32
     */
33
    public function __construct(ComputopConfig $computopConfig, ComputopToShipmentFacadeInterface $shipmentFacade)
34
    {
35
        $this->computopConfig = $computopConfig;
36
        $this->shipmentFacade = $shipmentFacade;
37
    }
38
39
    /**
40
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
41
     *
42
     * @return \Generated\Shared\Transfer\QuoteTransfer
43
     */
44
    public function expandQuoteWithDefaultShippingMethod(QuoteTransfer $quoteTransfer): QuoteTransfer
45
    {
46
        $quoteTransfer->setDefaultShipmentSelected(true);
47
        $defaultShipmentId = $this->computopConfig->getDefaultShipmentMethodId();
48
        $itemShipmentTransfer = $this->initItemShipment($defaultShipmentId);
49
        $this->addShipmentToQuoteItems($quoteTransfer, $itemShipmentTransfer);
50
        $defaultShipmentMethodTransfer = $this->getDefaultShipmentMethod($quoteTransfer, $defaultShipmentId);
51
        $itemShipmentTransfer->setMethod($defaultShipmentMethodTransfer);
52
53
        $quoteTransfer = $this->addShipmentToQuoteItems($quoteTransfer, $itemShipmentTransfer);
54
        $quoteTransfer = $this->expandQuoteWithShipmentGroups($quoteTransfer);
55
56
        return $quoteTransfer;
57
    }
58
59
    /**
60
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
61
     * @param \Generated\Shared\Transfer\ShipmentTransfer $shipmentTransfer
62
     *
63
     * @return \Generated\Shared\Transfer\QuoteTransfer
64
     */
65
    protected function addShipmentToQuoteItems(QuoteTransfer $quoteTransfer, ShipmentTransfer $shipmentTransfer): QuoteTransfer
66
    {
67
        foreach ($quoteTransfer->getItems() as $itemTransfer) {
68
            $itemTransfer->setShipment($shipmentTransfer);
69
        }
70
71
        return $quoteTransfer;
72
    }
73
74
    /**
75
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
76
     *
77
     * @return \Generated\Shared\Transfer\QuoteTransfer
78
     */
79
    protected function expandQuoteWithShipmentGroups(QuoteTransfer $quoteTransfer): QuoteTransfer
80
    {
81
        return $this->shipmentFacade->expandQuoteWithShipmentGroups($quoteTransfer);
82
    }
83
84
    /**
85
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
86
     * @param int $idShipmentMethod
87
     *
88
     * @return \Generated\Shared\Transfer\ShipmentMethodTransfer
89
     */
90
    protected function getDefaultShipmentMethod(QuoteTransfer $quoteTransfer, int $idShipmentMethod): ShipmentMethodTransfer
91
    {
92
        $availableMethods = $this->shipmentFacade
93
            ->getAvailableMethodsByShipment($quoteTransfer)
94
            ->getShipmentMethods();
95
        foreach ($availableMethods as $availableMethod) {
96
            foreach ($availableMethod->getMethods() as $method) {
97
                if ($method->getIdShipmentMethod() === $idShipmentMethod) {
98
                    return $method;
99
                }
100
            }
101
        }
102
103
        return new ShipmentMethodTransfer();
104
    }
105
106
    /**
107
     * @param int $defaultShipmentId
108
     *
109
     * @return \Generated\Shared\Transfer\ShipmentTransfer
110
     */
111
    protected function initItemShipment(int $defaultShipmentId): ShipmentTransfer
112
    {
113
        $itemShipmentTransfer = new ShipmentTransfer();
114
        $itemShipmentTransfer->setShipmentSelection($defaultShipmentId);
115
        $itemShipmentTransfer->setShippingAddress(new AddressTransfer());
116
117
        return $itemShipmentTransfer;
118
    }
119
}
120