Skip to content

Commit 0a71384

Browse files
Merge branch 'develop'
2 parents 2022a93 + 78b6157 commit 0a71384

File tree

1 file changed

+58
-2
lines changed

1 file changed

+58
-2
lines changed

Model/Component/ReviewRating.php

+58-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
use Magento\Review\Model\Rating;
77
use Magento\Review\Model\RatingFactory;
88
use Magento\Store\Model\StoreRepository;
9+
use Magento\Review\Model\Rating\Option;
10+
use Magento\Review\Model\Rating\OptionFactory;
911

1012
class ReviewRating extends YamlComponentAbstract
1113
{
14+
const MAX_NUM_RATINGS = 5;
15+
1216
protected $alias = 'review_rating';
1317

1418
protected $name = 'Review Rating';
@@ -23,14 +27,21 @@ class ReviewRating extends YamlComponentAbstract
2327
*/
2428
protected $storeRepository;
2529

30+
/**
31+
* @var OptionFactory
32+
*/
33+
protected $optionFactory;
34+
2635
public function __construct(
2736
LoggingInterface $log,
2837
ObjectManagerInterface $objectManager,
2938
RatingFactory $ratingFactory,
30-
StoreRepository $storeRepository
39+
StoreRepository $storeRepository,
40+
OptionFactory $optionFactory
3141
) {
3242
$this->ratingFactory = $ratingFactory;
3343
$this->storeRepository = $storeRepository;
44+
$this->optionFactory = $optionFactory;
3445
parent::__construct($log, $objectManager);
3546
}
3647

@@ -46,7 +57,8 @@ public function processData($data = null)
4657
$ratingModel = $this->getReviewRating($code);
4758
$ratingModel = $this->updateOrCreateRating($ratingModel, $code, $reviewRating);
4859
$ratingModel->save();
49-
$this->log->logInfo(sprintf('Updated review rating "%s"', $code));
60+
$this->setOptions($ratingModel);
61+
$this->log->logInfo(__('Updated review rating "%1"', $code));
5062
} catch (\Exception $e) {
5163
$this->log->logError(
5264
sprintf(
@@ -89,6 +101,13 @@ public function getReviewRating($reviewRatingCode)
89101
return $rating;
90102
}
91103

104+
/**
105+
* @param Rating $rating
106+
* @param $ratingCode
107+
* @param $ratingData
108+
*
109+
* @return Rating
110+
*/
92111
public function updateOrCreateRating(Rating $rating, $ratingCode, $ratingData)
93112
{
94113
$rating->setRatingCode($ratingCode);
@@ -113,6 +132,43 @@ public function updateOrCreateRating(Rating $rating, $ratingCode, $ratingData)
113132
return $rating;
114133
}
115134

135+
/**
136+
* Sets the options on the rating
137+
*
138+
* @param Rating $rating
139+
*/
140+
protected function setOptions(Rating $rating)
141+
{
142+
$ratingOptions = $rating->getOptions();
143+
if (count($ratingOptions) === self::MAX_NUM_RATINGS) {
144+
return;
145+
}
146+
$alreadyCreated = [];
147+
148+
foreach ($ratingOptions as $ratingOption) {
149+
$alreadyCreated[] = $ratingOption->getCode();
150+
}
151+
for ($count = 1; $count <= self::MAX_NUM_RATINGS; $count++) {
152+
if (in_array($count, $alreadyCreated)) {
153+
continue;
154+
}
155+
/**
156+
* @var Option $option
157+
*/
158+
$option = $this->optionFactory->create();
159+
$option->setRatingId($rating->getId());
160+
$option->setCode($count);
161+
$option->setValue($count);
162+
$option->setPosition($count);
163+
$option->save();
164+
}
165+
}
166+
167+
/**
168+
* @param $storeCodes
169+
*
170+
* @return array
171+
*/
116172
public function getStoresByCodes($storeCodes)
117173
{
118174
$storesResponse = [];

0 commit comments

Comments
 (0)