InvoiceBuilder   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 86
ccs 29
cts 29
cp 1
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A build() 0 7 1
A buildInvoiceItems() 0 11 2
A forOrder() 0 10 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\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 4
    final public function __construct(
44
        InvoiceItemCreationInterfaceFactory $itemFactory,
45
        InvoiceOrderInterface $invoiceOrder,
46
        InvoiceRepositoryInterface $invoiceRepository,
47
        Order $order
48
    ) {
49 4
        $this->itemFactory = $itemFactory;
50 4
        $this->invoiceOrder = $invoiceOrder;
51 4
        $this->invoiceRepository = $invoiceRepository;
52 4
        $this->order = $order;
53
54 4
        $this->orderItems = [];
55 4
    }
56
57 4
    public static function forOrder(
58
        Order $order
59
    ): InvoiceBuilder {
60 4
        $objectManager = Bootstrap::getObjectManager();
61
62 4
        return new static(
63 4
            $objectManager->create(InvoiceItemCreationInterfaceFactory::class),
64 4
            $objectManager->create(InvoiceOrderInterface::class),
65 4
            $objectManager->create(InvoiceRepositoryInterface::class),
66
            $order
67
        );
68
    }
69
70 1
    public function withItem(int $orderItemId, int $qty): InvoiceBuilder
71
    {
72 1
        $builder = clone $this;
73
74 1
        $builder->orderItems[$orderItemId] = $qty;
75
76 1
        return $builder;
77
    }
78
79 4
    public function build(): InvoiceInterface
80
    {
81 4
        $invoiceItems = $this->buildInvoiceItems();
82
83 4
        $invoiceId = $this->invoiceOrder->execute($this->order->getEntityId(), false, $invoiceItems);
84
85 4
        return $this->invoiceRepository->get($invoiceId);
86
    }
87
88
    /**
89
     * @return \Magento\Sales\Api\Data\InvoiceItemCreationInterface[]
90
     */
91 4
    private function buildInvoiceItems(): array
92
    {
93 4
        $invoiceItems = [];
94
95 4
        foreach ($this->orderItems as $orderItemId => $qty) {
96 1
            $invoiceItem = $this->itemFactory->create();
97 1
            $invoiceItem->setOrderItemId($orderItemId);
98 1
            $invoiceItem->setQty($qty);
99 1
            $invoiceItems[] = $invoiceItem;
100
        }
101 4
        return $invoiceItems;
102
    }
103
}
104