7
7
8
8
namespace Magento \Quote \Model ;
9
9
10
+ use Magento \Customer \Api \AddressRepositoryInterface ;
10
11
use Magento \Framework \App \ObjectManager ;
11
12
use Magento \Framework \Exception \InputException ;
12
13
use Magento \Quote \Api \BillingAddressManagementInterface ;
14
+ use Magento \Quote \Api \CartRepositoryInterface ;
13
15
use Magento \Quote \Api \Data \AddressInterface ;
14
16
use Psr \Log \LoggerInterface as Logger ;
15
17
16
18
/**
17
19
* Quote billing address write service object.
20
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
18
21
*/
19
22
class BillingAddressManagement implements BillingAddressManagementInterface
20
23
{
24
+ /**
25
+ * Billing address lock const
26
+ *
27
+ * @var string
28
+ */
29
+ private const CART_BILLING_ADDRESS_LOCK = 'cart_billing_address_lock_ ' ;
30
+
21
31
/**
22
32
* Validator.
23
33
*
@@ -35,12 +45,12 @@ class BillingAddressManagement implements BillingAddressManagementInterface
35
45
/**
36
46
* Quote repository object.
37
47
*
38
- * @var \Magento\Quote\Api\ CartRepositoryInterface
48
+ * @var CartRepositoryInterface
39
49
*/
40
50
protected $ quoteRepository ;
41
51
42
52
/**
43
- * @var \Magento\Customer\Api\ AddressRepositoryInterface
53
+ * @var AddressRepositoryInterface
44
54
*/
45
55
protected $ addressRepository ;
46
56
@@ -49,24 +59,33 @@ class BillingAddressManagement implements BillingAddressManagementInterface
49
59
*/
50
60
private $ shippingAddressAssignment ;
51
61
62
+ /**
63
+ * @var CartAddressMutexInterface
64
+ */
65
+ private $ cartAddressMutex ;
66
+
52
67
/**
53
68
* Constructs a quote billing address service object.
54
69
*
55
- * @param \Magento\Quote\Api\ CartRepositoryInterface $quoteRepository Quote repository.
70
+ * @param CartRepositoryInterface $quoteRepository Quote repository.
56
71
* @param QuoteAddressValidator $addressValidator Address validator.
57
72
* @param Logger $logger Logger.
58
- * @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
73
+ * @param AddressRepositoryInterface $addressRepository
74
+ * @param CartAddressMutexInterface|null $cartAddressMutex
59
75
*/
60
76
public function __construct (
61
- \ Magento \ Quote \ Api \ CartRepositoryInterface $ quoteRepository ,
77
+ CartRepositoryInterface $ quoteRepository ,
62
78
QuoteAddressValidator $ addressValidator ,
63
79
Logger $ logger ,
64
- \Magento \Customer \Api \AddressRepositoryInterface $ addressRepository
80
+ AddressRepositoryInterface $ addressRepository ,
81
+ ?CartAddressMutexInterface $ cartAddressMutex = null
65
82
) {
66
83
$ this ->addressValidator = $ addressValidator ;
67
84
$ this ->logger = $ logger ;
68
85
$ this ->quoteRepository = $ quoteRepository ;
69
86
$ this ->addressRepository = $ addressRepository ;
87
+ $ this ->cartAddressMutex = $ cartAddressMutex ??
88
+ ObjectManager::getInstance ()->get (CartAddressMutex::class);
70
89
}
71
90
72
91
/**
@@ -75,8 +94,29 @@ public function __construct(
75
94
*/
76
95
public function assign ($ cartId , AddressInterface $ address , $ useForShipping = false )
77
96
{
78
- /** @var \Magento\Quote\Model\ Quote $quote */
97
+ /** @var Quote $quote */
79
98
$ quote = $ this ->quoteRepository ->getActive ($ cartId );
99
+ $ billingAddressId = (int ) $ quote ->getBillingAddress ()->getId ();
100
+
101
+ return $ this ->cartAddressMutex ->execute (
102
+ self ::CART_BILLING_ADDRESS_LOCK .$ billingAddressId ,
103
+ $ this ->assignBillingAddress (...),
104
+ $ billingAddressId ,
105
+ [$ address , $ quote , $ useForShipping ]
106
+ );
107
+ }
108
+
109
+ /**
110
+ * Assign billing address to cart
111
+ *
112
+ * @param AddressInterface $address
113
+ * @param Quote $quote
114
+ * @param bool $useForShipping
115
+ * @return mixed
116
+ * @throws InputException
117
+ */
118
+ private function assignBillingAddress (AddressInterface $ address , Quote $ quote , bool $ useForShipping = false )
119
+ {
80
120
$ address ->setCustomerId ($ quote ->getCustomerId ());
81
121
$ quote ->removeAddress ($ quote ->getBillingAddress ()->getId ());
82
122
$ quote ->setBillingAddress ($ address );
@@ -85,9 +125,10 @@ public function assign($cartId, AddressInterface $address, $useForShipping = fal
85
125
$ quote ->setDataChanges (true );
86
126
$ this ->quoteRepository ->save ($ quote );
87
127
} catch (\Exception $ e ) {
88
- $ this ->logger ->critical ($ e );
128
+ $ this ->logger ->critical ($ e-> getMessage () );
89
129
throw new InputException (__ ('The address failed to save. Verify the address and try again. ' ));
90
130
}
131
+
91
132
return $ quote ->getBillingAddress ()->getId ();
92
133
}
93
134
0 commit comments