Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Synchronizer.php
Go to the documentation of this file.
1 <?php
7 
11 use Magento\Catalog\Model\ProductFrontendActionFactory;
13 use Magento\Catalog\Model\ResourceModel\ProductFrontendAction\CollectionFactory;
17 
23 {
29 
31  const ALLOW_SYNC_WITH_BACKEND_PATH = "catalog/recently_products/synchronize_with_backend";
32 
36  private $session;
37 
41  private $visitor;
42 
46  private $productFrontendActionFactory;
47 
51  private $entityManager;
52 
56  private $collectionFactory;
57 
61  private $frontendStorageConfigurationPool;
62 
71  public function __construct(
72  Session $session,
73  Visitor $visitor,
74  ProductFrontendActionFactory $productFrontendActionFactory,
75  EntityManager $entityManager,
76  CollectionFactory $collectionFactory,
77  FrontendStorageConfigurationPool $frontendStorageConfigurationPool
78  ) {
79  $this->session = $session;
80  $this->visitor = $visitor;
81  $this->productFrontendActionFactory = $productFrontendActionFactory;
82  $this->entityManager = $entityManager;
83  $this->collectionFactory = $collectionFactory;
84  $this->frontendStorageConfigurationPool = $frontendStorageConfigurationPool;
85  }
86 
95  private function getLifeTimeByNamespace($namespace)
96  {
97  $configurationObject = $this->frontendStorageConfigurationPool->get($namespace);
98  if ($configurationObject) {
99  $configuration = $configurationObject->get();
100  } else {
101  $configuration = [
103  ];
104  }
105 
106  return isset($configuration['lifetime']) ?
108  }
109 
118  private function filterNewestActions(array $productsData, $typeId)
119  {
120  $lifetime = $this->getLifeTimeByNamespace($typeId);
121  $actionsNumber = $lifetime * self::TIME_TO_DO_ONE_ACTION;
122 
123  uasort($productsData, function (array $firstProduct, array $secondProduct) {
124  return $firstProduct['added_at'] > $secondProduct['added_at'];
125  });
126 
127  return array_slice($productsData, 0, $actionsNumber, true);
128  }
129 
136  private function getProductIdsByActions(array $actions)
137  {
138  $productIds = [];
139 
140  foreach ($actions as $action) {
141  $productIds[] = $action['product_id'];
142  }
143 
144  return $productIds;
145  }
146 
156  public function syncActions(array $productsData, $typeId)
157  {
158  $productsData = $this->filterNewestActions($productsData, $typeId);
159  $customerId = $this->session->getCustomerId();
160  $visitorId = $this->visitor->getId();
161  $collection = $this->getActionsByType($typeId);
162  $collection->addFieldToFilter('product_id', $this->getProductIdsByActions($productsData));
163 
172  foreach ($collection as $item) {
173  $this->entityManager->delete($item);
174  }
175 
176  foreach ($productsData as $productId => $productData) {
178  $action = $this->productFrontendActionFactory->create([
179  'data' => [
180  'visitor_id' => $customerId ? null : $visitorId,
181  'customer_id' => $this->session->getCustomerId(),
182  'added_at' => $productData['added_at'],
183  'product_id' => $productId,
184  'type_id' => $typeId
185  ]
186  ]);
187 
188  $this->entityManager->save($action);
189  }
190  }
191 
198  public function getActionsByType($typeId)
199  {
200  $actions = $this->getAllActions();
201  $actions->addFieldToFilter('type_id', $typeId);
202 
203  return $actions;
204  }
205 
211  public function getAllActions()
212  {
214  $collection = $this->collectionFactory->create();
215  $customerId = $this->session->getCustomerId();
216  $visitorId = $this->visitor->getId();
217  $collection->addFilterByUserIdentities($customerId, $visitorId);
218 
219  return $collection;
220  }
221 }
$configuration
Definition: index.php:33
$productsData
Definition: products.php:19
__construct(Session $session, Visitor $visitor, ProductFrontendActionFactory $productFrontendActionFactory, EntityManager $entityManager, CollectionFactory $collectionFactory, FrontendStorageConfigurationPool $frontendStorageConfigurationPool)
$productData