diff --git a/src/Skobkin/Bundle/PointToolsBundle/Resources/config/services.yml b/src/Skobkin/Bundle/PointToolsBundle/Resources/config/services.yml index df13f11..9b3787d 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Resources/config/services.yml +++ b/src/Skobkin/Bundle/PointToolsBundle/Resources/config/services.yml @@ -37,7 +37,10 @@ services: # Point subscription manager app.point.subscriptions_manager: class: Skobkin\Bundle\PointToolsBundle\Service\SubscriptionsManager - arguments: [ '@doctrine.orm.entity_manager', '@event_dispatcher' ] + arguments: + - '@doctrine.orm.entity_manager' + - '@event_dispatcher' + - '@logger' # Factories diff --git a/src/Skobkin/Bundle/PointToolsBundle/Service/SubscriptionsManager.php b/src/Skobkin/Bundle/PointToolsBundle/Service/SubscriptionsManager.php index e5cbc28..97180dc 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Service/SubscriptionsManager.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Service/SubscriptionsManager.php @@ -3,6 +3,7 @@ namespace Skobkin\Bundle\PointToolsBundle\Service; use Doctrine\ORM\EntityManager; +use Psr\Log\LoggerInterface; use Skobkin\Bundle\PointToolsBundle\Entity\Subscription; use Skobkin\Bundle\PointToolsBundle\Entity\SubscriptionEvent; use Skobkin\Bundle\PointToolsBundle\Entity\User; @@ -21,11 +22,17 @@ class SubscriptionsManager */ private $eventDispatcher; + /** + * @var LoggerInterface + */ + private $logger; - public function __construct(EntityManager $entityManager, EventDispatcherInterface $eventDispatcher) + + public function __construct(EntityManager $entityManager, EventDispatcherInterface $eventDispatcher, LoggerInterface $logger) { $this->em = $entityManager; $this->eventDispatcher = $eventDispatcher; + $this->logger = $logger; } /** @@ -42,16 +49,16 @@ class SubscriptionsManager $oldSubscribersList[] = $subscription->getSubscriber(); } + $this->logger->debug('Counting user subscribers diff', ['user_id' => $user->getId()]); + $subscribedList = $this->getUsersListsDiff($newSubscribersList, $oldSubscribersList); $unsubscribedList = $this->getUsersListsDiff($oldSubscribersList, $newSubscribersList); + $this->logger->debug(sprintf('User has %d subscribed and %d unsubscribed users', count($subscribedList), count($unsubscribedList))); + $this->processSubscribedUsers($user, $subscribedList); $this->processUnsubscribedUsers($user, $unsubscribedList); - // Removing users from database - // @todo Maybe remove via ORM - $this->em->getRepository('SkobkinPointToolsBundle:Subscription')->removeSubscribers($user, $unsubscribedList); - $this->dispatchSubscribersUpdatedEvent($user, $subscribedList, $unsubscribedList); } @@ -84,6 +91,8 @@ class SubscriptionsManager */ private function processSubscribedUsers(User $user, array $subscribers) { + $this->logger->debug('Processing subscribed users'); + foreach ($subscribers as $subscriber) { $subscription = new Subscription($user, $subscriber); @@ -103,12 +112,18 @@ class SubscriptionsManager */ private function processUnsubscribedUsers(User $user, array $subscribers) { + $this->logger->debug('Processing unsubscribed users'); + foreach ($subscribers as $subscriber) { $logEvent = new SubscriptionEvent($user, $subscriber, SubscriptionEvent::ACTION_UNSUBSCRIBE); $this->em->persist($logEvent); $user->addNewSubscriberEvent($logEvent); } + + // Removing users from database + // @todo Maybe remove via ORM + $this->em->getRepository('SkobkinPointToolsBundle:Subscription')->removeSubscribers($user, $subscribers); } /**