2015-05-28 22:47:06 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Skobkin\Bundle\PointToolsBundle\Service;
|
|
|
|
|
|
|
|
use Doctrine\ORM\EntityManager;
|
2017-01-09 01:28:33 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2015-05-28 22:47:06 +00:00
|
|
|
use Skobkin\Bundle\PointToolsBundle\Entity\Subscription;
|
|
|
|
use Skobkin\Bundle\PointToolsBundle\Entity\SubscriptionEvent;
|
|
|
|
use Skobkin\Bundle\PointToolsBundle\Entity\User;
|
2017-01-07 18:46:50 +00:00
|
|
|
use Skobkin\Bundle\PointToolsBundle\Event\UserSubscribersUpdatedEvent;
|
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
2015-05-28 22:47:06 +00:00
|
|
|
|
|
|
|
class SubscriptionsManager
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var EntityManager
|
|
|
|
*/
|
2017-01-07 18:46:50 +00:00
|
|
|
private $em;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var EventDispatcherInterface
|
|
|
|
*/
|
|
|
|
private $eventDispatcher;
|
2015-05-28 22:47:06 +00:00
|
|
|
|
2017-01-09 01:28:33 +00:00
|
|
|
/**
|
|
|
|
* @var LoggerInterface
|
|
|
|
*/
|
|
|
|
private $logger;
|
2015-05-28 22:47:06 +00:00
|
|
|
|
2017-01-09 01:28:33 +00:00
|
|
|
|
|
|
|
public function __construct(EntityManager $entityManager, EventDispatcherInterface $eventDispatcher, LoggerInterface $logger)
|
2015-05-28 22:47:06 +00:00
|
|
|
{
|
|
|
|
$this->em = $entityManager;
|
2017-01-07 18:46:50 +00:00
|
|
|
$this->eventDispatcher = $eventDispatcher;
|
2017-01-09 01:28:33 +00:00
|
|
|
$this->logger = $logger;
|
2015-05-28 22:47:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param User $user
|
2017-01-07 18:46:50 +00:00
|
|
|
* @param User[] $newSubscribersList
|
2015-05-28 22:47:06 +00:00
|
|
|
*/
|
|
|
|
public function updateUserSubscribers(User $user, $newSubscribersList = [])
|
|
|
|
{
|
|
|
|
$tmpOldSubscribers = $user->getSubscribers();
|
|
|
|
|
|
|
|
$oldSubscribersList = [];
|
|
|
|
|
|
|
|
foreach ($tmpOldSubscribers as $subscription) {
|
|
|
|
$oldSubscribersList[] = $subscription->getSubscriber();
|
|
|
|
}
|
|
|
|
|
2017-01-09 01:28:33 +00:00
|
|
|
$this->logger->debug('Counting user subscribers diff', ['user_id' => $user->getId()]);
|
|
|
|
|
2015-05-28 22:47:06 +00:00
|
|
|
$subscribedList = $this->getUsersListsDiff($newSubscribersList, $oldSubscribersList);
|
|
|
|
$unsubscribedList = $this->getUsersListsDiff($oldSubscribersList, $newSubscribersList);
|
|
|
|
|
2017-01-09 01:28:33 +00:00
|
|
|
$this->logger->debug(sprintf('User has %d subscribed and %d unsubscribed users', count($subscribedList), count($unsubscribedList)));
|
|
|
|
|
2017-01-08 14:13:51 +00:00
|
|
|
$this->processSubscribedUsers($user, $subscribedList);
|
|
|
|
$this->processUnsubscribedUsers($user, $unsubscribedList);
|
2015-05-28 22:47:06 +00:00
|
|
|
|
2017-01-08 14:13:51 +00:00
|
|
|
$this->dispatchSubscribersUpdatedEvent($user, $subscribedList, $unsubscribedList);
|
2015-05-28 22:47:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compares $list1 against $list2 and returns the values in $list1 that are not present in $list2.
|
|
|
|
*
|
|
|
|
* @param User[] $list1
|
|
|
|
* @param User[] $list2
|
2017-01-06 21:38:20 +00:00
|
|
|
*
|
2017-01-08 14:13:51 +00:00
|
|
|
* @return User[]
|
2015-05-28 22:47:06 +00:00
|
|
|
*/
|
2017-01-08 14:13:51 +00:00
|
|
|
public function getUsersListsDiff(array $list1 = [], array $list2 = []): array
|
2015-05-28 22:47:06 +00:00
|
|
|
{
|
|
|
|
$hash1 = [];
|
|
|
|
$hash2 = [];
|
|
|
|
|
|
|
|
foreach ($list1 as $user) {
|
|
|
|
$hash1[$user->getId()] = $user;
|
|
|
|
}
|
|
|
|
foreach ($list2 as $user) {
|
|
|
|
$hash2[$user->getId()] = $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_diff_key($hash1, $hash2);
|
|
|
|
}
|
2017-01-08 14:13:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
* @param User[] $subscribers
|
|
|
|
*/
|
|
|
|
private function processSubscribedUsers(User $user, array $subscribers)
|
|
|
|
{
|
2017-01-09 01:28:33 +00:00
|
|
|
$this->logger->debug('Processing subscribed users');
|
|
|
|
|
2017-01-08 14:13:51 +00:00
|
|
|
foreach ($subscribers as $subscriber) {
|
|
|
|
$subscription = new Subscription($user, $subscriber);
|
|
|
|
|
|
|
|
$user->addSubscriber($subscription);
|
|
|
|
$this->em->persist($subscription);
|
|
|
|
|
|
|
|
$logEvent = new SubscriptionEvent($user, $subscriber, SubscriptionEvent::ACTION_SUBSCRIBE);
|
|
|
|
$this->em->persist($logEvent);
|
|
|
|
|
|
|
|
$user->addNewSubscriberEvent($logEvent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
* @param User[] $subscribers
|
|
|
|
*/
|
|
|
|
private function processUnsubscribedUsers(User $user, array $subscribers)
|
|
|
|
{
|
2017-01-09 01:28:33 +00:00
|
|
|
$this->logger->debug('Processing unsubscribed users');
|
|
|
|
|
2017-01-08 14:13:51 +00:00
|
|
|
foreach ($subscribers as $subscriber) {
|
|
|
|
$logEvent = new SubscriptionEvent($user, $subscriber, SubscriptionEvent::ACTION_UNSUBSCRIBE);
|
|
|
|
$this->em->persist($logEvent);
|
|
|
|
|
|
|
|
$user->addNewSubscriberEvent($logEvent);
|
|
|
|
}
|
2017-01-09 01:28:33 +00:00
|
|
|
|
|
|
|
// Removing users from database
|
|
|
|
// @todo Maybe remove via ORM
|
|
|
|
$this->em->getRepository('SkobkinPointToolsBundle:Subscription')->removeSubscribers($user, $subscribers);
|
2017-01-08 14:13:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
* @param User[] $subscribed
|
|
|
|
* @param User[] $unsubscribed
|
|
|
|
*/
|
|
|
|
private function dispatchSubscribersUpdatedEvent(User $user, array $subscribed, array $unsubscribed)
|
|
|
|
{
|
|
|
|
if (0 !== count($subscribed) || 0 !== count($unsubscribed)) {
|
|
|
|
// Dispatching event
|
|
|
|
$subscribersUpdatedEvent = new UserSubscribersUpdatedEvent($user, $subscribed, $unsubscribed);
|
|
|
|
$this->eventDispatcher->dispatch(UserSubscribersUpdatedEvent::NAME, $subscribersUpdatedEvent);
|
|
|
|
}
|
|
|
|
}
|
2015-05-28 22:47:06 +00:00
|
|
|
}
|