CustomerCheckout   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 202
Duplicated Lines 0 %

Test Coverage

Coverage 86.84%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
eloc 70
c 5
b 1
f 0
dl 0
loc 202
ccs 66
cts 76
cp 0.8684
rs 10
wmc 16

14 Methods

Rating   Name   Duplication   Size   Complexity  
A withPaymentMethodCode() 0 5 1
A getPaymentMethodCode() 0 3 1
A getShippingMethodCode() 0 4 1
A withCustomerShippingAddressId() 0 5 1
A withCustomerBillingAddressId() 0 5 1
A __construct() 0 21 1
A fromCart() 0 9 1
A getCustomerShippingAddressId() 0 4 1
A withShippingMethodCode() 0 5 1
A getCustomerBillingAddressId() 0 4 1
A savePayment() 0 5 1
A saveShipping() 0 10 1
A placeOrder() 0 16 3
A saveBilling() 0 7 1
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\Model\Order;
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 16
    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 16
        $this->addressRepository = $addressRepository;
75 16
        $this->quoteRepository = $quoteRepository;
76 16
        $this->quoteManagement = $quoteManagement;
77 16
        $this->paymentConfig = $paymentConfig;
78 16
        $this->cart = $cart;
79 16
        $this->shippingAddressId = $shippingAddressId;
80 16
        $this->billingAddressId = $billingAddressId;
81 16
        $this->shippingMethodCode = $shippingMethodCode;
82 16
        $this->paymentMethodCode = $paymentMethodCode;
83 16
    }
84
85 16
    public static function fromCart(Cart $cart): CustomerCheckout
86
    {
87 16
        $objectManager = Bootstrap::getObjectManager();
88 16
        return new static(
89 16
            $objectManager->create(AddressRepositoryInterface::class),
90 16
            $objectManager->create(CartRepositoryInterface::class),
91 16
            $objectManager->create(QuoteManagement::class),
92 16
            $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 2
    public function withShippingMethodCode(string $code): CustomerCheckout
112
    {
113 2
        $checkout = clone $this;
114 2
        $checkout->shippingMethodCode = $code;
115 2
        return $checkout;
116
    }
117
118 3
    public function withPaymentMethodCode(string $code): CustomerCheckout
119
    {
120 3
        $checkout = clone $this;
121 3
        $checkout->paymentMethodCode = $code;
122 3
        return $checkout;
123
    }
124
125
    /**
126
     * @return int Customer shipping address as configured or try default shipping address
127
     */
128 16
    private function getCustomerShippingAddressId(): int
129
    {
130 16
        return $this->shippingAddressId
131 16
            ?? (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 16
    private function getCustomerBillingAddressId(): int
138
    {
139 16
        return $this->billingAddressId
140 16
            ?? (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 16
    private function getShippingMethodCode(): string
147
    {
148 16
        return $this->shippingMethodCode
149 16
            ?? $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 16
    private function getPaymentMethodCode(): string
156
    {
157 16
        return $this->paymentMethodCode ?? array_values($this->paymentConfig->getActiveMethods())[0]->getCode();
158
    }
159
160
    /**
161
     * @return Order
162
     * @throws \Exception
163
     */
164 16
    public function placeOrder(): Order
165
    {
166 16
        $this->saveBilling();
167 16
        $this->saveShipping();
168 16
        $this->savePayment();
169
        /** @var Quote $reloadedQuote */
170 16
        $reloadedQuote = $this->quoteRepository->get($this->cart->getQuote()->getId());
171
        // Collect missing totals, like shipping
172 16
        $reloadedQuote->collectTotals();
173 16
        $order = $this->quoteManagement->submit($reloadedQuote);
174 16
        if (! $order instanceof Order) {
175
            $returnType = is_object($order) ? get_class($order) : gettype($order);
176
            throw new \RuntimeException('QuoteManagement::submit() returned ' . $returnType . ' instead of Order');
177
        }
178 16
        $this->cart->getCheckoutSession()->clearQuote();
179 16
        return $order;
180
    }
181
182
    /**
183
     * @throws \Exception
184
     */
185 16
    private function saveBilling(): void
186
    {
187 16
        $billingAddress = $this->cart->getQuote()->getBillingAddress();
188 16
        $billingAddress->importCustomerAddressData(
189 16
            $this->addressRepository->getById($this->getCustomerBillingAddressId())
190
        );
191 16
        $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

191
        /** @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...
192 16
    }
193
194
    /**
195
     * @throws \Exception
196
     */
197 16
    private function saveShipping(): void
198
    {
199 16
        $shippingAddress = $this->cart->getQuote()->getShippingAddress();
200 16
        $shippingAddress->importCustomerAddressData(
201 16
            $this->addressRepository->getById($this->getCustomerShippingAddressId())
202
        );
203 16
        $shippingAddress->setCollectShippingRates(true);
204 16
        $shippingAddress->collectShippingRates();
205 16
        $shippingAddress->setShippingMethod($this->getShippingMethodCode());
206 16
        $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

206
        /** @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...
207 16
    }
208
209
    /**
210
     * @throws \Exception
211
     */
212 16
    private function savePayment(): void
213
    {
214 16
        $payment = $this->cart->getQuote()->getPayment();
215 16
        $payment->setMethod($this->getPaymentMethodCode());
216 16
        $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

216
        /** @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...
217 16
    }
218
}
219