Review.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace Dotdigitalgroup\Email\Model\Sync;
  3. /**
  4. * Sync Reviews.
  5. */
  6. class Review
  7. {
  8. /**
  9. * @var mixed
  10. */
  11. private $start;
  12. /**
  13. * @var array
  14. */
  15. private $reviews;
  16. /**
  17. * @var int
  18. */
  19. private $countReviews;
  20. /**
  21. * @var \Dotdigitalgroup\Email\Helper\Data
  22. */
  23. private $helper;
  24. /**
  25. * @var array
  26. */
  27. private $reviewIds;
  28. /**
  29. * @var \Dotdigitalgroup\Email\Model\ImporterFactory
  30. */
  31. private $importerFactory;
  32. /**
  33. * @var \Dotdigitalgroup\Email\Model\Customer\ReviewFactory
  34. */
  35. private $connectorReviewFactory;
  36. /**
  37. * @var \Dotdigitalgroup\Email\Model\Customer\Review\RatingFactory
  38. */
  39. private $ratingFactory;
  40. /**
  41. * @var \Dotdigitalgroup\Email\Model\ResourceModel\Review\CollectionFactory
  42. */
  43. private $reviewCollection;
  44. /**
  45. * @var \Magento\Framework\Stdlib\DateTime\DateTime
  46. */
  47. private $coreDate;
  48. /**
  49. * @var \Dotdigitalgroup\Email\Model\ResourceModel\ReviewFactory
  50. */
  51. private $reviewResourceFactory;
  52. /**
  53. * Review constructor.
  54. *
  55. * @param \Dotdigitalgroup\Email\Model\ResourceModel\Review\CollectionFactory $reviewCollection
  56. * @param \Magento\Framework\Stdlib\DateTime\DateTime $coreDate
  57. * @param \Dotdigitalgroup\Email\Model\Customer\Review\RatingFactory $ratingFactory
  58. * @param \Dotdigitalgroup\Email\Model\Customer\ReviewFactory $connectorFactory
  59. * @param \Dotdigitalgroup\Email\Model\ImporterFactory $importerFactory
  60. * @param \Dotdigitalgroup\Email\Helper\Data $data
  61. * @param \Dotdigitalgroup\Email\Model\ResourceModel\ReviewFactory $reviewResourceFactory
  62. */
  63. public function __construct(
  64. \Dotdigitalgroup\Email\Model\ResourceModel\Review\CollectionFactory $reviewCollection,
  65. \Magento\Framework\Stdlib\DateTime\DateTime $coreDate,
  66. \Dotdigitalgroup\Email\Model\Customer\Review\RatingFactory $ratingFactory,
  67. \Dotdigitalgroup\Email\Model\Customer\ReviewFactory $connectorFactory,
  68. \Dotdigitalgroup\Email\Model\ImporterFactory $importerFactory,
  69. \Dotdigitalgroup\Email\Helper\Data $data,
  70. \Dotdigitalgroup\Email\Model\ResourceModel\ReviewFactory $reviewResourceFactory
  71. ) {
  72. $this->coreDate = $coreDate;
  73. $this->reviewCollection = $reviewCollection;
  74. $this->ratingFactory = $ratingFactory;
  75. $this->connectorReviewFactory = $connectorFactory;
  76. $this->importerFactory = $importerFactory;
  77. $this->helper = $data;
  78. $this->reviewResourceFactory = $reviewResourceFactory;
  79. }
  80. /**
  81. * Sync reviews.
  82. *
  83. * @return array
  84. */
  85. public function sync()
  86. {
  87. $response = ['success' => true, 'message' => 'Done.'];
  88. $this->countReviews = 0;
  89. $this->reviews = [];
  90. $this->start = microtime(true);
  91. $websites = $this->helper->getwebsites(true);
  92. foreach ($websites as $website) {
  93. $apiEnabled = $this->helper->isEnabled($website);
  94. $reviewEnabled = $this->helper->getWebsiteConfig(
  95. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_SYNC_REVIEW_ENABLED,
  96. $website
  97. );
  98. $storeIds = $website->getStoreIds();
  99. if ($apiEnabled && $reviewEnabled && !empty($storeIds)) {
  100. $this->_exportReviewsForWebsite($website);
  101. }
  102. if (isset($this->reviews[$website->getId()])) {
  103. $reviews = $this->reviews[$website->getId()];
  104. //send reviews as transactional data
  105. //register in queue with importer
  106. $this->importerFactory->create()
  107. ->registerQueue(
  108. \Dotdigitalgroup\Email\Model\Importer::IMPORT_TYPE_REVIEWS,
  109. $reviews,
  110. \Dotdigitalgroup\Email\Model\Importer::MODE_BULK,
  111. $website->getId()
  112. );
  113. //if no error then set imported
  114. $this->_setImported($this->reviewIds);
  115. $this->countReviews += count($reviews);
  116. }
  117. }
  118. if ($this->countReviews) {
  119. $message = '----------- Review sync ----------- : ' .
  120. gmdate('H:i:s', microtime(true) - $this->start) .
  121. ', synced = ' . $this->countReviews;
  122. $this->helper->log($message);
  123. $response['message'] = $message;
  124. }
  125. return $response;
  126. }
  127. /**
  128. * Export reviews for website.
  129. *
  130. * @param \Magento\Store\Model\Website $website
  131. *
  132. * @return null
  133. */
  134. public function _exportReviewsForWebsite(\Magento\Store\Model\Website $website)
  135. {
  136. $limit = $this->helper->getWebsiteConfig(
  137. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_TRANSACTIONAL_DATA_SYNC_LIMIT,
  138. $website
  139. );
  140. $emailReviews = $this->_getReviewsToExport($website, $limit);
  141. $this->reviewIds = [];
  142. if ($emailReviews->getSize()) {
  143. $ids = $emailReviews->getColumnValues('review_id');
  144. $reviewResourceFactory = $this->reviewResourceFactory->create();
  145. $reviews = $reviewResourceFactory->getMageReviewsByIds($ids);
  146. foreach ($reviews as $mageReview) {
  147. try {
  148. $product = $reviewResourceFactory
  149. ->getProductByIdAndStore($mageReview->getEntityPkValue(), $mageReview->getStoreId());
  150. $connectorReview = $this->connectorReviewFactory->create()
  151. ->setReviewData($mageReview)
  152. ->setProduct($product);
  153. $votesCollection = $reviewResourceFactory
  154. ->getVoteCollectionByReview($mageReview->getReviewId());
  155. foreach ($votesCollection as $ratingItem) {
  156. $rating = $this->ratingFactory->create()
  157. ->setRating($ratingItem);
  158. $connectorReview->createRating(
  159. $ratingItem->getRatingCode(),
  160. $rating
  161. );
  162. }
  163. $this->reviews[$website->getId()][] = $connectorReview->expose();
  164. $this->reviewIds[] = $mageReview->getReviewId();
  165. } catch (\Exception $e) {
  166. $this->helper->debug((string)$e, []);
  167. }
  168. }
  169. }
  170. }
  171. /**
  172. * Get reviews for export.
  173. *
  174. * @param \Magento\Store\Model\Website $website
  175. * @param int $limit
  176. *
  177. * @return \Dotdigitalgroup\Email\Model\ResourceModel\Review\Collection
  178. */
  179. public function _getReviewsToExport(\Magento\Store\Model\Website $website, $limit = 100)
  180. {
  181. return $this->reviewCollection->create()
  182. ->getReviewsToExportByWebsite($website, $limit);
  183. }
  184. /**
  185. * Set imported in bulk query.
  186. *
  187. * @param array $ids
  188. *
  189. * @return null
  190. */
  191. public function _setImported($ids)
  192. {
  193. $nowDate = $this->coreDate->gmtDate();
  194. $this->reviewResourceFactory->create()
  195. ->setImported($ids, $nowDate);
  196. }
  197. }