$cachedReactions[$reactionType->reactionTypeID] = 1;
}
+ $cachedReactions = self::cleanUpCachedReactions($cachedReactions);
+
// build update date
$updateData = [
'likes' => $cumulativeLikes,
}
}
+ $cachedReactions = self::cleanUpCachedReactions($cachedReactions);
+
// build update date
$updateData = [
'likes' => $cumulativeLikes,
return $firstReactionType ? $firstReactionType->reactionTypeID : null;
}
+ /**
+ * Removes deleted reactions from the reaction counter for the like object table.
+ *
+ * @param array $cachedReactions
+ * @return array
+ */
+ private function cleanUpCachedReactions(array $cachedReactions) {
+ foreach ($cachedReactions as $reactionTypeID => $count) {
+ if (self::getReactionTypeByID($reactionTypeID) === null) {
+ unset($cachedReactions[$reactionTypeID]);
+ }
+ }
+
+ return $cachedReactions;
+ }
+
/**
* @param string|null $cachedReactions
* @return array|null
if ($cachedReactions) {
$cachedReactions = @unserialize($cachedReactions);
- if (is_array($cachedReactions) && !empty($cachedReactions)) {
- $allReactions = array_sum($cachedReactions);
-
- arsort($cachedReactions, SORT_NUMERIC);
+ if (is_array($cachedReactions)) {
+ $cachedReactions = self::cleanUpCachedReactions($cachedReactions);
- $count = current($cachedReactions);
- return [
- 'count' => $count,
- 'other' => $allReactions - $count,
- 'reaction' => ReactionTypeCache::getInstance()->getReactionTypeByID(key($cachedReactions)),
- ];
+ if (!empty($cachedReactions)) {
+ $allReactions = array_sum($cachedReactions);
+
+ arsort($cachedReactions, SORT_NUMERIC);
+
+ $count = current($cachedReactions);
+ return [
+ 'count' => $count,
+ 'other' => $allReactions - $count,
+ 'reaction' => ReactionTypeCache::getInstance()->getReactionTypeByID(key($cachedReactions)),
+ ];
+ }
}
}