@@ -72,7 +72,7 @@ public function review($model, float $rating, ?string $reviewContent = null, boo
72
72
*
73
73
* @param \Illuminate\Database\Eloquent\Model $model
74
74
*/
75
- public function updateReview ($ model , float $ newRating , ?string $ newReview = null , bool $ isApproved = true ): Review
75
+ public function updateReview ($ model , float $ newRating , ?string $ newReview = null ): Review
76
76
{
77
77
$ data = [
78
78
'reviewer_id ' => $ this ->id ,
@@ -81,7 +81,6 @@ public function updateReview($model, float $newRating, ?string $newReview = null
81
81
'reviewable_type ' => $ model ->getMorphClass (),
82
82
'rating ' => $ newRating ,
83
83
'content ' => $ newReview ,
84
- 'approved_at ' => $ isApproved ? now () : null ,
85
84
];
86
85
87
86
return $ this ->saveReview ($ data , true );
@@ -125,24 +124,39 @@ protected function saveReview(array $data, bool $isUpdate = false): Review
125
124
->where ('reviewable_type ' , $ data ['reviewable_type ' ])
126
125
->firstOrNew ();
127
126
128
- $ oldRating = 0.0 ;
129
- if ($ isUpdate ) {
130
- $ oldRating = $ review ->rating ;
131
- }
127
+ $ oldRating = $ this ->getOldRating ($ review , $ isUpdate );
132
128
133
129
$ review ->fill ($ data );
134
130
$ review ->save ();
135
131
136
- if ($ data ['approved_at ' ]) {
137
- $ params = [
138
- 'rating ' => $ data ['rating ' ],
139
- 'oldRating ' => $ oldRating ,
140
- 'isUpdate ' => $ isUpdate ,
141
- ];
142
- $ review ->reviewable ->updateReviewSummary ($ params );
143
- }
132
+ $ this ->handleUpdateReviewSummary ($ review , $ data , $ isUpdate , $ oldRating );
144
133
145
134
return $ review ;
146
135
});
147
136
}
137
+
138
+ protected function getOldRating ($ review , $ isUpdate )
139
+ {
140
+ $ oldRating = 0.0 ;
141
+ if ($ isUpdate ) {
142
+ $ oldRating = $ review ->rating ;
143
+ }
144
+
145
+ return $ oldRating ;
146
+ }
147
+
148
+ protected function handleUpdateReviewSummary ($ review , $ data , $ isUpdate , $ oldRating )
149
+ {
150
+ $ isUnapprove = array_key_exists ('approved_at ' , $ data ) && is_null ($ data ['approved_at ' ]);
151
+ if ($ isUnapprove ) {
152
+ return ;
153
+ }
154
+
155
+ $ params = [
156
+ 'rating ' => $ data ['rating ' ],
157
+ 'oldRating ' => $ oldRating ,
158
+ 'isUpdate ' => $ isUpdate ,
159
+ ];
160
+ $ review ->reviewable ->updateReviewSummary ($ params );
161
+ }
148
162
}
0 commit comments