1010use Magento \Framework \GraphQl \Config \Element \Field ;
1111use Magento \Framework \GraphQl \Exception \GraphQlAuthorizationException ;
1212use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
13+ use Magento \Framework \GraphQl \Exception \GraphQlNoSuchEntityException ;
1314use Magento \Framework \GraphQl \Query \ResolverInterface ;
1415use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
1516use Magento \Wishlist \Model \ResourceModel \Wishlist as WishlistResourceModel ;
1617use Magento \Wishlist \Model \Wishlist ;
1718use Magento \Wishlist \Model \Wishlist \Config as WishlistConfig ;
1819use Magento \Wishlist \Model \Wishlist \Data \Error ;
1920use Magento \Wishlist \Model \Wishlist \Data \WishlistItemFactory ;
20- use Magento \Wishlist \Model \Wishlist \UpdateProductsInWishlist as UpdateProductsInWishlistModel ;
2121use Magento \Wishlist \Model \WishlistFactory ;
2222use Magento \WishlistGraphQl \Mapper \WishlistDataMapper ;
23+ use Magento \WishlistGraphQl \Model \UpdateWishlistItem ;
2324
2425/**
2526 * Update wishlist items resolver
2627 */
2728class UpdateProductsInWishlist implements ResolverInterface
2829{
2930 /**
30- * @var UpdateProductsInWishlistModel
31+ * @var UpdateWishlistItem
3132 */
32- private $ updateProductsInWishlist ;
33+ private $ updateWishlistItem ;
3334
3435 /**
3536 * @var WishlistDataMapper
@@ -55,20 +56,20 @@ class UpdateProductsInWishlist implements ResolverInterface
5556 * @param WishlistResourceModel $wishlistResource
5657 * @param WishlistFactory $wishlistFactory
5758 * @param WishlistConfig $wishlistConfig
58- * @param UpdateProductsInWishlistModel $updateProductsInWishlist
59+ * @param UpdateWishlistItem $updateWishlistItem
5960 * @param WishlistDataMapper $wishlistDataMapper
6061 */
6162 public function __construct (
6263 WishlistResourceModel $ wishlistResource ,
6364 WishlistFactory $ wishlistFactory ,
6465 WishlistConfig $ wishlistConfig ,
65- UpdateProductsInWishlistModel $ updateProductsInWishlist ,
66+ UpdateWishlistItem $ updateWishlistItem ,
6667 WishlistDataMapper $ wishlistDataMapper
6768 ) {
6869 $ this ->wishlistResource = $ wishlistResource ;
6970 $ this ->wishlistFactory = $ wishlistFactory ;
7071 $ this ->wishlistConfig = $ wishlistConfig ;
71- $ this ->updateProductsInWishlist = $ updateProductsInWishlist ;
72+ $ this ->updateWishlistItem = $ updateWishlistItem ;
7273 $ this ->wishlistDataMapper = $ wishlistDataMapper ;
7374 }
7475
@@ -83,34 +84,32 @@ public function resolve(
8384 array $ args = null
8485 ) {
8586 if (!$ this ->wishlistConfig ->isEnabled ()) {
86- throw new GraphQlInputException (__ ('The wishlist configuration is currently disabled. ' ));
87+ throw new GraphQlInputException (__ ('The wishlist configuration is currently disabled ' ));
8788 }
8889
8990 $ customerId = $ context ->getUserId ();
9091
91- /* Guest checking */
9292 if (null === $ customerId || $ customerId === 0 ) {
9393 throw new GraphQlAuthorizationException (__ ('The current user cannot perform operations on wishlist ' ));
9494 }
9595
96- $ wishlistId = ((int ) $ args ['wishlistId ' ]) ?: null ;
97- $ wishlist = $ this ->getWishlist ($ wishlistId , $ customerId );
96+ $ wishlist = $ this ->getWishlist ((int ) $ args ['wishlistId ' ], $ customerId );
9897
9998 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 ' ));
101100 }
102101
103- $ wishlistItems = $ args ['wishlistItems ' ];
104- $ wishlistItems = $ this ->getWishlistItems ($ wishlistItems , $ wishlist );
105- $ wishlistOutput = $ this ->updateProductsInWishlist ->execute ($ wishlist , $ wishlistItems );
102+ $ wishlistItems = $ this ->getWishlistItems ($ args ['wishlistItems ' ], $ wishlist );
106103
107- if ( count ( $ wishlistOutput -> getErrors ()) !== count ( $ wishlistItems ) ) {
108- $ this ->wishlistResource -> save ( $ wishlist );
104+ foreach ( $ wishlistItems as $ wishlistItem ) {
105+ $ this ->updateWishlistItem -> execute ( $ wishlistItem , $ wishlist );
109106 }
110107
108+ $ wishlistOutput = $ this ->updateWishlistItem ->prepareOutput ($ wishlist );
109+
111110 return [
112111 'wishlist ' => $ this ->wishlistDataMapper ->map ($ wishlistOutput ->getWishlist ()),
113- 'user_errors ' => \ array_map (
112+ 'user_errors ' => array_map (
114113 function (Error $ error ) {
115114 return [
116115 'code ' => $ error ->getCode (),
@@ -133,7 +132,6 @@ function (Error $error) {
133132 private function getWishlistItems (array $ wishlistItemsData , Wishlist $ wishlist ): array
134133 {
135134 $ wishlistItems = [];
136-
137135 foreach ($ wishlistItemsData as $ wishlistItemData ) {
138136 if (!isset ($ wishlistItemData ['quantity ' ])) {
139137 $ wishlistItem = $ wishlist ->getItem ($ wishlistItemData ['wishlist_item_id ' ]);
@@ -149,7 +147,6 @@ private function getWishlistItems(array $wishlistItemsData, Wishlist $wishlist):
149147 }
150148 $ wishlistItems [] = (new WishlistItemFactory ())->create ($ wishlistItemData );
151149 }
152-
153150 return $ wishlistItems ;
154151 }
155152
0 commit comments