@@ -322,8 +322,6 @@ public function getTotalReviews($entityPkValue, $approvedOnly = false, $storeId
322
322
*/
323
323
public function aggregate ($ object )
324
324
{
325
- $ readAdapter = $ this ->_getReadAdapter ();
326
- $ writeAdapter = $ this ->_getWriteAdapter ();
327
325
if (!$ object ->getEntityPkValue () && $ object ->getId ()) {
328
326
$ object ->load ($ object ->getReviewId ());
329
327
}
@@ -332,48 +330,72 @@ public function aggregate($object)
332
330
$ ratingSummaries = $ ratingModel ->getEntitySummary ($ object ->getEntityPkValue (), false );
333
331
334
332
foreach ($ ratingSummaries as $ ratingSummaryObject ) {
335
- if ($ ratingSummaryObject ->getCount ()) {
336
- $ ratingSummary = round ($ ratingSummaryObject ->getSum () / $ ratingSummaryObject ->getCount ());
337
- } else {
338
- $ ratingSummary = $ ratingSummaryObject ->getSum ();
339
- }
333
+ $ this ->aggregateReviewSummary ($ object , $ ratingSummaryObject );
334
+ }
335
+ }
340
336
341
- $ reviewsCount = $ this ->getTotalReviews (
342
- $ object ->getEntityPkValue (),
343
- true ,
344
- $ ratingSummaryObject ->getStoreId ()
345
- );
346
- $ select = $ readAdapter ->select ()->from ($ this ->_aggregateTable )
347
- ->where ('entity_pk_value = :pk_value ' )
348
- ->where ('entity_type = :entity_type ' )
349
- ->where ('store_id = :store_id ' );
350
- $ bind = [
351
- ':pk_value ' => $ object ->getEntityPkValue (),
352
- ':entity_type ' => $ object ->getEntityId (),
353
- ':store_id ' => $ ratingSummaryObject ->getStoreId (),
354
- ];
355
- $ oldData = $ readAdapter ->fetchRow ($ select , $ bind );
356
-
357
- $ data = new \Magento \Framework \Object ();
358
-
359
- $ data ->setReviewsCount ($ reviewsCount )
360
- ->setEntityPkValue ($ object ->getEntityPkValue ())
361
- ->setEntityType ($ object ->getEntityId ())
362
- ->setRatingSummary ($ ratingSummary > 0 ? $ ratingSummary : 0 )
363
- ->setStoreId ($ ratingSummaryObject ->getStoreId ());
364
-
365
- $ writeAdapter ->beginTransaction ();
366
- try {
367
- if ($ oldData ['primary_id ' ] > 0 ) {
368
- $ condition = ["{$ this ->_aggregateTable }.primary_id = ? " => $ oldData ['primary_id ' ]];
369
- $ writeAdapter ->update ($ this ->_aggregateTable , $ data ->getData (), $ condition );
370
- } else {
371
- $ writeAdapter ->insert ($ this ->_aggregateTable , $ data ->getData ());
372
- }
373
- $ writeAdapter ->commit ();
374
- } catch (\Exception $ e ) {
375
- $ writeAdapter ->rollBack ();
337
+ /**
338
+ * Aggregate review summary
339
+ *
340
+ * @param \Magento\Framework\Model\AbstractModel $object
341
+ * @param $ratingSummaryObject
342
+ */
343
+ protected function aggregateReviewSummary ($ object , $ ratingSummaryObject )
344
+ {
345
+ $ readAdapter = $ this ->_getReadAdapter ();
346
+
347
+ if ($ ratingSummaryObject ->getCount ()) {
348
+ $ ratingSummary = round ($ ratingSummaryObject ->getSum () / $ ratingSummaryObject ->getCount ());
349
+ } else {
350
+ $ ratingSummary = $ ratingSummaryObject ->getSum ();
351
+ }
352
+
353
+ $ reviewsCount = $ this ->getTotalReviews (
354
+ $ object ->getEntityPkValue (),
355
+ true ,
356
+ $ ratingSummaryObject ->getStoreId ()
357
+ );
358
+ $ select = $ readAdapter ->select ()->from ($ this ->_aggregateTable )
359
+ ->where ('entity_pk_value = :pk_value ' )
360
+ ->where ('entity_type = :entity_type ' )
361
+ ->where ('store_id = :store_id ' );
362
+ $ bind = [
363
+ ':pk_value ' => $ object ->getEntityPkValue (),
364
+ ':entity_type ' => $ object ->getEntityId (),
365
+ ':store_id ' => $ ratingSummaryObject ->getStoreId (),
366
+ ];
367
+ $ oldData = $ readAdapter ->fetchRow ($ select , $ bind );
368
+ $ data = new \Magento \Framework \Object ();
369
+
370
+ $ data ->setReviewsCount ($ reviewsCount )
371
+ ->setEntityPkValue ($ object ->getEntityPkValue ())
372
+ ->setEntityType ($ object ->getEntityId ())
373
+ ->setRatingSummary ($ ratingSummary > 0 ? $ ratingSummary : 0 )
374
+ ->setStoreId ($ ratingSummaryObject ->getStoreId ());
375
+
376
+ $ this ->writeReviewSummary ($ oldData , $ data );
377
+ }
378
+
379
+ /**
380
+ * Write rating summary
381
+ *
382
+ * @param array|bool $oldData
383
+ * @param \Magento\Framework\Object $data
384
+ */
385
+ protected function writeReviewSummary ($ oldData , \Magento \Framework \Object $ data )
386
+ {
387
+ $ writeAdapter = $ this ->_getWriteAdapter ();
388
+ $ writeAdapter ->beginTransaction ();
389
+ try {
390
+ if (isset ($ oldData ['primary_id ' ]) && $ oldData ['primary_id ' ] > 0 ) {
391
+ $ condition = ["{$ this ->_aggregateTable }.primary_id = ? " => $ oldData ['primary_id ' ]];
392
+ $ writeAdapter ->update ($ this ->_aggregateTable , $ data ->getData (), $ condition );
393
+ } else {
394
+ $ writeAdapter ->insert ($ this ->_aggregateTable , $ data ->getData ());
376
395
}
396
+ $ writeAdapter ->commit ();
397
+ } catch (\Exception $ e ) {
398
+ $ writeAdapter ->rollBack ();
377
399
}
378
400
}
379
401
0 commit comments