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: app.point.subscriptions_manager:
class: Skobkin\Bundle\PointToolsBundle\Service\SubscriptionsManager class: Skobkin\Bundle\PointToolsBundle\Service\SubscriptionsManager
arguments: arguments:
- '@doctrine.orm.entity_manager'
- '@event_dispatcher' - '@event_dispatcher'
- '@logger' - '@logger'
- '@app.point.subscription_repository'
- '@app.point.subscription_record_repository'
# Console commands # Console commands
@ -58,6 +59,24 @@ services:
- { name: console.command } - { 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 # Factories
# User factory # User factory
app.point.user_factory: app.point.user_factory:

View file

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