diff --git a/Model/Component/ReviewRating.php b/Model/Component/ReviewRating.php index 1a943c9..1736461 100644 --- a/Model/Component/ReviewRating.php +++ b/Model/Component/ReviewRating.php @@ -5,10 +5,18 @@ use Magento\Framework\ObjectManagerInterface; use Magento\Review\Model\Rating; use Magento\Review\Model\RatingFactory; +use Magento\Review\Model\Rating\Entity; +use Magento\Review\Model\Rating\EntityFactory; use Magento\Store\Model\StoreRepository; use Magento\Review\Model\Rating\Option; use Magento\Review\Model\Rating\OptionFactory; +/** + * Class ReviewRating + * @package CtiDigital\Configurator\Model\Component + * + * @SuppressWarnings("CouplingBetweenObjects") + */ class ReviewRating extends YamlComponentAbstract { const MAX_NUM_RATINGS = 5; @@ -17,6 +25,8 @@ class ReviewRating extends YamlComponentAbstract protected $name = 'Review Rating'; + protected $entityId; + /** * @var RatingFactory */ @@ -32,16 +42,23 @@ class ReviewRating extends YamlComponentAbstract */ protected $optionFactory; + /** + * @var EntityFactory + */ + protected $entityFactory; + public function __construct( LoggingInterface $log, ObjectManagerInterface $objectManager, RatingFactory $ratingFactory, StoreRepository $storeRepository, - OptionFactory $optionFactory + OptionFactory $optionFactory, + EntityFactory $entityFactory ) { $this->ratingFactory = $ratingFactory; $this->storeRepository = $storeRepository; $this->optionFactory = $optionFactory; + $this->entityFactory = $entityFactory; parent::__construct($log, $objectManager); } @@ -111,7 +128,8 @@ public function getReviewRating($reviewRatingCode) public function updateOrCreateRating(Rating $rating, $ratingCode, $ratingData) { $rating->setRatingCode($ratingCode); - $rating->setEntityId(1); + $reviewEntityId = $this->getReviewEntityId(); + $rating->setEntityId($reviewEntityId); $isActive = 0; if (isset($ratingData['is_active'])) { $isActive = $ratingData['is_active']; @@ -184,4 +202,21 @@ public function getStoresByCodes($storeCodes) return $storesResponse; } + + /** + * Get the review entity ID + * + * @return int + */ + private function getReviewEntityId() + { + if ($this->entityId === null) { + /** + * @var Entity $entity + */ + $entity = $this->entityFactory->create(); + $this->entityId = $entity->getIdByCode('product'); + } + return $this->entityId; + } }