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

InvoiceBuilder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 80
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 14 2
A __construct() 0 12 1
A forOrder() 0 13 2
A withItem() 0 7 1
1
<?php
2
3
namespace TddWizard\Fixtures\Sales;
4
5
use Magento\Framework\ObjectManagerInterface;
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\Data\OrderInterface;
9
use Magento\Sales\Api\InvoiceOrderInterface;
10
use Magento\Sales\Api\InvoiceRepositoryInterface;
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 OrderInterface
35
     */
36
    private $order;
37
38
    /**
39
     * @var int[]
40
     */
41
    private $orderItems;
42
43
    public function __construct(
44
        InvoiceItemCreationInterfaceFactory $itemFactory,
45
        InvoiceOrderInterface $invoiceOrder,
46
        InvoiceRepositoryInterface $invoiceRepository,
47
        OrderInterface $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
        OrderInterface $order,
59
        ObjectManagerInterface $objectManager = null
60
    ): InvoiceBuilder {
61
        if ($objectManager === null) {
62
            $objectManager = Bootstrap::getObjectManager();
63
        }
64
65
        return new static(
66
            $objectManager->create(InvoiceItemCreationInterfaceFactory::class),
67
            $objectManager->create(InvoiceOrderInterface::class),
68
            $objectManager->create(InvoiceRepositoryInterface::class),
69
            $order
70
        );
71
    }
72
73
    public function withItem(int $orderItemId, int $qty): InvoiceBuilder
74
    {
75
        $builder = clone $this;
76
77
        $builder->orderItems[$orderItemId] = $qty;
78
79
        return $builder;
80
    }
81
82
    public function build(): InvoiceInterface
83
    {
84
        $invoiceItems = [];
85
86
        foreach ($this->orderItems as $orderItemId => $qty) {
87
            $invoiceItem = $this->itemFactory->create();
88
            $invoiceItem->setOrderItemId($orderItemId);
89
            $invoiceItem->setQty($qty);
90
            $invoiceItems[] = $invoiceItem;
91
        }
92
93
        $invoiceId = $this->invoiceOrder->execute($this->order->getEntityId(), false, $invoiceItems);
94
95
        return $this->invoiceRepository->get($invoiceId);
96
    }
97
}
98