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

InvoiceBuilder::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 2
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace TddWizard\Fixtures\Sales;
5
6
use Magento\Sales\Api\Data\InvoiceInterface;
7
use Magento\Sales\Api\Data\InvoiceItemCreationInterfaceFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Sales\Api\Data\I...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\InvoiceOrderInterface;
9
use Magento\Sales\Api\InvoiceRepositoryInterface;
10
use Magento\Sales\Model\Order;
11
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...
12
13
/**
14
 * Builder to be used by fixtures
15
 */
16
class InvoiceBuilder
17
{
18
    /**
19
     * @var InvoiceItemCreationInterfaceFactory
20
     */
21
    private $itemFactory;
22
23
    /**
24
     * @var InvoiceOrderInterface
25
     */
26
    private $invoiceOrder;
27
28
    /**
29
     * @var InvoiceRepositoryInterface
30
     */
31
    private $invoiceRepository;
32
33
    /**
34
     * @var Order
35
     */
36
    private $order;
37
38
    /**
39
     * @var int[]
40
     */
41
    private $orderItems;
42
43
    final public function __construct(
44
        InvoiceItemCreationInterfaceFactory $itemFactory,
45
        InvoiceOrderInterface $invoiceOrder,
46
        InvoiceRepositoryInterface $invoiceRepository,
47
        Order $order
48
    ) {
49
        $this->itemFactory = $itemFactory;
50
        $this->invoiceOrder = $invoiceOrder;
51
        $this->invoiceRepository = $invoiceRepository;
52
        $this->order = $order;
53
54
        $this->orderItems = [];
55
    }
56
57
    public static function forOrder(
58
        Order $order
59
    ): InvoiceBuilder {
60
        $objectManager = Bootstrap::getObjectManager();
61
62
        return new static(
63
            $objectManager->create(InvoiceItemCreationInterfaceFactory::class),
64
            $objectManager->create(InvoiceOrderInterface::class),
65
            $objectManager->create(InvoiceRepositoryInterface::class),
66
            $order
67
        );
68
    }
69
70
    public function withItem(int $orderItemId, int $qty): InvoiceBuilder
71
    {
72
        $builder = clone $this;
73
74
        $builder->orderItems[$orderItemId] = $qty;
75
76
        return $builder;
77
    }
78
79
    public function build(): InvoiceInterface
80
    {
81
        $invoiceItems = $this->buildInvoiceItems();
82
83
        $invoiceId = $this->invoiceOrder->execute($this->order->getEntityId(), false, $invoiceItems);
84
85
        return $this->invoiceRepository->get($invoiceId);
86
    }
87
88
    /**
89
     * @return \Magento\Sales\Api\Data\InvoiceItemCreationInterface[]
90
     */
91
    private function buildInvoiceItems(): array
92
    {
93
        $invoiceItems = [];
94
95
        foreach ($this->orderItems as $orderItemId => $qty) {
96
            $invoiceItem = $this->itemFactory->create();
97
            $invoiceItem->setOrderItemId($orderItemId);
98
            $invoiceItem->setQty($qty);
99
            $invoiceItems[] = $invoiceItem;
100
        }
101
        return $invoiceItems;
102
    }
103
}
104