10
10
use Magento \Framework \GraphQl \Config \Element \Field ;
11
11
use Magento \Framework \GraphQl \Exception \GraphQlAuthorizationException ;
12
12
use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
13
+ use Magento \Framework \GraphQl \Exception \GraphQlNoSuchEntityException ;
13
14
use Magento \Framework \GraphQl \Query \ResolverInterface ;
14
15
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
15
16
use Magento \Wishlist \Model \ResourceModel \Wishlist as WishlistResourceModel ;
16
17
use Magento \Wishlist \Model \Wishlist ;
17
18
use Magento \Wishlist \Model \Wishlist \Config as WishlistConfig ;
18
19
use Magento \Wishlist \Model \Wishlist \Data \Error ;
19
20
use Magento \Wishlist \Model \Wishlist \Data \WishlistItemFactory ;
20
- use Magento \Wishlist \Model \Wishlist \UpdateProductsInWishlist as UpdateProductsInWishlistModel ;
21
21
use Magento \Wishlist \Model \WishlistFactory ;
22
22
use Magento \WishlistGraphQl \Mapper \WishlistDataMapper ;
23
+ use Magento \WishlistGraphQl \Model \UpdateWishlistItem ;
23
24
24
25
/**
25
26
* Update wishlist items resolver
26
27
*/
27
28
class UpdateProductsInWishlist implements ResolverInterface
28
29
{
29
30
/**
30
- * @var UpdateProductsInWishlistModel
31
+ * @var UpdateWishlistItem
31
32
*/
32
- private $ updateProductsInWishlist ;
33
+ private $ updateWishlistItem ;
33
34
34
35
/**
35
36
* @var WishlistDataMapper
@@ -55,20 +56,20 @@ class UpdateProductsInWishlist implements ResolverInterface
55
56
* @param WishlistResourceModel $wishlistResource
56
57
* @param WishlistFactory $wishlistFactory
57
58
* @param WishlistConfig $wishlistConfig
58
- * @param UpdateProductsInWishlistModel $updateProductsInWishlist
59
+ * @param UpdateWishlistItem $updateWishlistItem
59
60
* @param WishlistDataMapper $wishlistDataMapper
60
61
*/
61
62
public function __construct (
62
63
WishlistResourceModel $ wishlistResource ,
63
64
WishlistFactory $ wishlistFactory ,
64
65
WishlistConfig $ wishlistConfig ,
65
- UpdateProductsInWishlistModel $ updateProductsInWishlist ,
66
+ UpdateWishlistItem $ updateWishlistItem ,
66
67
WishlistDataMapper $ wishlistDataMapper
67
68
) {
68
69
$ this ->wishlistResource = $ wishlistResource ;
69
70
$ this ->wishlistFactory = $ wishlistFactory ;
70
71
$ this ->wishlistConfig = $ wishlistConfig ;
71
- $ this ->updateProductsInWishlist = $ updateProductsInWishlist ;
72
+ $ this ->updateWishlistItem = $ updateWishlistItem ;
72
73
$ this ->wishlistDataMapper = $ wishlistDataMapper ;
73
74
}
74
75
@@ -83,34 +84,32 @@ public function resolve(
83
84
array $ args = null
84
85
) {
85
86
if (!$ this ->wishlistConfig ->isEnabled ()) {
86
- throw new GraphQlInputException (__ ('The wishlist configuration is currently disabled. ' ));
87
+ throw new GraphQlInputException (__ ('The wishlist configuration is currently disabled ' ));
87
88
}
88
89
89
90
$ customerId = $ context ->getUserId ();
90
91
91
- /* Guest checking */
92
92
if (null === $ customerId || $ customerId === 0 ) {
93
93
throw new GraphQlAuthorizationException (__ ('The current user cannot perform operations on wishlist ' ));
94
94
}
95
95
96
- $ wishlistId = ((int ) $ args ['wishlistId ' ]) ?: null ;
97
- $ wishlist = $ this ->getWishlist ($ wishlistId , $ customerId );
96
+ $ wishlist = $ this ->getWishlist ((int ) $ args ['wishlistId ' ], $ customerId );
98
97
99
98
if (null === $ wishlist ->getId () || $ customerId !== (int ) $ wishlist ->getCustomerId ()) {
100
- throw new GraphQlInputException (__ ('The wishlist was not found. ' ));
99
+ throw new GraphQlInputException (__ ('Could not find the specified wishlist ' ));
101
100
}
102
101
103
- $ wishlistItems = $ args ['wishlistItems ' ];
104
- $ wishlistItems = $ this ->getWishlistItems ($ wishlistItems , $ wishlist );
105
- $ wishlistOutput = $ this ->updateProductsInWishlist ->execute ($ wishlist , $ wishlistItems );
102
+ $ wishlistItems = $ this ->getWishlistItems ($ args ['wishlistItems ' ], $ wishlist );
106
103
107
- if ( count ( $ wishlistOutput -> getErrors ()) !== count ( $ wishlistItems ) ) {
108
- $ this ->wishlistResource -> save ( $ wishlist );
104
+ foreach ( $ wishlistItems as $ wishlistItem ) {
105
+ $ this ->updateWishlistItem -> execute ( $ wishlistItem , $ wishlist );
109
106
}
110
107
108
+ $ wishlistOutput = $ this ->updateWishlistItem ->prepareOutput ($ wishlist );
109
+
111
110
return [
112
111
'wishlist ' => $ this ->wishlistDataMapper ->map ($ wishlistOutput ->getWishlist ()),
113
- 'user_errors ' => \ array_map (
112
+ 'user_errors ' => array_map (
114
113
function (Error $ error ) {
115
114
return [
116
115
'code ' => $ error ->getCode (),
@@ -133,7 +132,6 @@ function (Error $error) {
133
132
private function getWishlistItems (array $ wishlistItemsData , Wishlist $ wishlist ): array
134
133
{
135
134
$ wishlistItems = [];
136
-
137
135
foreach ($ wishlistItemsData as $ wishlistItemData ) {
138
136
if (!isset ($ wishlistItemData ['quantity ' ])) {
139
137
$ wishlistItem = $ wishlist ->getItem ($ wishlistItemData ['wishlist_item_id ' ]);
@@ -149,7 +147,6 @@ private function getWishlistItems(array $wishlistItemsData, Wishlist $wishlist):
149
147
}
150
148
$ wishlistItems [] = (new WishlistItemFactory ())->create ($ wishlistItemData );
151
149
}
152
-
153
150
return $ wishlistItems ;
154
151
}
155
152
0 commit comments