Completed
Push — master ( 49928c...938e21 )
by Fabian
15s queued 11s
created

ShipmentBuilder::withTrackingNumbers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 7
ccs 3
cts 3
cp 1
crap 1
rs 10
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 6
    final public function __construct(
56
        ShipmentItemCreationInterfaceFactory $itemFactory,
57
        ShipmentTrackCreationInterfaceFactory $trackFactory,
58
        ShipOrderInterface $shipOrder,
59
        ShipmentRepositoryInterface $shipmentRepository,
60
        Order $order
61
    ) {
62 6
        $this->itemFactory = $itemFactory;
63 6
        $this->trackFactory = $trackFactory;
64 6
        $this->shipOrder = $shipOrder;
65 6
        $this->shipmentRepository = $shipmentRepository;
66 6
        $this->order = $order;
67
68 6
        $this->orderItems = [];
69 6
        $this->trackingNumbers = [];
70 6
    }
71
72 6
    public static function forOrder(
73
        Order $order
74
    ): ShipmentBuilder {
75
        $objectManager = Bootstrap::getObjectManager();
76 6
77 6
        return new static(
78
            $objectManager->create(ShipmentItemCreationInterfaceFactory::class),
79
            $objectManager->create(ShipmentTrackCreationInterfaceFactory::class),
80 6
            $objectManager->create(ShipOrderInterface::class),
81 6
            $objectManager->create(ShipmentRepositoryInterface::class),
82 6
            $order
83 6
        );
84 6
    }
85
86
    public function withItem(int $orderItemId, int $qty): ShipmentBuilder
87
    {
88
        $builder = clone $this;
89 1
90
        $builder->orderItems[$orderItemId] = $qty;
91 1
92
        return $builder;
93 1
    }
94
95 1
    public function withTrackingNumbers(string ...$trackingNumbers): ShipmentBuilder
96
    {
97
        $builder = clone $this;
98 1
99
        $builder->trackingNumbers = $trackingNumbers;
100 1
101
        return $builder;
102 1
    }
103
104 1
    public function build(): ShipmentInterface
105
    {
106
        $shipmentItems = $this->buildShipmentItems();
107 6
        $tracks = $this->buildTracks();
108
109 6
        $shipmentId = $this->shipOrder->execute(
110
            $this->order->getEntityId(),
111 6
            $shipmentItems,
112 1
            false,
113 1
            false,
114 1
            null,
115 1
            $tracks
116
        );
117
118 6
        $shipment = $this->shipmentRepository->get($shipmentId);
119 6
        if (!empty($this->trackingNumbers)) {
120 1
            $shipment->setShippingLabel('%PDF-1.4');
121 1
            $this->shipmentRepository->save($shipment);
122 1
        }
123 1
124 1
        return $shipment;
125
    }
126 1
127 6
    /**
128 6
     * @return \Magento\Sales\Api\Data\ShipmentTrackCreationInterface[]
129
     */
130
    private function buildTracks(): array
131 6
    {
132 6
        return array_map(
133
            function (string $trackingNumber): ShipmentTrackCreationInterface {
134 6
                $carrierCode = strtok((string)$this->order->getShippingMethod(), '_');
135 6
                $track = $this->trackFactory->create();
136 6
                $track->setCarrierCode($carrierCode);
137
                $track->setTitle($carrierCode);
138
                $track->setTrackNumber($trackingNumber);
139
140 6
                return $track;
141 6
            },
142 1
            $this->trackingNumbers
143 1
        );
144
    }
145
146 6
    /**
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