diff --git a/src/Skobkin/Bundle/PointToolsBundle/Service/SubscriptionsManager.php b/src/Skobkin/Bundle/PointToolsBundle/Service/SubscriptionsManager.php index f17e5cb..09579b2 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Service/SubscriptionsManager.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Service/SubscriptionsManager.php @@ -39,6 +39,14 @@ class SubscriptionsManager $oldSubscribersList[] = $subscription->getSubscriber(); } + $isFirstTime = false; + + // Preventing to add garbage subscriptions for first processing + // @todo improve algorithm + if ((count($oldSubscribersList) === 0) && (count($newSubscribersList) > 1)) { + $isFirstTime = true; + } + unset($tmpOldSubscribers); $subscribedList = $this->getUsersListsDiff($newSubscribersList, $oldSubscribersList); @@ -54,17 +62,20 @@ class SubscriptionsManager $user->addSubscriber($subscription); - $logEvent = new SubscriptionEvent(); - $logEvent - ->setSubscriber($subscribedUser) - ->setAuthor($user) - ->setAction(SubscriptionEvent::ACTION_SUBSCRIBE) - ; + // If it's not first processing + if (!$isFirstTime) { + $logEvent = new SubscriptionEvent(); + $logEvent + ->setSubscriber($subscribedUser) + ->setAuthor($user) + ->setAction(SubscriptionEvent::ACTION_SUBSCRIBE); - $user->addNewSubscriberEvent($logEvent); + $user->addNewSubscriberEvent($logEvent); + + $this->em->persist($logEvent); + } $this->em->persist($subscription); - $this->em->persist($logEvent); } unset($subscribedList);