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

OrderBuilder::withProducts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
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 1
dl 0
loc 6
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\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
    final public function __construct()
40
    {
41
    }
42
43
    /**
44
     * @var string
45
     */
46
    private $paymentMethod;
47
48
    public static function anOrder(): OrderBuilder
49
    {
50
        return new static();
51
    }
52
53
    public function withProducts(ProductBuilder ...$productBuilders): OrderBuilder
54
    {
55
        $builder = clone $this;
56
        $builder->productBuilders = $productBuilders;
57
58
        return $builder;
59
    }
60
61
    public function withCustomer(CustomerBuilder $customerBuilder): OrderBuilder
62
    {
63
        $builder = clone $this;
64
        $builder->customerBuilder = $customerBuilder;
65
66
        return $builder;
67
    }
68
69
    public function withCart(CartBuilder $cartBuilder): OrderBuilder
70
    {
71
        $builder = clone $this;
72
        $builder->cartBuilder = $cartBuilder;
73
74
        return $builder;
75
    }
76
77
    public function withShippingMethod(string $shippingMethod): OrderBuilder
78
    {
79
        $builder = clone $this;
80
        $builder->shippingMethod = $shippingMethod;
81
82
        return $builder;
83
    }
84
85
    public function withPaymentMethod(string $paymentMethod): OrderBuilder
86
    {
87
        $builder = clone $this;
88
        $builder->paymentMethod = $paymentMethod;
89
90
        return $builder;
91
    }
92
93
    /**
94
     * @return OrderInterface
95
     * @throws \Exception
96
     */
97
    public function build(): OrderInterface
98
    {
99
        $builder = clone $this;
100
101
        if (empty($builder->productBuilders)) {
102
            // init simple products
103
            for ($i = 0; $i < 3; $i++) {
104
                $builder->productBuilders[] = ProductBuilder::aSimpleProduct();
105
            }
106
        }
107
108
        // create products
109
        $products = array_map(
110
            static function (ProductBuilder $productBuilder) {
111
                return $productBuilder->build();
112
            },
113
            $builder->productBuilders
114
        );
115
116
        if (empty($builder->customerBuilder)) {
117
            // init customer
118
            $builder->customerBuilder = CustomerBuilder::aCustomer()
119
                ->withAddresses(AddressBuilder::anAddress()->asDefaultBilling()->asDefaultShipping());
120
        }
121
122
        // log customer in
123
        $customer = $builder->customerBuilder->build();
124
        $customerFixture = new CustomerFixture($customer);
125
        $customerFixture->login();
126
127
        if (empty($builder->cartBuilder)) {
128
            // init cart, add products
129
            $builder->cartBuilder = CartBuilder::forCurrentSession();
130
            foreach ($products as $product) {
131
                $qty = 1;
132
                $builder->cartBuilder = $builder->cartBuilder->withSimpleProduct($product->getSku(), $qty);
133
            }
134
        }
135
136
        // check out, place order
137
        $checkout = CustomerCheckout::fromCart($builder->cartBuilder->build());
138
        if ($builder->shippingMethod) {
139
            $checkout = $checkout->withShippingMethodCode($builder->shippingMethod);
140
        }
141
142
        if ($builder->paymentMethod) {
143
            $checkout = $checkout->withPaymentMethodCode($builder->paymentMethod);
144
        }
145
146
        $order = $checkout->placeOrder();
147
148
        $customerFixture->logout();
149
150
        return $order;
151
    }
152
}
153