Completed
Push — master ( ab9bf2...7d6b3f )
by Fabian
09:56 queued 01:25
created

ShipmentBuilder   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 58
c 4
b 0
f 0
dl 0
loc 142
ccs 0
cts 55
cp 0
rs 10
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 21 2
A withTrackingNumbers() 0 7 1
A buildShipmentItems() 0 11 2
A __construct() 0 15 1
A buildTracks() 0 13 1
A forOrder() 0 11 1
A withItem() 0 7 1
1
<?php
2
declare(strict_types=1);
3
4
namespace TddWizard\Fixtures\Sales;
5
6
use Magento\Sales\Api\Data\ShipmentInterface;
7
use Magento\Sales\Api\Data\ShipmentItemCreationInterfaceFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Sales\Api\Data\S...reationInterfaceFactory 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 Magento\Sales\Api\Data\ShipmentTrackCreationInterface;
9
use Magento\Sales\Api\Data\ShipmentTrackCreationInterfaceFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Sales\Api\Data\S...reationInterfaceFactory 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...
10
use Magento\Sales\Api\ShipmentRepositoryInterface;
11
use Magento\Sales\Api\ShipOrderInterface;
12
use Magento\Sales\Model\Order;
13
use Magento\TestFramework\Helper\Bootstrap;
0 ignored issues
show
Bug introduced by
The type Magento\TestFramework\Helper\Bootstrap 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
15
/**
16
 * Builder to be used by fixtures
17
 */
18
class ShipmentBuilder
19
{
20
    /**
21
     * @var ShipmentItemCreationInterfaceFactory
22
     */
23
    private $itemFactory;
24
25
    /**
26
     * @var ShipmentTrackCreationInterfaceFactory
27
     */
28
    private $trackFactory;
29
30
    /**
31
     * @var ShipOrderInterface
32
     */
33
    private $shipOrder;
34
35
    /**
36
     * @var ShipmentRepositoryInterface
37
     */
38
    private $shipmentRepository;
39
40
    /**
41
     * @var Order
42
     */
43
    private $order;
44
45
    /**
46
     * @var int[]
47
     */
48
    private $orderItems;
49
50
    /**
51
     * @var string[]
52
     */
53
    private $trackingNumbers;
54
55
    final public function __construct(
56
        ShipmentItemCreationInterfaceFactory $itemFactory,
57
        ShipmentTrackCreationInterfaceFactory $trackFactory,
58
        ShipOrderInterface $shipOrder,
59
        ShipmentRepositoryInterface $shipmentRepository,
60
        Order $order
61
    ) {
62
        $this->itemFactory = $itemFactory;
63
        $this->trackFactory = $trackFactory;
64
        $this->shipOrder = $shipOrder;
65
        $this->shipmentRepository = $shipmentRepository;
66
        $this->order = $order;
67
68
        $this->orderItems = [];
69
        $this->trackingNumbers = [];
70
    }
71
72
    public static function forOrder(
73
        Order $order
74
    ): ShipmentBuilder {
75
        $objectManager = Bootstrap::getObjectManager();
76
77
        return new static(
78
            $objectManager->create(ShipmentItemCreationInterfaceFactory::class),
79
            $objectManager->create(ShipmentTrackCreationInterfaceFactory::class),
80
            $objectManager->create(ShipOrderInterface::class),
81
            $objectManager->create(ShipmentRepositoryInterface::class),
82
            $order
83
        );
84
    }
85
86
    public function withItem(int $orderItemId, int $qty): ShipmentBuilder
87
    {
88
        $builder = clone $this;
89
90
        $builder->orderItems[$orderItemId] = $qty;
91
92
        return $builder;
93
    }
94
95
    public function withTrackingNumbers(string ...$trackingNumbers): ShipmentBuilder
96
    {
97
        $builder = clone $this;
98
99
        $builder->trackingNumbers = $trackingNumbers;
100
101
        return $builder;
102
    }
103
104
    public function build(): ShipmentInterface
105
    {
106
        $shipmentItems = $this->buildShipmentItems();
107
        $tracks = $this->buildTracks();
108
109
        $shipmentId = $this->shipOrder->execute(
110
            $this->order->getEntityId(),
111
            $shipmentItems,
112
            false,
113
            false,
114
            null,
115
            $tracks
116
        );
117
118
        $shipment = $this->shipmentRepository->get($shipmentId);
119
        if (!empty($this->trackingNumbers)) {
120
            $shipment->setShippingLabel('%PDF-1.4');
121
            $this->shipmentRepository->save($shipment);
122
        }
123
124
        return $shipment;
125
    }
126
127
    /**
128
     * @return \Magento\Sales\Api\Data\ShipmentTrackCreationInterface[]
129
     */
130
    private function buildTracks(): array
131
    {
132
        return array_map(
133
            function (string $trackingNumber): ShipmentTrackCreationInterface {
134
                $carrierCode = strtok((string)$this->order->getShippingMethod(), '_');
135
                $track = $this->trackFactory->create();
136
                $track->setCarrierCode($carrierCode);
137
                $track->setTitle($carrierCode);
138
                $track->setTrackNumber($trackingNumber);
139
140
                return $track;
141
            },
142
            $this->trackingNumbers
143
        );
144
    }
145
146
    /**
147
     * @return \Magento\Sales\Api\Data\ShipmentItemCreationInterface[]
148
     */
149
    private function buildShipmentItems(): array
150
    {
151
        $shipmentItems = [];
152
153
        foreach ($this->orderItems as $orderItemId => $qty) {
154
            $shipmentItem = $this->itemFactory->create();
155
            $shipmentItem->setOrderItemId($orderItemId);
156
            $shipmentItem->setQty($qty);
157
            $shipmentItems[] = $shipmentItem;
158
        }
159
        return $shipmentItems;
160
    }
161
}
162