Base user-related and subscriptions-related entities refactored and optimized.

This commit is contained in:
Alexey Skobkin 2017-01-08 18:29:12 +03:00
parent 7dacc3e4d3
commit c3d430e8f8
5 changed files with 42 additions and 300 deletions

View file

@ -81,11 +81,7 @@ class ImportUsersCommand extends ContainerAwareCommand
$createdAt = new \DateTime(); $createdAt = new \DateTime();
} }
$user = new User(); $user = (new User($row[0], $row[1], $row[2]))
$user
->setId($row[0])
->setLogin($row[1])
->setName($row[2])
->setCreatedAt($createdAt) ->setCreatedAt($createdAt)
; ;

View file

@ -5,12 +5,10 @@ namespace Skobkin\Bundle\PointToolsBundle\Entity;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
/** /**
* Subscription
*
* @ORM\Table(name="subscriptions", schema="subscriptions", uniqueConstraints={ * @ORM\Table(name="subscriptions", schema="subscriptions", uniqueConstraints={
* @ORM\UniqueConstraint(name="subscription_unique", columns={"author_id", "subscriber_id"})} * @ORM\UniqueConstraint(name="subscription_unique", columns={"author_id", "subscriber_id"})}
* ) * )
* @ORM\Entity(repositoryClass="Skobkin\Bundle\PointToolsBundle\Repository\SubscriptionRepository") * @ORM\Entity(repositoryClass="Skobkin\Bundle\PointToolsBundle\Repository\SubscriptionRepository", readOnly=true)
*/ */
class Subscription class Subscription
{ {
@ -34,33 +32,21 @@ class Subscription
/** /**
* Subscription constructor.
*
* @param User $author * @param User $author
* @param User $subscriber * @param User $subscriber
*/ */
public function __construct(User $author = null, User $subscriber = null) public function __construct(User $author, User $subscriber)
{ {
$this->author = $author; $this->author = $author;
$this->subscriber = $subscriber; $this->subscriber = $subscriber;
} }
/** public function getAuthor(): User
* Get author
*
* @return User
*/
public function getAuthor()
{ {
return $this->author; return $this->author;
} }
/** public function getSubscriber(): User
* Get subscriber
*
* @return User
*/
public function getSubscriber()
{ {
return $this->subscriber; return $this->subscriber;
} }

View file

@ -5,8 +5,6 @@ namespace Skobkin\Bundle\PointToolsBundle\Entity;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
/** /**
* SubscriptionEvent
*
* @ORM\Table(name="log", schema="subscriptions", indexes={ * @ORM\Table(name="log", schema="subscriptions", indexes={
* @ORM\Index(name="author_idx", columns={"author_id"}), * @ORM\Index(name="author_idx", columns={"author_id"}),
* @ORM\Index(name="subscriber_idx", columns={"subscriber_id"}), * @ORM\Index(name="subscriber_idx", columns={"subscriber_id"}),
@ -21,7 +19,7 @@ class SubscriptionEvent
const ACTION_UNSUBSCRIBE = 'unsubscribe'; const ACTION_UNSUBSCRIBE = 'unsubscribe';
/** /**
* @var integer * @var int
* *
* @ORM\Column(name="id", type="integer") * @ORM\Column(name="id", type="integer")
* @ORM\Id * @ORM\Id
@ -40,7 +38,7 @@ class SubscriptionEvent
/** /**
* @var User Blog subscriber * @var User Blog subscriber
* *
* @ORM\ManyToOne(targetEntity="User", inversedBy="newSubscriptionEvents") * @ORM\ManyToOne(targetEntity="User")
* @ORM\JoinColumn(name="subscriber_id", nullable=false) * @ORM\JoinColumn(name="subscriber_id", nullable=false)
*/ */
private $subscriber; private $subscriber;
@ -65,7 +63,7 @@ class SubscriptionEvent
* @param User $subscriber * @param User $subscriber
* @param string $action * @param string $action
*/ */
public function __construct(User $author = null, User $subscriber = null, $action = self::ACTION_SUBSCRIBE) public function __construct(User $author, User $subscriber, string $action = self::ACTION_SUBSCRIBE)
{ {
$this->author = $author; $this->author = $author;
$this->subscriber = $subscriber; $this->subscriber = $subscriber;
@ -82,52 +80,27 @@ class SubscriptionEvent
} }
} }
/** public function getId(): int
* Get id
*
* @return integer
*/
public function getId()
{ {
return $this->id; return $this->id;
} }
/** public function getDate(): \DateTime
* Get date
*
* @return \DateTime
*/
public function getDate()
{ {
return $this->date; return $this->date;
} }
/** public function getSubscriber(): User
* Get subscriber
*
* @return User
*/
public function getSubscriber()
{ {
return $this->subscriber; return $this->subscriber;
} }
/** public function getAuthor(): User
* Get author
*
* @return User
*/
public function getAuthor()
{ {
return $this->author; return $this->author;
} }
/** public function getAction(): string
* Get action
*
* @return string
*/
public function getAction()
{ {
return $this->action; return $this->action;
} }

View file

@ -6,8 +6,6 @@ use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
/** /**
* User
*
* @ORM\Table(name="users", schema="users") * @ORM\Table(name="users", schema="users")
* @ORM\Entity(repositoryClass="Skobkin\Bundle\PointToolsBundle\Repository\UserRepository") * @ORM\Entity(repositoryClass="Skobkin\Bundle\PointToolsBundle\Repository\UserRepository")
* @ORM\HasLifecycleCallbacks * @ORM\HasLifecycleCallbacks
@ -15,7 +13,7 @@ use Doctrine\ORM\Mapping as ORM;
class User class User
{ {
/** /**
* @var integer * @var int
* *
* @ORM\Column(name="id", type="integer") * @ORM\Column(name="id", type="integer")
* @ORM\Id * @ORM\Id
@ -51,29 +49,22 @@ class User
private $updatedAt; private $updatedAt;
/** /**
* @var ArrayCollection * @var Subscription|ArrayCollection
* *
* @ORM\OneToMany(targetEntity="Subscription", mappedBy="author", fetch="EXTRA_LAZY") * @ORM\OneToMany(targetEntity="Subscription", mappedBy="author", fetch="EXTRA_LAZY")
*/ */
private $subscribers; private $subscribers;
/** /**
* @var ArrayCollection * @var Subscription|ArrayCollection
* *
* @ORM\OneToMany(targetEntity="Subscription", mappedBy="subscriber", fetch="EXTRA_LAZY") * @ORM\OneToMany(targetEntity="Subscription", mappedBy="subscriber", fetch="EXTRA_LAZY")
*/ */
private $subscriptions; private $subscriptions;
/** /**
* @var ArrayCollection * @var SubscriptionEvent|ArrayCollection
* * @ORM\OneToMany(targetEntity="SubscriptionEvent", mappedBy="author", fetch="EXTRA_LAZY")
* @ORM\OneToMany(targetEntity="SubscriptionEvent", mappedBy="subscriber")
*/
private $newSubscriptionEvents;
/**
* @var ArrayCollection
* @ORM\OneToMany(targetEntity="SubscriptionEvent", mappedBy="author")
*/ */
private $newSubscriberEvents; private $newSubscriberEvents;
@ -83,7 +74,7 @@ class User
* @param string $login * @param string $login
* @param string $name * @param string $name
*/ */
public function __construct($id = null, $login = null, $name = null) public function __construct(int $id, string $login = null, string $name = null)
{ {
$this->id = $id; $this->id = $id;
$this->login = $login; $this->login = $login;
@ -92,7 +83,6 @@ class User
$this->subscribers = new ArrayCollection(); $this->subscribers = new ArrayCollection();
$this->subscriptions = new ArrayCollection(); $this->subscriptions = new ArrayCollection();
$this->newSubscriberEvents = new ArrayCollection(); $this->newSubscriberEvents = new ArrayCollection();
$this->newSubscriptionEvents = new ArrayCollection();
} }
/** /**
@ -113,101 +103,52 @@ class User
$this->updatedAt = new \DateTime(); $this->updatedAt = new \DateTime();
} }
/** public function getId(): int
* Get id
*
* @return integer
*/
public function getId()
{ {
return $this->id; return $this->id;
} }
/** /**
* Set id of user (for API services only)
*
* @param integer $id
* @return User
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* Set login
*
* @param string $login * @param string $login
* @return User * @return User
*/ */
public function setLogin($login) public function setLogin(string $login): self
{ {
$this->login = $login; $this->login = $login;
return $this; return $this;
} }
/** public function getLogin(): string
* Get login
*
* @return string
*/
public function getLogin()
{ {
return $this->login; return $this->login;
} }
/** public function setName(string $name): self
* Set name
*
* @param string $name
* @return User
*/
public function setName($name)
{ {
$this->name = $name; $this->name = $name;
return $this; return $this;
} }
/** public function getName(): string
* Get name
*
* @return string
*/
public function getName()
{ {
return $this->name; return $this->name;
} }
/** public function addSubscriber(Subscription $subscribers): self
* Add subscribers
*
* @param Subscription $subscribers
* @return User
*/
public function addSubscriber(Subscription $subscribers)
{ {
$this->subscribers[] = $subscribers; $this->subscribers[] = $subscribers;
return $this; return $this;
} }
/**
* Remove subscribers
*
* @param Subscription $subscribers
*/
public function removeSubscriber(Subscription $subscribers) public function removeSubscriber(Subscription $subscribers)
{ {
$this->subscribers->removeElement($subscribers); $this->subscribers->removeElement($subscribers);
} }
/** /**
* Get subscribers
*
* @return Subscription[]|ArrayCollection * @return Subscription[]|ArrayCollection
*/ */
public function getSubscribers() public function getSubscribers()
@ -216,78 +157,14 @@ class User
} }
/** /**
* Add subscriptions * @return Subscription[]|ArrayCollection
*
* @param Subscription $subscriptions
* @return User
*/
public function addSubscription(Subscription $subscriptions)
{
$this->subscriptions[] = $subscriptions;
return $this;
}
/**
* Remove subscriptions
*
* @param Subscription $subscriptions
*/
public function removeSubscription(Subscription $subscriptions)
{
$this->subscriptions->removeElement($subscriptions);
}
/**
* Get subscriptions
*
* @return ArrayCollection
*/ */
public function getSubscriptions() public function getSubscriptions()
{ {
return $this->subscriptions; return $this->subscriptions;
} }
/** public function addNewSubscriberEvent(SubscriptionEvent $newSubscriberEvents): self
* Add newSubscriptionEvents
*
* @param SubscriptionEvent $newSubscriptionEvents
* @return User
*/
public function addNewSubscriptionEvent(SubscriptionEvent $newSubscriptionEvents)
{
$this->newSubscriptionEvents[] = $newSubscriptionEvents;
return $this;
}
/**
* Remove newSubscriptionEvents
*
* @param SubscriptionEvent $newSubscriptionEvents
*/
public function removeNewSubscriptionEvent(SubscriptionEvent $newSubscriptionEvents)
{
$this->newSubscriptionEvents->removeElement($newSubscriptionEvents);
}
/**
* Get newSubscriptionEvents
*
* @return ArrayCollection
*/
public function getNewSubscriptionEvents()
{
return $this->newSubscriptionEvents;
}
/**
* Add newSubscriberEvents
*
* @param SubscriptionEvent $newSubscriberEvents
* @return User
*/
public function addNewSubscriberEvent(SubscriptionEvent $newSubscriberEvents)
{ {
$this->newSubscriberEvents[] = $newSubscriberEvents; $this->newSubscriberEvents[] = $newSubscriberEvents;
@ -295,47 +172,26 @@ class User
} }
/** /**
* Remove newSubscriberEvents * @return SubscriptionEvent[]|ArrayCollection
*
* @param SubscriptionEvent $newSubscriberEvents
*/
public function removeNewSubscriberEvent(SubscriptionEvent $newSubscriberEvents)
{
$this->newSubscriberEvents->removeElement($newSubscriberEvents);
}
/**
* Get newSubscriberEvents
*
* @return ArrayCollection
*/ */
public function getNewSubscriberEvents() public function getNewSubscriberEvents()
{ {
return $this->newSubscriberEvents; return $this->newSubscriberEvents;
} }
/** public function getCreatedAt(): \DateTime
* @return \DateTime
*/
public function getCreatedAt()
{ {
return $this->createdAt; return $this->createdAt;
} }
/** public function setCreatedAt(\DateTime $createdAt): self
* @param \DateTime $createdAt
* @return User
*/
public function setCreatedAt($createdAt)
{ {
$this->createdAt = $createdAt; $this->createdAt = $createdAt;
return $this; return $this;
} }
/** public function getUpdatedAt(): \DateTime
* @return \DateTime
*/
public function getUpdatedAt()
{ {
return $this->updatedAt; return $this->updatedAt;
} }

View file

@ -5,14 +5,11 @@ namespace Skobkin\Bundle\PointToolsBundle\Entity;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
/** /**
* UserRenameEvent
*
* @ORM\Table(name="rename_log", schema="users", indexes={ * @ORM\Table(name="rename_log", schema="users", indexes={
* @ORM\Index(name="idx_rename_log_date", columns={"date"}), * @ORM\Index(name="idx_rename_log_date", columns={"date"}),
* @ORM\Index(name="idx_rename_log_old_login", columns={"old_login"}) * @ORM\Index(name="idx_rename_log_old_login", columns={"old_login"})
* }) * })
* @ORM\Entity * @ORM\Entity(readOnly=true)
* @ORM\HasLifecycleCallbacks
*/ */
class UserRenameEvent class UserRenameEvent
{ {
@ -48,96 +45,30 @@ class UserRenameEvent
private $oldLogin; private $oldLogin;
public function __construct(User $user, $old) public function __construct(User $user, string $old)
{ {
$this->user = $user; $this->user = $user;
$this->oldLogin = $old; $this->oldLogin = $old;
}
/**
* @ORM\PrePersist
*/
public function prePersist()
{
$this->date = new \DateTime(); $this->date = new \DateTime();
} }
/** public function getId(): int
* Get id
*
* @return integer
*/
public function getId()
{ {
return $this->id; return $this->id;
} }
/** public function getDate(): \DateTime
* Set date
*
* @param \DateTime $date
* @return UserRenameEvent
*/
public function setDate($date)
{
$this->date = $date;
return $this;
}
/**
* Get date
*
* @return \DateTime
*/
public function getDate()
{ {
return $this->date; return $this->date;
} }
/** public function getUser(): User
* Set oldLogin
*
* @param string $oldLogin
* @return UserRenameEvent
*/
public function setOldLogin($oldLogin)
{
$this->oldLogin = $oldLogin;
return $this;
}
/**
* Get oldLogin
*
* @return string
*/
public function getOldLogin()
{
return $this->oldLogin;
}
/**
* Set user
*
* @param User $user
* @return UserRenameEvent
*/
public function setUser(User $user)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return User
*/
public function getUser()
{ {
return $this->user; return $this->user;
} }
public function getOldLogin(): string
{
return $this->oldLogin;
}
} }