13
13
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
14
14
use Magento \QuoteGraphQl \Model \Cart \AddProductsToCart ;
15
15
use Magento \QuoteGraphQl \Model \Cart \GetCartForUser ;
16
+ use Magento \Framework \Lock \LockManagerInterface ;
16
17
17
18
/**
18
19
* Add simple products to cart GraphQl resolver
@@ -30,16 +31,24 @@ class AddSimpleProductsToCart implements ResolverInterface
30
31
*/
31
32
private $ addProductsToCart ;
32
33
34
+ /**
35
+ * @var LockManagerInterface
36
+ */
37
+ private $ lockManager ;
38
+
33
39
/**
34
40
* @param GetCartForUser $getCartForUser
35
41
* @param AddProductsToCart $addProductsToCart
42
+ * @param LockManagerInterface $lockManager
36
43
*/
37
44
public function __construct (
38
45
GetCartForUser $ getCartForUser ,
39
- AddProductsToCart $ addProductsToCart
46
+ AddProductsToCart $ addProductsToCart ,
47
+ LockManagerInterface $ lockManager
40
48
) {
41
49
$ this ->getCartForUser = $ getCartForUser ;
42
50
$ this ->addProductsToCart = $ addProductsToCart ;
51
+ $ this ->lockManager = $ lockManager ;
43
52
}
44
53
45
54
/**
@@ -58,12 +67,20 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
58
67
throw new GraphQlInputException (__ ('Required parameter "cart_items" is missing ' ));
59
68
}
60
69
$ cartItems = $ args ['input ' ]['cart_items ' ];
61
-
62
70
$ storeId = (int )$ context ->getExtensionAttributes ()->getStore ()->getId ();
71
+
72
+ $ lockName = 'cart_processing_lock_ ' . $ maskedCartId ;
73
+ while ($ this ->lockManager ->isLocked ($ lockName )) {
74
+ // wait till other process working with the same cart complete
75
+ usleep (rand (100 , 600 ));
76
+ }
77
+ $ this ->lockManager ->lock ($ lockName , 1 );
78
+
63
79
$ cart = $ this ->getCartForUser ->execute ($ maskedCartId , $ context ->getUserId (), $ storeId );
64
80
$ this ->addProductsToCart ->execute ($ cart , $ cartItems );
65
-
66
81
$ cart = $ this ->getCartForUser ->execute ($ maskedCartId , $ context ->getUserId (), $ storeId );
82
+
83
+ $ this ->lockManager ->unlock ($ lockName );
67
84
return [
68
85
'cart ' => [
69
86
'model ' => $ cart ,
0 commit comments