Completed
Push — master ( 7d6b3f...2df636 )
by Fabian
14s queued 10s
created

OrderBuilder   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 49
c 1
b 0
f 0
dl 0
loc 134
ccs 52
cts 52
cp 1
rs 10
wmc 15

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A withCustomer() 0 6 1
A withShippingMethod() 0 6 1
A withProducts() 0 6 1
A withCart() 0 6 1
A withPaymentMethod() 0 6 1
B build() 0 54 8
A anOrder() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace TddWizard\Fixtures\Sales;
5
6
use Magento\Sales\Api\Data\OrderInterface;
7
use TddWizard\Fixtures\Catalog\ProductBuilder;
8
use TddWizard\Fixtures\Checkout\CartBuilder;
9
use TddWizard\Fixtures\Checkout\CustomerCheckout;
10
use TddWizard\Fixtures\Customer\AddressBuilder;
11
use TddWizard\Fixtures\Customer\CustomerBuilder;
12
use TddWizard\Fixtures\Customer\CustomerFixture;
13
14
/**
15
 * Builder to be used by fixtures
16
 */
17
class OrderBuilder
18
{
19
    /**
20
     * @var CartBuilder
21
     */
22
    private $cartBuilder;
23
24
    /**
25
     * @var ProductBuilder[]
26
     */
27
    private $productBuilders;
28
29
    /**
30
     * @var CustomerBuilder
31
     */
32
    private $customerBuilder;
33
34
    /**
35
     * @var string
36
     */
37
    private $shippingMethod;
38
39 14
    final public function __construct()
40
    {
41 14
    }
42
43
    /**
44
     * @var string
45
     */
46
    private $paymentMethod;
47
48 14
    public static function anOrder(): OrderBuilder
49
    {
50 14
        return new static();
51
    }
52
53 7
    public function withProducts(ProductBuilder ...$productBuilders): OrderBuilder
54
    {
55 7
        $builder = clone $this;
56 7
        $builder->productBuilders = $productBuilders;
57
58 7
        return $builder;
59
    }
60
61 4
    public function withCustomer(CustomerBuilder $customerBuilder): OrderBuilder
62
    {
63 4
        $builder = clone $this;
64 4
        $builder->customerBuilder = $customerBuilder;
65
66 4
        return $builder;
67
    }
68
69 5
    public function withCart(CartBuilder $cartBuilder): OrderBuilder
70
    {
71 5
        $builder = clone $this;
72 5
        $builder->cartBuilder = $cartBuilder;
73
74 5
        return $builder;
75
    }
76
77 2
    public function withShippingMethod(string $shippingMethod): OrderBuilder
78
    {
79 2
        $builder = clone $this;
80 2
        $builder->shippingMethod = $shippingMethod;
81
82 2
        return $builder;
83
    }
84
85 3
    public function withPaymentMethod(string $paymentMethod): OrderBuilder
86
    {
87 3
        $builder = clone $this;
88 3
        $builder->paymentMethod = $paymentMethod;
89
90 3
        return $builder;
91
    }
92
93
    /**
94
     * @return OrderInterface
95
     * @throws \Exception
96
     */
97 14
    public function build(): OrderInterface
98
    {
99 14
        $builder = clone $this;
100
101 14
        if (empty($builder->productBuilders)) {
102
            // init simple products
103 8
            for ($i = 0; $i < 3; $i++) {
104 8
                $builder->productBuilders[] = ProductBuilder::aSimpleProduct();
105
            }
106
        }
107
108
        // create products
109 14
        $products = array_map(
110
            static function (ProductBuilder $productBuilder) {
111 14
                return $productBuilder->build();
112 14
            },
113 14
            $builder->productBuilders
114
        );
115
116 14
        if (empty($builder->customerBuilder)) {
117
            // init customer
118 11
            $builder->customerBuilder = CustomerBuilder::aCustomer()
119 11
                ->withAddresses(AddressBuilder::anAddress()->asDefaultBilling()->asDefaultShipping());
120
        }
121
122
        // log customer in
123 14
        $customer = $builder->customerBuilder->build();
124 14
        $customerFixture = new CustomerFixture($customer);
125 14
        $customerFixture->login();
126
127 14
        if (empty($builder->cartBuilder)) {
128
            // init cart, add products
129 10
            $builder->cartBuilder = CartBuilder::forCurrentSession();
130 10
            foreach ($products as $product) {
131 10
                $qty = 1;
132 10
                $builder->cartBuilder = $builder->cartBuilder->withSimpleProduct($product->getSku(), $qty);
133
            }
134
        }
135
136
        // check out, place order
137 14
        $checkout = CustomerCheckout::fromCart($builder->cartBuilder->build());
138 14
        if ($builder->shippingMethod) {
139 2
            $checkout = $checkout->withShippingMethodCode($builder->shippingMethod);
140
        }
141
142 14
        if ($builder->paymentMethod) {
143 3
            $checkout = $checkout->withPaymentMethodCode($builder->paymentMethod);
144
        }
145
146 14
        $order = $checkout->placeOrder();
147
148 14
        $customerFixture->logout();
149
150 14
        return $order;
151
    }
152
}
153