Completed
Push — master ( 51c17f...9ad827 )
by Fabian
14s queued 10s
created

ShipmentBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 5
dl 0
loc 15
rs 10
1
<?php
2
3
namespace TddWizard\Fixtures\Sales;
4
5
use Magento\Framework\ObjectManagerInterface;
6
use Magento\Sales\Api\Data\OrderInterface;
7
use Magento\Sales\Api\Data\ShipmentInterface;
8
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...
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\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...
13
14
/**
15
 * Builder to be used by fixtures
16
 */
17
class ShipmentBuilder
18
{
19
    /**
20
     * @var ShipmentItemCreationInterfaceFactory
21
     */
22
    private $itemFactory;
23
24
    /**
25
     * @var ShipmentTrackCreationInterfaceFactory
26
     */
27
    private $trackFactory;
28
29
    /**
30
     * @var ShipOrderInterface
31
     */
32
    private $shipOrder;
33
34
    /**
35
     * @var ShipmentRepositoryInterface
36
     */
37
    private $shipmentRepository;
38
39
    /**
40
     * @var OrderInterface
41
     */
42
    private $order;
43
44
    /**
45
     * @var int[]
46
     */
47
    private $orderItems;
48
49
    /**
50
     * @var string[]
51
     */
52
    private $trackingNumbers;
53
54
    public function __construct(
55
        ShipmentItemCreationInterfaceFactory $itemFactory,
56
        ShipmentTrackCreationInterfaceFactory $trackFactory,
57
        ShipOrderInterface $shipOrder,
58
        ShipmentRepositoryInterface $shipmentRepository,
59
        OrderInterface $order
60
    ) {
61
        $this->itemFactory = $itemFactory;
62
        $this->trackFactory = $trackFactory;
63
        $this->shipOrder = $shipOrder;
64
        $this->shipmentRepository = $shipmentRepository;
65
        $this->order = $order;
66
67
        $this->orderItems = [];
68
        $this->trackingNumbers = [];
69
    }
70
71
    public static function forOrder(
72
        OrderInterface $order,
73
        ObjectManagerInterface $objectManager = null
74
    ): ShipmentBuilder {
75
        if ($objectManager === null) {
76
            $objectManager = Bootstrap::getObjectManager();
77
        }
78
79
        return new static(
80
            $objectManager->create(ShipmentItemCreationInterfaceFactory::class),
81
            $objectManager->create(ShipmentTrackCreationInterfaceFactory::class),
82
            $objectManager->create(ShipOrderInterface::class),
83
            $objectManager->create(ShipmentRepositoryInterface::class),
84
            $order
85
        );
86
    }
87
88
    public function withItem(int $orderItemId, int $qty): ShipmentBuilder
89
    {
90
        $builder = clone $this;
91
92
        $builder->orderItems[$orderItemId] = $qty;
93
94
        return $builder;
95
    }
96
97
    public function withTrackingNumbers(string ...$trackingNumbers): ShipmentBuilder
98
    {
99
        $builder = clone $this;
100
101
        $builder->trackingNumbers = $trackingNumbers;
102
103
        return $builder;
104
    }
105
106
    public function build(): ShipmentInterface
107
    {
108
        $shipmentItems = [];
109
110
        foreach ($this->orderItems as $orderItemId => $qty) {
111
            $shipmentItem = $this->itemFactory->create();
112
            $shipmentItem->setOrderItemId($orderItemId);
113
            $shipmentItem->setQty($qty);
114
            $shipmentItems[] = $shipmentItem;
115
        }
116
117
        $tracks = array_map(
118
            function (string $trackingNumber) {
119
                $carrierCode = strtok($this->order->getShippingMethod(), '_');
0 ignored issues
show
Bug introduced by
The method getShippingMethod() does not exist on Magento\Sales\Api\Data\OrderInterface. Did you maybe mean getShippingAmount()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

119
                $carrierCode = strtok($this->order->/** @scrutinizer ignore-call */ getShippingMethod(), '_');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
120
                $track = $this->trackFactory->create();
121
                $track->setCarrierCode($carrierCode);
122
                $track->setTitle($carrierCode);
123
                $track->setTrackNumber($trackingNumber);
124
125
                return $track;
126
            },
127
            $this->trackingNumbers
128
        );
129
130
        $shipmentId = $this->shipOrder->execute(
131
            $this->order->getEntityId(),
132
            $shipmentItems,
133
            false,
134
            false,
135
            null,
136
            $tracks
137
        );
138
139
        $shipment = $this->shipmentRepository->get($shipmentId);
140
        if (!empty($this->trackingNumbers)) {
141
            $shipment->setShippingLabel('%PDF-1.4');
142
            $this->shipmentRepository->save($shipment);
143
        }
144
145
        return $shipment;
146
    }
147
}
148