Commit b409d90948fa1cdc887b37fed2da4cd2b4e6aaf0

Authored by Yarik
1 parent 30415cab

Rating caching

Module.php
... ... @@ -34,7 +34,7 @@
34 34 * @var string comment model class
35 35 */
36 36 public $commentModelClass = null;
37   -
  37 +
38 38 public $ratingModelClass = null;
39 39  
40 40 /**
... ... @@ -63,7 +63,7 @@
63 63 * @var bool
64 64 */
65 65 public $enableRating = true;
66   -
  66 +
67 67 /**
68 68 * Whether to enable comment premoderate or not.
69 69 *
... ... @@ -72,6 +72,11 @@
72 72 public $enablePremoderate = true;
73 73  
74 74 /**
  75 + * @var string[] $cacheModels
  76 + */
  77 + public $cacheModels = [];
  78 +
  79 + /**
75 80 * Initializes the module.
76 81 * This method is called after the module is created and initialized with property values
77 82 * given in configuration. The default implementation will initialize
... ... @@ -99,7 +104,7 @@
99 104 }
100 105 parent::init();
101 106 }
102   -
  107 +
103 108 /**
104 109 * Prompt to register current module
105 110 *
... ... @@ -110,4 +115,33 @@
110 115 throw new InvalidConfigException(\Yii::t('artbox-comment', 'Register artbox-comment module'));
111 116 }
112 117  
  118 + /**
  119 + * Rating model factory
  120 + *
  121 + * @param $config
  122 + *
  123 + * @return \yii\db\ActiveRecord
  124 + */
  125 + public function createRating($config)
  126 + {
  127 + /**
  128 + * @var \yii\db\ActiveRecord $model
  129 + */
  130 + $model = \Yii::createObject(
  131 + array_merge(
  132 + [
  133 + 'class' => $this->ratingModelClass,
  134 + ],
  135 + $config
  136 + )
  137 + );
  138 + $behaviors = [];
  139 + foreach ($this->cacheModels as $cacheModel) {
  140 + $behaviors[] = \Yii::createObject($cacheModel);
  141 + }
  142 + if (!empty($behaviors)) {
  143 + $model->attachBehaviors($behaviors);
  144 + }
  145 + return $model;
  146 + }
113 147 }
... ...
controllers/DefaultController.php
... ... @@ -3,7 +3,6 @@
3 3 namespace artbox\webcomment\controllers;
4 4  
5 5 use artbox\webcomment\models\CommentModel;
6   - use artbox\webcomment\models\RatingModel;
7 6 use artbox\webcomment\Module;
8 7 use yii\filters\AccessControl;
9 8 use yii\filters\VerbFilter;
... ... @@ -45,7 +44,7 @@
45 44 ],
46 45 ];
47 46 }
48   -
  47 +
49 48 /**
50 49 * Create comment.
51 50 *
... ... @@ -80,11 +79,7 @@
80 79 $model->setAttributes($entity_data);
81 80 if ($model->save()) {
82 81 if (empty($model->parent_id) && $module->enableRating) {
83   - $ratingModelClass = $module->ratingModelClass;
84   - /**
85   - * @var RatingModel $rating
86   - */
87   - $rating = new $ratingModelClass(
  82 + $rating = $module->createRating(
88 83 [
89 84 'model' => $model::className(),
90 85 'model_id' => $model->primaryKey,
... ...
models/CommentModel.php
... ... @@ -33,6 +33,7 @@
33 33 * @property string $link
34 34 * @property \artbox\webcomment\models\CommentModel[] $children
35 35 * @property \artbox\order\models\Customer $customer
  36 + * @property \artbox\webcomment\models\RatingModel $rating
36 37 */
37 38 class CommentModel extends ActiveRecord implements CommentInterface
38 39 {
... ... @@ -49,7 +50,7 @@
49 50 public $encryptedEntity;
50 51  
51 52 public $entityId;
52   -
  53 +
53 54 /**
54 55 * @inheritdoc
55 56 */
... ... @@ -85,7 +86,7 @@
85 86 ];
86 87 return $scenarios;
87 88 }
88   -
  89 +
89 90 /**
90 91 * @inheritdoc
91 92 */
... ... @@ -93,7 +94,7 @@
93 94 {
94 95 return '{{%artbox_comment}}';
95 96 }
96   -
  97 +
97 98 /**
98 99 * @inheritdoc
99 100 */
... ... @@ -139,7 +140,7 @@
139 140 ],
140 141 ];
141 142 }
142   -
  143 +
143 144 /**
144 145 * @inheritdoc
145 146 */
... ... @@ -188,7 +189,7 @@
188 189 ],
189 190 ];
190 191 }
191   -
  192 +
192 193 /**
193 194 * @inheritdoc
194 195 */
... ... @@ -212,7 +213,7 @@
212 213 'entity_id' => \Yii::t('artbox-comment', 'Entity ID'),
213 214 ];
214 215 }
215   -
  216 +
216 217 /**
217 218 * Set Entity of Comment model
218 219 *
... ... @@ -222,7 +223,7 @@
222 223 {
223 224 $this->entity = $entity;
224 225 }
225   -
  226 +
226 227 /**
227 228 * Get Entity of Comment model
228 229 *
... ... @@ -232,7 +233,7 @@
232 233 {
233 234 return $this->entity;
234 235 }
235   -
  236 +
236 237 /**
237 238 * Get ActiveDataProvider of comments for particular Entity according to its EntityId
238 239 *
... ... @@ -281,7 +282,7 @@
281 282 ]
282 283 );
283 284 }
284   -
  285 +
285 286 /**
286 287 * Delete comment
287 288 *
... ... @@ -296,7 +297,7 @@
296 297 }
297 298 return false;
298 299 }
299   -
  300 +
300 301 /**
301 302 * Set EntityId of Comment model
302 303 *
... ... @@ -306,7 +307,7 @@
306 307 {
307 308 $this->entityId = $entityId;
308 309 }
309   -
  310 +
310 311 /**
311 312 * Get EntityId of Comment model
312 313 *
... ... @@ -316,7 +317,7 @@
316 317 {
317 318 return $this->entityId;
318 319 }
319   -
  320 +
320 321 /**
321 322 * Get children relation for current comment
322 323 *
... ... @@ -328,7 +329,7 @@
328 329 ->andFilterWhere([ 'status' => self::STATUS_ACTIVE ])
329 330 ->inverseOf('parent');
330 331 }
331   -
  332 +
332 333 /**
333 334 * Get parent relation for current comment
334 335 *
... ... @@ -339,7 +340,7 @@
339 340 return $this->hasOne(self::className(), [ 'id' => 'parent_id' ])
340 341 ->inverseOf('children');
341 342 }
342   -
  343 +
343 344 /**
344 345 * Get customer relation for current comment
345 346 *
... ... @@ -349,7 +350,7 @@
349 350 {
350 351 return $this->hasOne(self::getCustomerClass(), [ 'id' => 'customer_id' ]);
351 352 }
352   -
  353 +
353 354 /**
354 355 * Get rating relation for current model
355 356 *
... ... @@ -366,7 +367,7 @@
366 367 ]
367 368 );
368 369 }
369   -
  370 +
370 371 /**
371 372 * Get entity model for current comment or false if not ActiveRecord
372 373 *
... ... @@ -387,7 +388,7 @@
387 388 return false;
388 389 }
389 390 }
390   -
  391 +
391 392 /**
392 393 * Get Customer model name
393 394 *
... ...