From b7e4374daceb50d6d9ae7099460eaf934797e836 Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Tue, 2 Jun 2015 05:27:19 +0300 Subject: [PATCH] Fix #13. --- .../Service/SubscriptionsManager.php | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) 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);