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

OrderBuilder::withPaymentMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

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