Optimize code to update user stats
authorJoshua Rüsweg <josh@bastelstu.be>
Sun, 27 May 2018 20:20:45 +0000 (22:20 +0200)
committerJoshua Rüsweg <josh@bastelstu.be>
Sun, 27 May 2018 20:20:45 +0000 (22:20 +0200)
See #2508

wcfsetup/install/files/lib/system/reaction/ReactionHandler.class.php

index ae07e29f05300685b6542639669e57088e1e9851..85b18e005bdf2020af321627eb097af008b76ecd 100644 (file)
@@ -413,50 +413,27 @@ class ReactionHandler extends SingletonFactory {
         */
        private function updateUsersLikeCounter(ILikeObject $likeable, LikeObject $likeObject, Like $like, ReactionType $reactionType = null) {
                if ($likeable->getUserID()) {
-                       if ($reactionType === null) {
-                               if ($like->likeID) {
-                                       if ($like->getReactionType()->isPositive()) {
-                                               $userEditor = new UserEditor(UserRuntimeCache::getInstance()->getObject($likeable->getUserID()));
-                                               $userEditor->updateCounters([
-                                                       'likesReceived' => -1, 
-                                                       'positiveReactionsReceived' => -1
-                                               ]);
-                                       }
-                                       else if ($like->getReactionType()->isNegative()) {
-                                               $userEditor = new UserEditor(UserRuntimeCache::getInstance()->getObject($likeable->getUserID()));
-                                               $userEditor->updateCounters([
-                                                       'negativeReactionsReceived' => -1
-                                               ]);
-                                       }
-                                       else if ($like->getReactionType()->isNeutral()) {
-                                               $userEditor = new UserEditor(UserRuntimeCache::getInstance()->getObject($likeable->getUserID()));
-                                               $userEditor->updateCounters([
-                                                       'neutralReactionsReceived' => -1
-                                               ]);
-                                       }
+                       $counters = [
+                               'likesReceived' => 0,
+                               'positiveReactionsReceived' => 0,
+                               'negativeReactionsReceived' => 0,
+                               'neutralReactionsReceived' => 0
+                       ];
+                       
+                       if ($like->likeID) {
+                               if ($like->getReactionType()->isPositive()) {
+                                       $counters['likesReceived']--;
+                                       $counters['positiveReactionsReceived']--;
                                }
-                       } 
-                       else {
-                               $counters = [
-                                       'likesReceived' => 0,
-                                       'positiveReactionsReceived' => 0,
-                                       'negativeReactionsReceived' => 0,
-                                       'neutralReactionsReceived' => 0
-                               ];
-                               
-                               if ($like->likeID) {
-                                       if ($like->getReactionType()->isPositive()) {
-                                               $counters['likesReceived']--;
-                                               $counters['positiveReactionsReceived']--;
-                                       }
-                                       else if ($like->getReactionType()->isNegative()) {
-                                               $counters['negativeReactionsReceived']--;
-                                       }
-                                       else if ($like->getReactionType()->isNeutral()) {
-                                               $counters['neutralReactionsReceived']--;
-                                       }
+                               else if ($like->getReactionType()->isNegative()) {
+                                       $counters['negativeReactionsReceived']--;
                                }
-                               
+                               else if ($like->getReactionType()->isNeutral()) {
+                                       $counters['neutralReactionsReceived']--;
+                               }
+                       }
+                       
+                       if ($reactionType !== null) {
                                if ($reactionType->isPositive()) {
                                        $counters['likesReceived']++;
                                        $counters['positiveReactionsReceived']++;
@@ -467,10 +444,10 @@ class ReactionHandler extends SingletonFactory {
                                else if ($reactionType->isNeutral()) {
                                        $counters['neutralReactionsReceived']++;
                                }
-                               
-                               $userEditor = new UserEditor(UserRuntimeCache::getInstance()->getObject($likeable->getUserID()));
-                               $userEditor->updateCounters($counters);
                        }
+                       
+                       $userEditor = new UserEditor(UserRuntimeCache::getInstance()->getObject($likeable->getUserID()));
+                       $userEditor->updateCounters($counters);
                }
        }