Some of repositories are now presented as services in container. SubscriptionsManager is now not dependent on EntityManager directly.

This commit is contained in:
Alexey Skobkin 2017-01-11 04:27:41 +03:00
parent ba574984d7
commit 4ca70ea9f3
2 changed files with 42 additions and 12 deletions

View File

@ -40,9 +40,10 @@ services:
app.point.subscriptions_manager:
class: Skobkin\Bundle\PointToolsBundle\Service\SubscriptionsManager
arguments:
- '@doctrine.orm.entity_manager'
- '@event_dispatcher'
- '@logger'
- '@app.point.subscription_repository'
- '@app.point.subscription_record_repository'
# Console commands
@ -58,6 +59,24 @@ services:
- { name: console.command }
# Entity repositories as services
# User
app.point.user_repository:
class: Skobkin\Bundle\PointToolsBundle\Repository\UserRepository
factory: 'doctrine:getRepository'
arguments: ['Skobkin\Bundle\PointToolsBundle\Entity\User']
# Subscription
app.point.subscription_repository:
class: Skobkin\Bundle\PointToolsBundle\Repository\SubscriptionRepository
factory: 'doctrine:getRepository'
arguments: ['Skobkin\Bundle\PointToolsBundle\Entity\Subscription']
# Subscription record/event
app.point.subscription_record_repository:
class: Skobkin\Bundle\PointToolsBundle\Repository\SubscriptionEventRepository
factory: 'doctrine:getRepository'
arguments: ['Skobkin\Bundle\PointToolsBundle\Entity\SubscriptionEvent']
# Factories
# User factory
app.point.user_factory:

View File

@ -2,20 +2,26 @@
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;
use Skobkin\Bundle\PointToolsBundle\Event\UserSubscribersUpdatedEvent;
use Skobkin\Bundle\PointToolsBundle\Repository\SubscriptionEventRepository;
use Skobkin\Bundle\PointToolsBundle\Repository\SubscriptionRepository;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class SubscriptionsManager
{
/**
* @var EntityManager
* @var SubscriptionRepository
*/
private $em;
private $subscriptionRepo;
/**
* @var SubscriptionEventRepository
*/
private $subscriptionRecordRepo;
/**
* @var EventDispatcherInterface
@ -28,11 +34,16 @@ class SubscriptionsManager
private $logger;
public function __construct(EntityManager $entityManager, EventDispatcherInterface $eventDispatcher, LoggerInterface $logger)
{
$this->em = $entityManager;
public function __construct(
EventDispatcherInterface $eventDispatcher,
LoggerInterface $logger,
SubscriptionRepository $subscriptionRepo,
SubscriptionEventRepository $subscriptionRecordRepo
) {
$this->eventDispatcher = $eventDispatcher;
$this->logger = $logger;
$this->subscriptionRepo = $subscriptionRepo;
$this->subscriptionRecordRepo = $subscriptionRecordRepo;
}
/**
@ -97,10 +108,10 @@ class SubscriptionsManager
$subscription = new Subscription($user, $subscriber);
$user->addSubscriber($subscription);
$this->em->persist($subscription);
$this->subscriptionRepo->add($subscription);
$logEvent = new SubscriptionEvent($user, $subscriber, SubscriptionEvent::ACTION_SUBSCRIBE);
$this->em->persist($logEvent);
$this->subscriptionRecordRepo->add($logEvent);
$user->addNewSubscriberEvent($logEvent);
}
@ -116,14 +127,14 @@ class SubscriptionsManager
foreach ($subscribers as $subscriber) {
$logEvent = new SubscriptionEvent($user, $subscriber, SubscriptionEvent::ACTION_UNSUBSCRIBE);
$this->em->persist($logEvent);
$this->subscriptionRecordRepo->add($logEvent);
$user->addNewSubscriberEvent($logEvent);
}
// Removing users from database
// @todo Maybe remove via ORM
$this->em->getRepository('SkobkinPointToolsBundle:Subscription')->removeSubscribers($user, $subscribers);
// @todo Refactor to collection usage (after dealing with orphanRemoval bug)
$this->subscriptionRepo->removeSubscribers($user, $subscribers);
}
/**