Blame view

behaviors/CommentBehavior.php 6.1 KB
226062a5   Yarik   CommentBehavior
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  <?php
      
      namespace artweb\artbox\comment\behaviors;
      
      use artweb\artbox\comment\models\CommentModel;
      use artweb\artbox\comment\models\interfaces\RatingCacheInterface;
      use yii\base\Behavior;
      use yii\base\InvalidConfigException;
      use yii\db\ActiveQuery;
      use yii\db\ActiveRecord;
      
      /**
       * Class CommentBehavior
       *
       * @property ActiveQuery    $averageRating
       * @property CommentModel[] $comments
       * @package artweb\artbox\comment\behaviors
       */
      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
42
      
226062a5   Yarik   CommentBehavior
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
135
136
137
138
139
140
141
142
143
144
145
146
147
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
          /**
           * @inheritdoc
           */
          public function attach($owner)
          {
              if ($this->cacheRating) {
                  if (empty( $this->cacheModelName ) || !is_string($this->cacheModelName)) {
                      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
           */
          public function getCacheModelName():string
          {
              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
           *
           * @return \artweb\artbox\comment\models\interfaces\RatingCacheInterface
           */
          public function getCacheModel():RatingCacheInterface
          {
              return $this->cacheModel;
          }
          
          /**
           * Set model to hold cache
           *
           * @param \artweb\artbox\comment\models\interfaces\RatingCacheInterface|null $value
           */
          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;
              $pk = $owner->primaryKey()[ 0 ];
              $query = $owner->hasMany(CommentModel::className(), [ 'entity_id' => $pk ])
                             ->where(
                                 [
                                     'artbox_comment.entity'             => $owner::className(),
                                     'artbox_comment.status'             => CommentModel::STATUS_ACTIVE,
                                     'artbox_comment.artbox_comment_pid' => null,
                                 ]
                             );
              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();
              if (!empty( $averageRating )) {
                  $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;
              }
          }
      }