Completed
Push — master ( 971d31...2154ff )
by Fabian
10:09
created

CustomerCheckout::getCustomerShippingAddressId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TddWizard\Fixtures\Checkout;
4
5
use Magento\Checkout\Model\Cart;
6
use Magento\Customer\Api\AddressRepositoryInterface;
7
use Magento\Framework\ObjectManagerInterface;
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
     * @var CartRepositoryInterface
23
     */
24
    private $quoteRepository;
25
    /**
26
     * @var QuoteManagement
27
     */
28
    private $quoteManagement;
29
    /**
30
     * @var PaymentConfig
31
     */
32
    private $paymentConfig;
33
    /**
34
     * @var Cart
35
     */
36
    private $cart;
37
    /**
38
     * @var int|null
39
     */
40
    private $customerShippingAddressId;
41
    /**
42
     * @var int|null
43
     */
44
    private $customerBillingAddressId;
45
    /**
46
     * @var string|null
47
     */
48
    private $shippingMethodCode;
49
    /**
50
     * @var string|null
51
     */
52
    private $paymentMethodCode;
53
54
    public function __construct(
55
        AddressRepositoryInterface $addressRepository,
56
        CartRepositoryInterface $quoteRepository,
57
        QuoteManagement $quoteManagement,
58
        PaymentConfig $paymentConfig,
59
        Cart $cart,
60
        $customerShippingAddressId = null,
61
        $customerBillingAddressId = null,
62
        $shippingMethodCode = null,
63
        $paymentMethodCode = null
64
    ) {
65
    
66
        $this->addressRepository = $addressRepository;
67
        $this->quoteRepository = $quoteRepository;
68
        $this->quoteManagement = $quoteManagement;
69
        $this->paymentConfig = $paymentConfig;
70
        $this->cart = $cart;
71
        $this->customerShippingAddressId = $customerShippingAddressId;
72
        $this->customerBillingAddressId = $customerBillingAddressId;
73
        $this->shippingMethodCode = $shippingMethodCode;
74
        $this->paymentMethodCode = $paymentMethodCode;
75
    }
76
77
    public static function fromCart(Cart $cart, ObjectManagerInterface $objectManager = null) : CustomerCheckout
78
    {
79
        if ($objectManager === null) {
80
            $objectManager = Bootstrap::getObjectManager();
81
        }
82
        return new static(
83
            $objectManager->create(AddressRepositoryInterface::class),
84
            $objectManager->create(CartRepositoryInterface::class),
85
            $objectManager->create(QuoteManagement::class),
86
            $objectManager->create(PaymentConfig::class),
87
            $cart
88
        );
89
    }
90
91
    public function withCustomerBillingAddressId(int $addressId) : CustomerCheckout
92
    {
93
        $checkout = clone $this;
94
        $checkout->customerBillingAddressId = $addressId;
95
        return $checkout;
96
    }
97
98
    public function withCustomerShippingAddressId(int $addressId) : CustomerCheckout
99
    {
100
        $checkout = clone $this;
101
        $checkout->customerShippingAddressId = $addressId;
102
        return $checkout;
103
    }
104
105
    public function withShippingMethodCode(string $code) : CustomerCheckout
106
    {
107
        $checkout = clone $this;
108
        $checkout->shippingMethodCode = $code;
109
        return $checkout;
110
    }
111
112
    public function withPaymentMethodCode(string $code) : CustomerCheckout
113
    {
114
        $checkout = clone $this;
115
        $checkout->paymentMethodCode = $code;
116
        return $checkout;
117
    }
118
119
    /**
120
     * @return int Customer shipping address as configured or try default shipping address
121
     */
122
    private function getCustomerShippingAddressId() : int
123
    {
124
        return $this->customerShippingAddressId
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->customerSh...ppingAddress()->getId() could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
125
            ?? $this->cart->getCustomerSession()->getCustomer()->getDefaultShippingAddress()->getId();
126
    }
127
128
129
    /**
130
     * @return int Customer billing address as configured or try default billing address
131
     */
132
    private function getCustomerBillingAddressId() : int
133
    {
134
        return $this->customerBillingAddressId
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->customerBi...llingAddress()->getId() could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
135
            ?? $this->cart->getCustomerSession()->getCustomer()->getDefaultBillingAddress()->getId();
136
    }
137
138
    /**
139
     * @return string Shipping method code as configured, or try first available rate
140
     */
141
    private function getShippingMethodCode() : string
142
    {
143
        return $this->shippingMethodCode
144
            ?? $this->cart->getQuote()->getShippingAddress()->getAllShippingRates()[0]->getCode();
145
    }
146
147
    /**
148
     * @return string Payment method code as configured, or try first available method
149
     */
150
    private function getPaymentMethodCode() : string
151
    {
152
        return $this->paymentMethodCode ?? array_values($this->paymentConfig->getActiveMethods())[0]->getCode();
153
    }
154
155
    public function placeOrder() : OrderInterface
156
    {
157
        $this->saveBilling();
158
        $this->saveShipping();
159
        $this->savePayment();
160
        /** @var Quote $reloadedQuote */
161
        $reloadedQuote = $this->quoteRepository->get($this->cart->getQuote()->getId());
162
        // Collect missing totals, like shipping
163
        $reloadedQuote->collectTotals();
164
        return $this->quoteManagement->submit($reloadedQuote);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->quoteManag...>submit($reloadedQuote) could return the type null|Magento\Framework\M...AbstractExtensibleModel 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...
165
    }
166
167
    private function saveBilling()
168
    {
169
        $billingAddress = $this->cart->getQuote()->getBillingAddress();
170
        $billingAddress->importCustomerAddressData(
171
            $this->addressRepository->getById($this->getCustomerShippingAddressId())
172
        );
173
        $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

173
        /** @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...
174
    }
175
176
    private function saveShipping()
177
    {
178
        $shippingAddress = $this->cart->getQuote()->getShippingAddress();
179
        $shippingAddress->importCustomerAddressData(
180
            $this->addressRepository->getById($this->getCustomerBillingAddressId())
181
        );
182
        $shippingAddress->setCollectShippingRates(true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type integer expected by parameter $value of Magento\Quote\Model\Quot...tCollectShippingRates(). ( Ignorable by Annotation )

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

182
        $shippingAddress->setCollectShippingRates(/** @scrutinizer ignore-type */ true);
Loading history...
183
        $shippingAddress->collectShippingRates();
184
        $shippingAddress->setShippingMethod($this->getShippingMethodCode());
185
        $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

185
        /** @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...
186
    }
187
188
    private function savePayment()
189
    {
190
        $payment = $this->cart->getQuote()->getPayment();
191
        $payment->setMethod($this->getPaymentMethodCode());
192
        $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

192
        /** @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...
193
    }
194
}
195