Ported SubscriptionsManager.
This commit is contained in:
parent
ca1fb63604
commit
8a9a5028dc
|
@ -1,48 +1,26 @@
|
||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace src\PointToolsBundle\Service;
|
namespace App\Service;
|
||||||
|
|
||||||
|
use App\Entity\{Subscription, SubscriptionEvent, User};
|
||||||
|
use App\Event\UserSubscribersUpdatedEvent;
|
||||||
|
use App\Repository\{SubscriptionEventRepository, SubscriptionRepository};
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use src\PointToolsBundle\Entity\SubscriptionEvent;
|
|
||||||
use src\PointToolsBundle\Entity\User;
|
|
||||||
use src\PointToolsBundle\Repository\SubscriptionRepository;
|
|
||||||
use src\PointToolsBundle\Entity\{Subscription};
|
|
||||||
use src\PointToolsBundle\Event\UserSubscribersUpdatedEvent;
|
|
||||||
use src\PointToolsBundle\Repository\{SubscriptionEventRepository};
|
|
||||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||||
|
|
||||||
class SubscriptionsManager
|
class SubscriptionsManager
|
||||||
{
|
{
|
||||||
/** @var SubscriptionRepository */
|
|
||||||
private $subscriptionRepo;
|
|
||||||
|
|
||||||
/** @var SubscriptionEventRepository */
|
|
||||||
private $subscriptionRecordRepo;
|
|
||||||
|
|
||||||
/** @var EventDispatcherInterface */
|
|
||||||
private $eventDispatcher;
|
|
||||||
|
|
||||||
/** @var LoggerInterface */
|
|
||||||
private $logger;
|
|
||||||
|
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
EventDispatcherInterface $eventDispatcher,
|
private readonly EventDispatcherInterface $eventDispatcher,
|
||||||
LoggerInterface $logger,
|
private readonly LoggerInterface $logger,
|
||||||
SubscriptionRepository $subscriptionRepo,
|
private readonly SubscriptionRepository $subscriptionRepo,
|
||||||
SubscriptionEventRepository $subscriptionRecordRepo
|
private readonly SubscriptionEventRepository $subscriptionRecordRepo
|
||||||
) {
|
) {
|
||||||
$this->eventDispatcher = $eventDispatcher;
|
|
||||||
$this->logger = $logger;
|
|
||||||
$this->subscriptionRepo = $subscriptionRepo;
|
|
||||||
$this->subscriptionRecordRepo = $subscriptionRecordRepo;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** @param User[] $newSubscribersList */
|
||||||
* @param User $user
|
public function updateUserSubscribers(User $user, array $newSubscribersList = []): void
|
||||||
* @param User[] $newSubscribersList
|
|
||||||
*/
|
|
||||||
public function updateUserSubscribers(User $user, $newSubscribersList = []): void
|
|
||||||
{
|
{
|
||||||
// @todo optimize
|
// @todo optimize
|
||||||
$tmpOldSubscribers = $user->getSubscribers();
|
$tmpOldSubscribers = $user->getSubscribers();
|
||||||
|
@ -86,7 +64,7 @@ class SubscriptionsManager
|
||||||
$hash2[$user->getId()] = $user;
|
$hash2[$user->getId()] = $user;
|
||||||
}
|
}
|
||||||
|
|
||||||
return array_diff_key($hash1, $hash2);
|
return \array_diff_key($hash1, $hash2);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -101,10 +79,10 @@ class SubscriptionsManager
|
||||||
$subscription = new Subscription($user, $subscriber);
|
$subscription = new Subscription($user, $subscriber);
|
||||||
|
|
||||||
$user->addSubscriber($subscription);
|
$user->addSubscriber($subscription);
|
||||||
$this->subscriptionRepo->add($subscription);
|
$this->subscriptionRepo->save($subscription);
|
||||||
|
|
||||||
$logEvent = new SubscriptionEvent($user, $subscriber, SubscriptionEvent::ACTION_SUBSCRIBE);
|
$logEvent = new SubscriptionEvent($user, $subscriber, SubscriptionEvent::ACTION_SUBSCRIBE);
|
||||||
$this->subscriptionRecordRepo->add($logEvent);
|
$this->subscriptionRecordRepo->save($logEvent);
|
||||||
|
|
||||||
$user->addNewSubscriberEvent($logEvent);
|
$user->addNewSubscriberEvent($logEvent);
|
||||||
}
|
}
|
||||||
|
@ -120,7 +98,7 @@ class SubscriptionsManager
|
||||||
|
|
||||||
foreach ($subscribers as $subscriber) {
|
foreach ($subscribers as $subscriber) {
|
||||||
$logEvent = new SubscriptionEvent($user, $subscriber, SubscriptionEvent::ACTION_UNSUBSCRIBE);
|
$logEvent = new SubscriptionEvent($user, $subscriber, SubscriptionEvent::ACTION_UNSUBSCRIBE);
|
||||||
$this->subscriptionRecordRepo->add($logEvent);
|
$this->subscriptionRecordRepo->save($logEvent);
|
||||||
|
|
||||||
$user->addNewSubscriberEvent($logEvent);
|
$user->addNewSubscriberEvent($logEvent);
|
||||||
}
|
}
|
||||||
|
@ -131,7 +109,6 @@ class SubscriptionsManager
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param User $user
|
|
||||||
* @param User[] $subscribed
|
* @param User[] $subscribed
|
||||||
* @param User[] $unsubscribed
|
* @param User[] $unsubscribed
|
||||||
*/
|
*/
|
||||||
|
@ -140,7 +117,7 @@ class SubscriptionsManager
|
||||||
if (0 !== count($subscribed) || 0 !== count($unsubscribed)) {
|
if (0 !== count($subscribed) || 0 !== count($unsubscribed)) {
|
||||||
// Dispatching event
|
// Dispatching event
|
||||||
$subscribersUpdatedEvent = new UserSubscribersUpdatedEvent($user, $subscribed, $unsubscribed);
|
$subscribersUpdatedEvent = new UserSubscribersUpdatedEvent($user, $subscribed, $unsubscribed);
|
||||||
$this->eventDispatcher->dispatch(UserSubscribersUpdatedEvent::NAME, $subscribersUpdatedEvent);
|
$this->eventDispatcher->dispatch($subscribersUpdatedEvent, UserSubscribersUpdatedEvent::NAME);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue