10
10
use Magento \Catalog \Model \CompareList ;
11
11
use Magento \Catalog \Model \CompareListFactory ;
12
12
use Magento \Catalog \Model \ResourceModel \Product \Compare \CompareList as ResourceCompareList ;
13
+ use Magento \Catalog \Model \ResourceModel \Product \Compare \Item \Collection ;
14
+ use Magento \Catalog \Model \ResourceModel \Product \Compare \Item \CollectionFactory as CompareItemsCollectionFactory ;
15
+ use Magento \CompareListGraphQl \Model \Service \AddToCompareList ;
13
16
use Magento \Framework \GraphQl \Exception \GraphQlAuthenticationException ;
14
17
use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
15
18
use Magento \Framework \GraphQl \Exception \GraphQlNoSuchEntityException ;
19
+ use Magento \Framework \GraphQl \Query \Resolver \ContextInterface ;
16
20
17
21
/**
18
22
* Assign customer to compare list
@@ -34,6 +38,26 @@ class SetCustomerToCompareList
34
38
*/
35
39
private $ resourceCompareList ;
36
40
41
+ /**
42
+ * @var GetListIdByCustomerId
43
+ */
44
+ private $ getListIdByCustomerId ;
45
+
46
+ /**
47
+ * @var Collection
48
+ */
49
+ private $ items ;
50
+
51
+ /**
52
+ * @var CompareItemsCollectionFactory
53
+ */
54
+ private $ itemCollectionFactory ;
55
+
56
+ /**
57
+ * @var AddToCompareList
58
+ */
59
+ private $ addProductToCompareList ;
60
+
37
61
/**
38
62
* @param ValidateCustomer $validateCustomer
39
63
* @param CompareListFactory $compareListFactory
@@ -42,11 +66,17 @@ class SetCustomerToCompareList
42
66
public function __construct (
43
67
ValidateCustomer $ validateCustomer ,
44
68
CompareListFactory $ compareListFactory ,
45
- ResourceCompareList $ resourceCompareList
69
+ ResourceCompareList $ resourceCompareList ,
70
+ GetListIdByCustomerId $ getListIdByCustomerId ,
71
+ CompareItemsCollectionFactory $ itemCollectionFactory ,
72
+ AddToCompareList $ addProductToCompareList
46
73
) {
47
74
$ this ->validateCustomer = $ validateCustomer ;
48
75
$ this ->compareListFactory = $ compareListFactory ;
49
76
$ this ->resourceCompareList = $ resourceCompareList ;
77
+ $ this ->getListIdByCustomerId = $ getListIdByCustomerId ;
78
+ $ this ->itemCollectionFactory = $ itemCollectionFactory ;
79
+ $ this ->addProductToCompareList = $ addProductToCompareList ;
50
80
}
51
81
52
82
/**
@@ -61,12 +91,22 @@ public function __construct(
61
91
* @throws GraphQlInputException
62
92
* @throws GraphQlNoSuchEntityException
63
93
*/
64
- public function execute (int $ listId , int $ customerId ): ?CompareList
94
+ public function execute (int $ listId , int $ customerId, ContextInterface $ context ): ?CompareList
65
95
{
66
96
if ($ this ->validateCustomer ->execute ($ customerId )) {
67
97
/** @var CompareList $compareListModel */
68
98
$ compareList = $ this ->compareListFactory ->create ();
99
+ $ customerListId = $ this ->getListIdByCustomerId ->execute ($ customerId );
69
100
$ this ->resourceCompareList ->load ($ compareList , $ listId , 'list_id ' );
101
+ if ($ customerListId ) {
102
+ $ this ->items = $ this ->itemCollectionFactory ->create ();
103
+ $ products = $ this ->items ->getProductsByListId ($ listId );
104
+ $ this ->addProductToCompareList ->execute ($ customerListId , $ products , $ context );
105
+ $ this ->resourceCompareList ->delete ($ compareList );
106
+ $ compareList = $ this ->compareListFactory ->create ();
107
+ $ this ->resourceCompareList ->load ($ compareList , $ customerListId , 'list_id ' );
108
+ return $ compareList ;
109
+ }
70
110
$ compareList ->setCustomerId ($ customerId );
71
111
$ this ->resourceCompareList ->save ($ compareList );
72
112
return $ compareList ;
0 commit comments