Blame view

behaviors/CommentBehavior.php 6.23 KB
226062a5   Yarik   CommentBehavior
1
2
  <?php
      
faff2c48   Yarik   Artbox comment cr...
3
      namespace artbox\webcomment\behaviors;
226062a5   Yarik   CommentBehavior
4
      
faff2c48   Yarik   Artbox comment cr...
5
6
      use artbox\webcomment\models\CommentModel;
      use artbox\webcomment\models\interfaces\RatingCacheInterface;
226062a5   Yarik   CommentBehavior
7
8
      use yii\base\Behavior;
      use yii\base\InvalidConfigException;
faff2c48   Yarik   Artbox comment cr...
9
      use yii\base\InvalidParamException;
226062a5   Yarik   CommentBehavior
10
11
12
13
14
15
16
17
      use yii\db\ActiveQuery;
      use yii\db\ActiveRecord;
      
      /**
       * Class CommentBehavior
       *
       * @property ActiveQuery    $averageRating
       * @property CommentModel[] $comments
faff2c48   Yarik   Artbox comment cr...
18
       * @package artbox\webcomment\behaviors
226062a5   Yarik   CommentBehavior
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
       */
      class CommentBehavior extends Behavior
      {
          
          /**
           * Fully-classified name of class to hold rating cache
           *
           * @var string
           */
          private $cacheModelName = '';
          
          /**
           * Class to hold rating cache
           *
           * @var RatingCacheInterface
           */
          private $cacheModel = null;
          
          /**
           * Whether to cache rating or not
           *
           * @var bool
           */
          private $cacheRating = false;
920dd24d   Yarik   Comments
43
      
226062a5   Yarik   CommentBehavior
44
45
46
47
48
49
          /**
           * @inheritdoc
           */
          public function attach($owner)
          {
              if ($this->cacheRating) {
faff2c48   Yarik   Artbox comment cr...
50
                  if (empty($this->cacheModelName) || !is_string($this->cacheModelName)) {
226062a5   Yarik   CommentBehavior
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
                      throw new InvalidConfigException(
                          'To use rating cache you must set $cacheModelName where to store it'
                      );
                  }
                  $this->setCacheModel(
                      \Yii::createObject(
                          [
                              'class' => $this->cacheModelName,
                          ]
                      )
                  );
              }
              parent::attach($owner);
          }
          
          /**
           * Get fully-classified name of class to hold rating cache
           *
           * @return string
           */
faff2c48   Yarik   Artbox comment cr...
71
          public function getCacheModelName(): string
226062a5   Yarik   CommentBehavior
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
          {
              return $this->cacheModelName;
          }
          
          /**
           * Set fully-classified name of class to hold rating cache
           *
           * @param string $value
           */
          public function setCacheModelName(string $value)
          {
              $this->cacheModelName = $value;
          }
          
          /**
           * Get model to hold rating cache
226062a5   Yarik   CommentBehavior
88
           */
faff2c48   Yarik   Artbox comment cr...
89
          public function getCacheModel(): RatingCacheInterface
226062a5   Yarik   CommentBehavior
90
91
92
93
94
95
96
          {
              return $this->cacheModel;
          }
          
          /**
           * Set model to hold cache
           *
faff2c48   Yarik   Artbox comment cr...
97
           * @param \artbox\webcomment\models\interfaces\RatingCacheInterface|null $value
226062a5   Yarik   CommentBehavior
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
           */
          private function setCacheModel(RatingCacheInterface $value = null)
          {
              $this->cacheModel = $value;
          }
          
          /**
           * Whether cache rating or not
           *
           * @return bool
           */
          public function getCacheRating(): bool
          {
              return $this->cacheRating;
          }
          
          /**
           * Set whether to cache rating or not
           *
           * @param bool $value
           */
          public function setCacheRating(bool $value)
          {
              $this->cacheRating = $value;
          }
          
          /**
           * Get query to get all comments for current $owner model
           *
           * @return \yii\db\ActiveQuery
           */
          public function getComments(): ActiveQuery
          {
              /**
               * @var ActiveRecord $owner
               */
              $owner = $this->owner;
faff2c48   Yarik   Artbox comment cr...
135
136
137
138
139
140
141
              $pkKeys = $owner->primaryKey();
              if (!empty($pkKeys)) {
                  $pkKey = $pkKeys[ 0 ];
              } else {
                  throw new InvalidParamException('Entity must have primary key.');
              }
              $pk = $owner->getAttribute($pkKey);
226062a5   Yarik   CommentBehavior
142
143
144
              $query = $owner->hasMany(CommentModel::className(), [ 'entity_id' => $pk ])
                             ->where(
                                 [
faff2c48   Yarik   Artbox comment cr...
145
146
147
                                     'artbox_comment.entity'    => $owner::className(),
                                     'artbox_comment.status'    => CommentModel::STATUS_ACTIVE,
                                     'artbox_comment.parent_id' => null,
226062a5   Yarik   CommentBehavior
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
                                 ]
                             );
              return $query;
          }
          
          /**
           * Get query to get average rating for current $owner model if it cached
           *
           * @return \yii\db\ActiveQuery
           */
          public function getAverageRating(): ActiveQuery
          {
              /**
               * @var ActiveRecord $owner
               */
              $owner = $this->owner;
              $pk = $owner->primaryKey()[ 0 ];
              $query = $owner->hasOne($this->cacheModelName, [ $this->cacheModel->getLinkAttribute() => $pk ]);
              return $query;
          }
          
          /**
           * Recalculate cache for current $owner model
           *
           * @return bool
           */
          public function recalculateRating(): bool
          {
              /**
               * @var RatingCacheInterface $averageRating
               */
              $average = $this->getComments()
                              ->joinWith('rating')
                              ->select([ 'average' => 'avg(artbox_comment_rating.value)::float' ])
                              ->scalar();
              if (!$average) {
                  $average = 0;
              }
              $averageRating = $this->getAverageRating()
                                    ->one();
faff2c48   Yarik   Artbox comment cr...
188
              if (!empty($averageRating)) {
226062a5   Yarik   CommentBehavior
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
                  $averageRating->setValue($average);
              } else {
                  /**
                   * @var ActiveRecord         $owner
                   * @var RatingCacheInterface $averageRating
                   */
                  $owner = $this->owner;
                  $pk = $owner->primaryKey()[ 0 ];
                  $averageRating = \Yii::createObject(
                      [
                          'class'                               => $this->cacheModelName,
                          $this->cacheModel->getLinkAttribute() => $this->owner->$pk,
                      ]
                  );
                  $averageRating->setValue($average);
              }
              if ($averageRating->save()) {
                  return true;
              } else {
                  return false;
              }
          }
      }