3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \Quote \Model ;
7
9
10
+ use Magento \Customer \Api \AddressRepositoryInterface ;
11
+ use Magento \Customer \Api \CustomerRepositoryInterface ;
12
+ use Magento \Customer \Model \Session ;
13
+ use Magento \Framework \Exception \InputException ;
14
+ use Magento \Framework \Exception \LocalizedException ;
8
15
use Magento \Framework \Exception \NoSuchEntityException ;
9
16
use Magento \Quote \Api \Data \AddressInterface ;
10
17
use Magento \Quote \Api \Data \CartInterface ;
17
24
class QuoteAddressValidator
18
25
{
19
26
/**
20
- * Address factory.
21
- *
22
- * @var \Magento\Customer\Api\AddressRepositoryInterface
27
+ * @var AddressRepositoryInterface
23
28
*/
24
- protected $ addressRepository ;
29
+ protected AddressRepositoryInterface $ addressRepository ;
25
30
26
31
/**
27
- * Customer repository.
28
- *
29
- * @var \Magento\Customer\Api\CustomerRepositoryInterface
32
+ * @var CustomerRepositoryInterface
30
33
*/
31
- protected $ customerRepository ;
34
+ protected CustomerRepositoryInterface $ customerRepository ;
32
35
33
36
/**
37
+ * @var Session
34
38
* @deprecated 101.1.1 This class is not a part of HTML presentation layer and should not use sessions.
39
+ * @see Session
35
40
*/
36
- protected $ customerSession ;
41
+ protected Session $ customerSession ;
37
42
38
43
/**
39
44
* Constructs a quote shipping address validator service object.
40
45
*
41
- * @param \Magento\Customer\Api\ AddressRepositoryInterface $addressRepository
42
- * @param \Magento\Customer\Api\ CustomerRepositoryInterface $customerRepository Customer repository.
43
- * @param \Magento\Customer\Model\ Session $customerSession
46
+ * @param AddressRepositoryInterface $addressRepository
47
+ * @param CustomerRepositoryInterface $customerRepository Customer repository.
48
+ * @param Session $customerSession
44
49
*/
45
50
public function __construct (
46
- \ Magento \ Customer \ Api \ AddressRepositoryInterface $ addressRepository ,
47
- \ Magento \ Customer \ Api \ CustomerRepositoryInterface $ customerRepository ,
48
- \ Magento \ Customer \ Model \ Session $ customerSession
51
+ AddressRepositoryInterface $ addressRepository ,
52
+ CustomerRepositoryInterface $ customerRepository ,
53
+ Session $ customerSession
49
54
) {
50
55
$ this ->addressRepository = $ addressRepository ;
51
56
$ this ->customerRepository = $ customerRepository ;
@@ -56,18 +61,18 @@ public function __construct(
56
61
* Validate address.
57
62
*
58
63
* @param AddressInterface $address
59
- * @param int|null $customerId Cart belongs to
64
+ * @param int|null $customerId
60
65
* @return void
61
- * @throws \Magento\Framework\Exception\InputException The specified address belongs to another customer .
62
- * @throws \Magento\Framework\Exception\ NoSuchEntityException The specified customer ID or address ID is not valid.
66
+ * @throws LocalizedException The specified customer ID or address ID is not valid .
67
+ * @throws NoSuchEntityException The specified customer ID or address ID is not valid.
63
68
*/
64
69
private function doValidate (AddressInterface $ address , ?int $ customerId ): void
65
70
{
66
71
//validate customer id
67
72
if ($ customerId ) {
68
73
$ customer = $ this ->customerRepository ->getById ($ customerId );
69
74
if (!$ customer ->getId ()) {
70
- throw new \ Magento \ Framework \ Exception \ NoSuchEntityException (
75
+ throw new NoSuchEntityException (
71
76
__ ('Invalid customer id %1 ' , $ customerId )
72
77
);
73
78
}
@@ -76,15 +81,15 @@ private function doValidate(AddressInterface $address, ?int $customerId): void
76
81
if ($ address ->getCustomerAddressId ()) {
77
82
//Existing address cannot belong to a guest
78
83
if (!$ customerId ) {
79
- throw new \ Magento \ Framework \ Exception \ NoSuchEntityException (
84
+ throw new NoSuchEntityException (
80
85
__ ('Invalid customer address id %1 ' , $ address ->getCustomerAddressId ())
81
86
);
82
87
}
83
88
//Validating address ID
84
89
try {
85
90
$ this ->addressRepository ->getById ($ address ->getCustomerAddressId ());
86
91
} catch (NoSuchEntityException $ e ) {
87
- throw new \ Magento \ Framework \ Exception \ NoSuchEntityException (
92
+ throw new NoSuchEntityException (
88
93
__ ('Invalid address id %1 ' , $ address ->getId ())
89
94
);
90
95
}
@@ -94,7 +99,7 @@ private function doValidate(AddressInterface $address, ?int $customerId): void
94
99
return $ address ->getId ();
95
100
}, $ this ->customerRepository ->getById ($ customerId )->getAddresses ());
96
101
if (!in_array ($ address ->getCustomerAddressId (), $ applicableAddressIds )) {
97
- throw new \ Magento \ Framework \ Exception \ NoSuchEntityException (
102
+ throw new NoSuchEntityException (
98
103
__ ('Invalid customer address id %1 ' , $ address ->getCustomerAddressId ())
99
104
);
100
105
}
@@ -104,29 +109,53 @@ private function doValidate(AddressInterface $address, ?int $customerId): void
104
109
/**
105
110
* Validates the fields in a specified address data object.
106
111
*
107
- * @param \Magento\Quote\Api\Data\ AddressInterface $addressData The address data object.
112
+ * @param AddressInterface $addressData The address data object.
108
113
* @return bool
109
- * @throws \Magento\Framework\Exception\ InputException The specified address belongs to another customer.
110
- * @throws \Magento\Framework\Exception\ NoSuchEntityException The specified customer ID or address ID is not valid.
114
+ * @throws InputException The specified address belongs to another customer.
115
+ * @throws NoSuchEntityException|LocalizedException The specified customer ID or address ID is not valid.
111
116
*/
112
- public function validate (AddressInterface $ addressData )
117
+ public function validate (AddressInterface $ addressData ): bool
113
118
{
114
119
$ this ->doValidate ($ addressData , $ addressData ->getCustomerId ());
115
120
116
121
return true ;
117
122
}
118
123
124
+ /**
125
+ * Validate Quest Address for guest user
126
+ *
127
+ * @param AddressInterface $address
128
+ * @param CartInterface $cart
129
+ * @return void
130
+ * @throws NoSuchEntityException
131
+ */
132
+ private function doValidateForGuestQuoteAddress (AddressInterface $ address , CartInterface $ cart ): void
133
+ {
134
+ //validate guest cart address
135
+ if ($ address ->getId () !== null ) {
136
+ $ old = $ cart ->getAddressesCollection ()->getItemById ($ address ->getId ());
137
+ if ($ old === null ) {
138
+ throw new NoSuchEntityException (
139
+ __ ('Invalid quote address id %1 ' , $ address ->getId ())
140
+ );
141
+ }
142
+ }
143
+ }
144
+
119
145
/**
120
146
* Validate address to be used for cart.
121
147
*
122
148
* @param CartInterface $cart
123
149
* @param AddressInterface $address
124
150
* @return void
125
- * @throws \Magento\Framework\Exception\ InputException The specified address belongs to another customer.
126
- * @throws \Magento\Framework\Exception\ NoSuchEntityException The specified customer ID or address ID is not valid.
151
+ * @throws InputException The specified address belongs to another customer.
152
+ * @throws NoSuchEntityException|LocalizedException The specified customer ID or address ID is not valid.
127
153
*/
128
154
public function validateForCart (CartInterface $ cart , AddressInterface $ address ): void
129
155
{
130
- $ this ->doValidate ($ address , $ cart ->getCustomerIsGuest () ? null : $ cart ->getCustomer ()->getId ());
156
+ if ($ cart ->getCustomerIsGuest ()) {
157
+ $ this ->doValidateForGuestQuoteAddress ($ address , $ cart );
158
+ }
159
+ $ this ->doValidate ($ address , $ cart ->getCustomerIsGuest () ? null : (int ) $ cart ->getCustomer ()->getId ());
131
160
}
132
161
}
0 commit comments