2015-05-28 22:47:06 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Skobkin\Bundle\PointToolsBundle\Service;
|
|
|
|
|
|
|
|
use Doctrine\ORM\EntityManager;
|
|
|
|
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-07 18:46:50 +00:00
|
|
|
public function __construct(EntityManager $entityManager, EventDispatcherInterface $eventDispatcher)
|
2015-05-28 22:47:06 +00:00
|
|
|
{
|
|
|
|
$this->em = $entityManager;
|
2017-01-07 18:46:50 +00:00
|
|
|
$this->eventDispatcher = $eventDispatcher;
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
$subscribedList = $this->getUsersListsDiff($newSubscribersList, $oldSubscribersList);
|
|
|
|
$unsubscribedList = $this->getUsersListsDiff($oldSubscribersList, $newSubscribersList);
|
|
|
|
|
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-07 18:46:50 +00:00
|
|
|
// Removing users from database
|
2017-01-08 14:13:51 +00:00
|
|
|
// @todo Maybe remove via ORM
|
2017-01-07 18:46:50 +00:00
|
|
|
$this->em->getRepository('SkobkinPointToolsBundle:Subscription')->removeSubscribers($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)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
foreach ($subscribers as $subscriber) {
|
|
|
|
$logEvent = new SubscriptionEvent($user, $subscriber, SubscriptionEvent::ACTION_UNSUBSCRIBE);
|
|
|
|
$this->em->persist($logEvent);
|
|
|
|
|
|
|
|
$user->addNewSubscriberEvent($logEvent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
}
|