Completed
Push — master ( 7d6b3f...2df636 )
by Fabian
14s queued 10s
created

ShipmentBuilder::buildTracks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

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