LoadSubscribersData fixture added. Subscription and SubscriptionEvent entities refactored. SubscriptionsManager refactored as well.
This commit is contained in:
parent
24a173af16
commit
6e5e0435ac
|
@ -0,0 +1,79 @@
|
|||
<?php
|
||||
|
||||
namespace Skobkin\Bundle\PointToolsBundle\DataFixtures\ORM;
|
||||
|
||||
use Doctrine\Common\DataFixtures\AbstractFixture;
|
||||
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
|
||||
use Doctrine\Common\Persistence\ObjectManager;
|
||||
use Skobkin\Bundle\PointToolsBundle\Entity\Subscription;
|
||||
use Skobkin\Bundle\PointToolsBundle\Entity\SubscriptionEvent;
|
||||
use Skobkin\Bundle\PointToolsBundle\Entity\User;
|
||||
|
||||
/**
|
||||
* Load user subscriptions
|
||||
*/
|
||||
class LoadSubscribersData extends AbstractFixture implements OrderedFixtureInterface
|
||||
{
|
||||
public function load(ObjectManager $om)
|
||||
{
|
||||
/** @var User[] $users */
|
||||
$users = [
|
||||
$this->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;
|
||||
}
|
||||
}
|
|
@ -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
|
||||
*
|
||||
|
|
|
@ -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
|
||||
*
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue