Passed
Push — feature/eco-3656/eco-3658-enab... ( 4d2e5e...c70d81 )
by
unknown
06:33
created

createShipmentTransfer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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