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

CustomerCheckout::getShippingMethodCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace TddWizard\Fixtures\Checkout;
5
6
use Magento\Checkout\Model\Cart;
7
use Magento\Customer\Api\AddressRepositoryInterface;
8
use Magento\Payment\Model\Config as PaymentConfig;
9
use Magento\Quote\Api\CartRepositoryInterface;
10
use Magento\Quote\Model\Quote;
11
use Magento\Quote\Model\QuoteManagement;
12
use Magento\Sales\Api\Data\OrderInterface;
13
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...
14
15
class CustomerCheckout
16
{
17
    /**
18
     * @var AddressRepositoryInterface
19
     */
20
    private $addressRepository;
21
22
    /**
23
     * @var CartRepositoryInterface
24
     */
25
    private $quoteRepository;
26
27
    /**
28
     * @var QuoteManagement
29
     */
30
    private $quoteManagement;
31
32
    /**
33
     * @var PaymentConfig
34
     */
35
    private $paymentConfig;
36
37
    /**
38
     * @var Cart
39
     */
40
    private $cart;
41
42
    /**
43
     * @var int|null
44
     */
45
    private $shippingAddressId;
46
47
    /**
48
     * @var int|null
49
     */
50
    private $billingAddressId;
51
52
    /**
53
     * @var string|null
54
     */
55
    private $shippingMethodCode;
56
57
    /**
58
     * @var string|null
59
     */
60
    private $paymentMethodCode;
61
62
    final public function __construct(
63
        AddressRepositoryInterface $addressRepository,
64
        CartRepositoryInterface $quoteRepository,
65
        QuoteManagement $quoteManagement,
66
        PaymentConfig $paymentConfig,
67
        Cart $cart,
68
        int $shippingAddressId = null,
69
        int $billingAddressId = null,
70
        string $shippingMethodCode = null,
71
        string $paymentMethodCode = null
72
    ) {
73
74
        $this->addressRepository = $addressRepository;
75
        $this->quoteRepository = $quoteRepository;
76
        $this->quoteManagement = $quoteManagement;
77
        $this->paymentConfig = $paymentConfig;
78
        $this->cart = $cart;
79
        $this->shippingAddressId = $shippingAddressId;
80
        $this->billingAddressId = $billingAddressId;
81
        $this->shippingMethodCode = $shippingMethodCode;
82
        $this->paymentMethodCode = $paymentMethodCode;
83
    }
84
85
    public static function fromCart(Cart $cart): CustomerCheckout
86
    {
87
        $objectManager = Bootstrap::getObjectManager();
88
        return new static(
89
            $objectManager->create(AddressRepositoryInterface::class),
90
            $objectManager->create(CartRepositoryInterface::class),
91
            $objectManager->create(QuoteManagement::class),
92
            $objectManager->create(PaymentConfig::class),
93
            $cart
94
        );
95
    }
96
97
    public function withCustomerBillingAddressId(int $addressId): CustomerCheckout
98
    {
99
        $checkout = clone $this;
100
        $checkout->billingAddressId = $addressId;
101
        return $checkout;
102
    }
103
104
    public function withCustomerShippingAddressId(int $addressId): CustomerCheckout
105
    {
106
        $checkout = clone $this;
107
        $checkout->shippingAddressId = $addressId;
108
        return $checkout;
109
    }
110
111
    public function withShippingMethodCode(string $code): CustomerCheckout
112
    {
113
        $checkout = clone $this;
114
        $checkout->shippingMethodCode = $code;
115
        return $checkout;
116
    }
117
118
    public function withPaymentMethodCode(string $code): CustomerCheckout
119
    {
120
        $checkout = clone $this;
121
        $checkout->paymentMethodCode = $code;
122
        return $checkout;
123
    }
124
125
    /**
126
     * @return int Customer shipping address as configured or try default shipping address
127
     */
128
    private function getCustomerShippingAddressId(): int
129
    {
130
        return $this->shippingAddressId
131
            ?? (int) $this->cart->getCustomerSession()->getCustomer()->getDefaultShippingAddress()->getId();
132
    }
133
134
    /**
135
     * @return int Customer billing address as configured or try default billing address
136
     */
137
    private function getCustomerBillingAddressId(): int
138
    {
139
        return $this->billingAddressId
140
            ?? (int) $this->cart->getCustomerSession()->getCustomer()->getDefaultBillingAddress()->getId();
141
    }
142
143
    /**
144
     * @return string Shipping method code as configured, or try first available rate
145
     */
146
    private function getShippingMethodCode(): string
147
    {
148
        return $this->shippingMethodCode
149
            ?? $this->cart->getQuote()->getShippingAddress()->getAllShippingRates()[0]->getCode();
150
    }
151
152
    /**
153
     * @return string Payment method code as configured, or try first available method
154
     */
155
    private function getPaymentMethodCode(): string
156
    {
157
        return $this->paymentMethodCode ?? array_values($this->paymentConfig->getActiveMethods())[0]->getCode();
158
    }
159
160
    /**
161
     * @return OrderInterface
162
     * @throws \Exception
163
     */
164
    public function placeOrder(): OrderInterface
165
    {
166
        $this->saveBilling();
167
        $this->saveShipping();
168
        $this->savePayment();
169
        /** @var Quote $reloadedQuote */
170
        $reloadedQuote = $this->quoteRepository->get($this->cart->getQuote()->getId());
171
        // Collect missing totals, like shipping
172
        $reloadedQuote->collectTotals();
173
        $order = $this->quoteManagement->submit($reloadedQuote);
174
        $this->cart->getCheckoutSession()->clearQuote();
175
        return $order;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $order could return the type null which is incompatible with the type-hinted return Magento\Sales\Api\Data\OrderInterface. Consider adding an additional type-check to rule them out.
Loading history...
176
    }
177
178
    /**
179
     * @throws \Exception
180
     */
181
    private function saveBilling(): void
182
    {
183
        $billingAddress = $this->cart->getQuote()->getBillingAddress();
184
        $billingAddress->importCustomerAddressData(
185
            $this->addressRepository->getById($this->getCustomerBillingAddressId())
186
        );
187
        $billingAddress->save();
0 ignored issues
show
Deprecated Code introduced by
The function Magento\Framework\Model\AbstractModel::save() has been deprecated: 100.1.0 because entities must not be responsible for their own persistence. Service contracts should persist entities. Use resource model "save" to implement service contract persistence operations. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

187
        /** @scrutinizer ignore-deprecated */ $billingAddress->save();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
188
    }
189
190
    /**
191
     * @throws \Exception
192
     */
193
    private function saveShipping(): void
194
    {
195
        $shippingAddress = $this->cart->getQuote()->getShippingAddress();
196
        $shippingAddress->importCustomerAddressData(
197
            $this->addressRepository->getById($this->getCustomerShippingAddressId())
198
        );
199
        $shippingAddress->setCollectShippingRates(true);
200
        $shippingAddress->collectShippingRates();
201
        $shippingAddress->setShippingMethod($this->getShippingMethodCode());
202
        $shippingAddress->save();
0 ignored issues
show
Deprecated Code introduced by
The function Magento\Framework\Model\AbstractModel::save() has been deprecated: 100.1.0 because entities must not be responsible for their own persistence. Service contracts should persist entities. Use resource model "save" to implement service contract persistence operations. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

202
        /** @scrutinizer ignore-deprecated */ $shippingAddress->save();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
203
    }
204
205
    /**
206
     * @throws \Exception
207
     */
208
    private function savePayment(): void
209
    {
210
        $payment = $this->cart->getQuote()->getPayment();
211
        $payment->setMethod($this->getPaymentMethodCode());
212
        $payment->save();
0 ignored issues
show
Deprecated Code introduced by
The function Magento\Framework\Model\AbstractModel::save() has been deprecated: 100.1.0 because entities must not be responsible for their own persistence. Service contracts should persist entities. Use resource model "save" to implement service contract persistence operations. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

212
        /** @scrutinizer ignore-deprecated */ $payment->save();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
213
    }
214
}
215