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 = [])
|
|
|
|
{
|
|
|
|
/** @var Subscription[] $tmpOldSubscribers */
|
|
|
|
$tmpOldSubscribers = $user->getSubscribers();
|
|
|
|
|
|
|
|
$oldSubscribersList = [];
|
|
|
|
|
|
|
|
foreach ($tmpOldSubscribers as $subscription) {
|
|
|
|
$oldSubscribersList[] = $subscription->getSubscriber();
|
|
|
|
}
|
|
|
|
|
2017-01-07 18:46:50 +00:00
|
|
|
// @todo remove
|
2015-06-02 02:27:19 +00:00
|
|
|
$isFirstTime = false;
|
|
|
|
|
|
|
|
// Preventing to add garbage subscriptions for first processing
|
|
|
|
// @todo improve algorithm
|
|
|
|
if ((count($oldSubscribersList) === 0) && (count($newSubscribersList) > 1)) {
|
|
|
|
$isFirstTime = true;
|
|
|
|
}
|
|
|
|
|
2015-05-28 22:47:06 +00:00
|
|
|
unset($tmpOldSubscribers);
|
|
|
|
|
|
|
|
$subscribedList = $this->getUsersListsDiff($newSubscribersList, $oldSubscribersList);
|
|
|
|
$unsubscribedList = $this->getUsersListsDiff($oldSubscribersList, $newSubscribersList);
|
|
|
|
|
|
|
|
/** @var User $subscribedUser */
|
|
|
|
foreach ($subscribedList as $subscribedUser) {
|
2016-12-11 23:21:05 +00:00
|
|
|
$subscription = new Subscription($user, $subscribedUser);
|
2015-05-28 22:47:06 +00:00
|
|
|
|
|
|
|
$user->addSubscriber($subscription);
|
2016-12-11 23:21:05 +00:00
|
|
|
$this->em->persist($subscription);
|
2015-05-28 22:47:06 +00:00
|
|
|
|
2015-06-02 02:27:19 +00:00
|
|
|
// If it's not first processing
|
|
|
|
if (!$isFirstTime) {
|
2016-12-11 23:21:05 +00:00
|
|
|
$logEvent = new SubscriptionEvent($user, $subscribedUser, SubscriptionEvent::ACTION_SUBSCRIBE);
|
|
|
|
$this->em->persist($logEvent);
|
2015-06-02 02:27:19 +00:00
|
|
|
|
|
|
|
$user->addNewSubscriberEvent($logEvent);
|
|
|
|
}
|
2015-05-28 22:47:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @var User $unsubscribedUser */
|
|
|
|
foreach ($unsubscribedList as $unsubscribedUser) {
|
2016-12-11 23:21:05 +00:00
|
|
|
$logEvent = new SubscriptionEvent($user, $unsubscribedUser, SubscriptionEvent::ACTION_UNSUBSCRIBE);
|
|
|
|
$this->em->persist($logEvent);
|
2015-05-28 22:47:06 +00:00
|
|
|
|
|
|
|
$user->addNewSubscriberEvent($logEvent);
|
|
|
|
}
|
|
|
|
|
2017-01-07 18:46:50 +00:00
|
|
|
// Removing users from database
|
|
|
|
$this->em->getRepository('SkobkinPointToolsBundle:Subscription')->removeSubscribers($user, $unsubscribedList);
|
2015-05-28 22:47:06 +00:00
|
|
|
|
2017-01-07 18:46:50 +00:00
|
|
|
// Dispatching event
|
|
|
|
$subscribersUpdatedEvent = new UserSubscribersUpdatedEvent($user, $subscribedList, $unsubscribedList);
|
|
|
|
$this->eventDispatcher->dispatch(UserSubscribersUpdatedEvent::NAME, $subscribersUpdatedEvent);
|
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
|
|
|
*
|
2015-05-28 22:47:06 +00:00
|
|
|
* @return User[] Diff
|
|
|
|
*/
|
|
|
|
public function getUsersListsDiff(array $list1 = [], array $list2 = [])
|
|
|
|
{
|
|
|
|
$hash1 = [];
|
|
|
|
$hash2 = [];
|
|
|
|
|
|
|
|
foreach ($list1 as $user) {
|
|
|
|
$hash1[$user->getId()] = $user;
|
|
|
|
}
|
|
|
|
foreach ($list2 as $user) {
|
|
|
|
$hash2[$user->getId()] = $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_diff_key($hash1, $hash2);
|
|
|
|
}
|
|
|
|
}
|