6
6
use Magento \Review \Model \Rating ;
7
7
use Magento \Review \Model \RatingFactory ;
8
8
use Magento \Store \Model \StoreRepository ;
9
+ use Magento \Review \Model \Rating \Option ;
10
+ use Magento \Review \Model \Rating \OptionFactory ;
9
11
10
12
class ReviewRating extends YamlComponentAbstract
11
13
{
14
+ const MAX_NUM_RATINGS = 5 ;
15
+
12
16
protected $ alias = 'review_rating ' ;
13
17
14
18
protected $ name = 'Review Rating ' ;
@@ -23,14 +27,21 @@ class ReviewRating extends YamlComponentAbstract
23
27
*/
24
28
protected $ storeRepository ;
25
29
30
+ /**
31
+ * @var OptionFactory
32
+ */
33
+ protected $ optionFactory ;
34
+
26
35
public function __construct (
27
36
LoggingInterface $ log ,
28
37
ObjectManagerInterface $ objectManager ,
29
38
RatingFactory $ ratingFactory ,
30
- StoreRepository $ storeRepository
39
+ StoreRepository $ storeRepository ,
40
+ OptionFactory $ optionFactory
31
41
) {
32
42
$ this ->ratingFactory = $ ratingFactory ;
33
43
$ this ->storeRepository = $ storeRepository ;
44
+ $ this ->optionFactory = $ optionFactory ;
34
45
parent ::__construct ($ log , $ objectManager );
35
46
}
36
47
@@ -46,7 +57,8 @@ public function processData($data = null)
46
57
$ ratingModel = $ this ->getReviewRating ($ code );
47
58
$ ratingModel = $ this ->updateOrCreateRating ($ ratingModel , $ code , $ reviewRating );
48
59
$ 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 ));
50
62
} catch (\Exception $ e ) {
51
63
$ this ->log ->logError (
52
64
sprintf (
@@ -89,6 +101,13 @@ public function getReviewRating($reviewRatingCode)
89
101
return $ rating ;
90
102
}
91
103
104
+ /**
105
+ * @param Rating $rating
106
+ * @param $ratingCode
107
+ * @param $ratingData
108
+ *
109
+ * @return Rating
110
+ */
92
111
public function updateOrCreateRating (Rating $ rating , $ ratingCode , $ ratingData )
93
112
{
94
113
$ rating ->setRatingCode ($ ratingCode );
@@ -113,6 +132,43 @@ public function updateOrCreateRating(Rating $rating, $ratingCode, $ratingData)
113
132
return $ rating ;
114
133
}
115
134
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
+ */
116
172
public function getStoresByCodes ($ storeCodes )
117
173
{
118
174
$ storesResponse = [];
0 commit comments