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

InvoiceBuilder   A

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 27
cts 27
cp 1
rs 10
wmc 6

5 Methods

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