From 6e5e0435ac8ff68cea1661fe7685f5e915b95813 Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Mon, 12 Dec 2016 02:21:05 +0300 Subject: [PATCH] LoadSubscribersData fixture added. Subscription and SubscriptionEvent entities refactored. SubscriptionsManager refactored as well. --- .../DataFixtures/ORM/LoadSubscribersData.php | 79 +++++++++++++++++++ .../PointToolsBundle/Entity/Subscription.php | 22 +----- .../Entity/SubscriptionEvent.php | 64 +++------------ .../Service/SubscriptionsManager.php | 28 ++----- 4 files changed, 102 insertions(+), 91 deletions(-) create mode 100644 src/Skobkin/Bundle/PointToolsBundle/DataFixtures/ORM/LoadSubscribersData.php diff --git a/src/Skobkin/Bundle/PointToolsBundle/DataFixtures/ORM/LoadSubscribersData.php b/src/Skobkin/Bundle/PointToolsBundle/DataFixtures/ORM/LoadSubscribersData.php new file mode 100644 index 0000000..8030335 --- /dev/null +++ b/src/Skobkin/Bundle/PointToolsBundle/DataFixtures/ORM/LoadSubscribersData.php @@ -0,0 +1,79 @@ +getReference('test_user_99999'), + $this->getReference('test_user_99998'), + $this->getReference('test_user_99997'), + $this->getReference('test_user_99996'), + $this->getReference('test_user_99995'), + ]; + + foreach ($users as $key => $user) { + // At least 2 subscribers for first user in the list + if (0 === $key) { + $minimum = 2; + } else { + $minimum = mt_rand(0, count($users)); + } + + $subscribers = $this->getRandomSubscribers($users, $minimum); + + foreach ($subscribers as $subscriber) { + $subscription = new Subscription($user,$subscriber); + $subscriptionEvent = new SubscriptionEvent($user, $subscriber, SubscriptionEvent::ACTION_SUBSCRIBE); + $om->persist($subscription); + $om->persist($subscriptionEvent); + $user->addSubscriber($subscription); + } + } + + $om->flush(); + } + + public function getOrder() + { + return 4; + } + + /** + * Returns array with random users from given users array + * + * @param User[] $users + * @param int $min + * + * @return User[] + */ + private function getRandomSubscribers($users, $min = 0) + { + if (0 === $number = mt_rand($min, count($users))) { + return []; + } + + $keys = array_rand($users, $number); + + $result = []; + + foreach ($keys as $key) { + $result[] = $users[$key]; + } + + return $result; + } +} \ No newline at end of file diff --git a/src/Skobkin/Bundle/PointToolsBundle/Entity/Subscription.php b/src/Skobkin/Bundle/PointToolsBundle/Entity/Subscription.php index 7115faa..c4c2063 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Entity/Subscription.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Entity/Subscription.php @@ -34,16 +34,15 @@ class Subscription /** - * Set author + * Subscription constructor. * * @param User $author - * @return Subscription + * @param User $subscriber */ - public function setAuthor(User $author = null) + public function __construct(User $author = null, User $subscriber = null) { $this->author = $author; - - return $this; + $this->subscriber = $subscriber; } /** @@ -56,19 +55,6 @@ class Subscription return $this->author; } - /** - * Set subscriber - * - * @param User $subscriber - * @return Subscription - */ - public function setSubscriber(User $subscriber = null) - { - $this->subscriber = $subscriber; - - return $this; - } - /** * Get subscriber * diff --git a/src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionEvent.php b/src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionEvent.php index 36be206..912de03 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionEvent.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionEvent.php @@ -60,6 +60,18 @@ class SubscriptionEvent private $action; + /** + * @param User $author + * @param User $subscriber + * @param string $action + */ + public function __construct(User $author = null, User $subscriber = null, $action = self::ACTION_SUBSCRIBE) + { + $this->author = $author; + $this->subscriber = $subscriber; + $this->action = $action; + } + /** * @ORM\PrePersist */ @@ -80,19 +92,6 @@ class SubscriptionEvent return $this->id; } - /** - * Set date - * - * @param \DateTime $date - * @return SubscriptionEvent - */ - public function setDate($date) - { - $this->date = $date; - - return $this; - } - /** * Get date * @@ -103,19 +102,6 @@ class SubscriptionEvent return $this->date; } - /** - * Set subscriber - * - * @param User $subscriber - * @return SubscriptionEvent - */ - public function setSubscriber(User $subscriber) - { - $this->subscriber = $subscriber; - - return $this; - } - /** * Get subscriber * @@ -126,19 +112,6 @@ class SubscriptionEvent return $this->subscriber; } - /** - * Set author - * - * @param User $author - * @return SubscriptionEvent - */ - public function setAuthor(User $author) - { - $this->author = $author; - - return $this; - } - /** * Get author * @@ -149,19 +122,6 @@ class SubscriptionEvent return $this->author; } - /** - * Set action - * - * @param string $action - * @return SubscriptionEvent - */ - public function setAction($action) - { - $this->action = $action; - - return $this; - } - /** * Get action * diff --git a/src/Skobkin/Bundle/PointToolsBundle/Service/SubscriptionsManager.php b/src/Skobkin/Bundle/PointToolsBundle/Service/SubscriptionsManager.php index 09579b2..80231df 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Service/SubscriptionsManager.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Service/SubscriptionsManager.php @@ -54,28 +54,20 @@ class SubscriptionsManager /** @var User $subscribedUser */ foreach ($subscribedList as $subscribedUser) { - $subscription = new Subscription(); - $subscription - ->setAuthor($user) - ->setSubscriber($subscribedUser) - ; + $subscription = new Subscription($user, $subscribedUser); $user->addSubscriber($subscription); + $this->em->persist($subscription); // If it's not first processing if (!$isFirstTime) { - $logEvent = new SubscriptionEvent(); - $logEvent - ->setSubscriber($subscribedUser) - ->setAuthor($user) - ->setAction(SubscriptionEvent::ACTION_SUBSCRIBE); + $logEvent = new SubscriptionEvent($user, $subscribedUser, SubscriptionEvent::ACTION_SUBSCRIBE); + $this->em->persist($logEvent); $user->addNewSubscriberEvent($logEvent); - - $this->em->persist($logEvent); } - $this->em->persist($subscription); + } unset($subscribedList); @@ -90,16 +82,10 @@ class SubscriptionsManager /** @var User $unsubscribedUser */ foreach ($unsubscribedList as $unsubscribedUser) { - $logEvent = new SubscriptionEvent(); - $logEvent - ->setSubscriber($unsubscribedUser) - ->setAuthor($user) - ->setAction(SubscriptionEvent::ACTION_UNSUBSCRIBE) - ; + $logEvent = new SubscriptionEvent($user, $unsubscribedUser, SubscriptionEvent::ACTION_UNSUBSCRIBE); + $this->em->persist($logEvent); $user->addNewSubscriberEvent($logEvent); - - $this->em->persist($logEvent); } $unsubscribedQuery