From c3d430e8f83d72452e654d98c7ef73d4206f7ce7 Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Sun, 8 Jan 2017 18:29:12 +0300 Subject: [PATCH] Base user-related and subscriptions-related entities refactored and optimized. --- .../Command/ImportUsersCommand.php | 6 +- .../PointToolsBundle/Entity/Subscription.php | 22 +-- .../Entity/SubscriptionEvent.php | 43 +---- .../Bundle/PointToolsBundle/Entity/User.php | 182 ++---------------- .../Entity/UserRenameEvent.php | 89 +-------- 5 files changed, 42 insertions(+), 300 deletions(-) diff --git a/src/Skobkin/Bundle/PointToolsBundle/Command/ImportUsersCommand.php b/src/Skobkin/Bundle/PointToolsBundle/Command/ImportUsersCommand.php index e8bd044..87de45b 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Command/ImportUsersCommand.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Command/ImportUsersCommand.php @@ -81,11 +81,7 @@ class ImportUsersCommand extends ContainerAwareCommand $createdAt = new \DateTime(); } - $user = new User(); - $user - ->setId($row[0]) - ->setLogin($row[1]) - ->setName($row[2]) + $user = (new User($row[0], $row[1], $row[2])) ->setCreatedAt($createdAt) ; diff --git a/src/Skobkin/Bundle/PointToolsBundle/Entity/Subscription.php b/src/Skobkin/Bundle/PointToolsBundle/Entity/Subscription.php index c4c2063..75dffab 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Entity/Subscription.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Entity/Subscription.php @@ -5,12 +5,10 @@ namespace Skobkin\Bundle\PointToolsBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** - * Subscription - * * @ORM\Table(name="subscriptions", schema="subscriptions", uniqueConstraints={ * @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 { @@ -34,33 +32,21 @@ class Subscription /** - * Subscription constructor. - * * @param User $author * @param User $subscriber */ - public function __construct(User $author = null, User $subscriber = null) + public function __construct(User $author, User $subscriber) { $this->author = $author; $this->subscriber = $subscriber; } - /** - * Get author - * - * @return User - */ - public function getAuthor() + public function getAuthor(): User { return $this->author; } - /** - * Get subscriber - * - * @return User - */ - public function getSubscriber() + public function getSubscriber(): User { return $this->subscriber; } diff --git a/src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionEvent.php b/src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionEvent.php index 912de03..bb4ec6a 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionEvent.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionEvent.php @@ -5,8 +5,6 @@ namespace Skobkin\Bundle\PointToolsBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** - * SubscriptionEvent - * * @ORM\Table(name="log", schema="subscriptions", indexes={ * @ORM\Index(name="author_idx", columns={"author_id"}), * @ORM\Index(name="subscriber_idx", columns={"subscriber_id"}), @@ -21,7 +19,7 @@ class SubscriptionEvent const ACTION_UNSUBSCRIBE = 'unsubscribe'; /** - * @var integer + * @var int * * @ORM\Column(name="id", type="integer") * @ORM\Id @@ -40,7 +38,7 @@ class SubscriptionEvent /** * @var User Blog subscriber * - * @ORM\ManyToOne(targetEntity="User", inversedBy="newSubscriptionEvents") + * @ORM\ManyToOne(targetEntity="User") * @ORM\JoinColumn(name="subscriber_id", nullable=false) */ private $subscriber; @@ -65,7 +63,7 @@ class SubscriptionEvent * @param User $subscriber * @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->subscriber = $subscriber; @@ -82,52 +80,27 @@ class SubscriptionEvent } } - /** - * Get id - * - * @return integer - */ - public function getId() + public function getId(): int { return $this->id; } - /** - * Get date - * - * @return \DateTime - */ - public function getDate() + public function getDate(): \DateTime { return $this->date; } - /** - * Get subscriber - * - * @return User - */ - public function getSubscriber() + public function getSubscriber(): User { return $this->subscriber; } - /** - * Get author - * - * @return User - */ - public function getAuthor() + public function getAuthor(): User { return $this->author; } - /** - * Get action - * - * @return string - */ - public function getAction() + public function getAction(): string { return $this->action; } diff --git a/src/Skobkin/Bundle/PointToolsBundle/Entity/User.php b/src/Skobkin/Bundle/PointToolsBundle/Entity/User.php index ce0cf6b..489882a 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Entity/User.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Entity/User.php @@ -6,8 +6,6 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; /** - * User - * * @ORM\Table(name="users", schema="users") * @ORM\Entity(repositoryClass="Skobkin\Bundle\PointToolsBundle\Repository\UserRepository") * @ORM\HasLifecycleCallbacks @@ -15,7 +13,7 @@ use Doctrine\ORM\Mapping as ORM; class User { /** - * @var integer + * @var int * * @ORM\Column(name="id", type="integer") * @ORM\Id @@ -51,29 +49,22 @@ class User private $updatedAt; /** - * @var ArrayCollection + * @var Subscription|ArrayCollection * * @ORM\OneToMany(targetEntity="Subscription", mappedBy="author", fetch="EXTRA_LAZY") */ private $subscribers; /** - * @var ArrayCollection + * @var Subscription|ArrayCollection * * @ORM\OneToMany(targetEntity="Subscription", mappedBy="subscriber", fetch="EXTRA_LAZY") */ private $subscriptions; /** - * @var ArrayCollection - * - * @ORM\OneToMany(targetEntity="SubscriptionEvent", mappedBy="subscriber") - */ - private $newSubscriptionEvents; - - /** - * @var ArrayCollection - * @ORM\OneToMany(targetEntity="SubscriptionEvent", mappedBy="author") + * @var SubscriptionEvent|ArrayCollection + * @ORM\OneToMany(targetEntity="SubscriptionEvent", mappedBy="author", fetch="EXTRA_LAZY") */ private $newSubscriberEvents; @@ -83,7 +74,7 @@ class User * @param string $login * @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->login = $login; @@ -92,7 +83,6 @@ class User $this->subscribers = new ArrayCollection(); $this->subscriptions = new ArrayCollection(); $this->newSubscriberEvents = new ArrayCollection(); - $this->newSubscriptionEvents = new ArrayCollection(); } /** @@ -113,101 +103,52 @@ class User $this->updatedAt = new \DateTime(); } - /** - * Get id - * - * @return integer - */ - public function getId() + public function getId(): int { 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 * @return User */ - public function setLogin($login) + public function setLogin(string $login): self { $this->login = $login; return $this; } - /** - * Get login - * - * @return string - */ - public function getLogin() + public function getLogin(): string { return $this->login; } - /** - * Set name - * - * @param string $name - * @return User - */ - public function setName($name) + public function setName(string $name): self { $this->name = $name; return $this; } - /** - * Get name - * - * @return string - */ - public function getName() + public function getName(): string { return $this->name; } - /** - * Add subscribers - * - * @param Subscription $subscribers - * @return User - */ - public function addSubscriber(Subscription $subscribers) + public function addSubscriber(Subscription $subscribers): self { $this->subscribers[] = $subscribers; return $this; } - /** - * Remove subscribers - * - * @param Subscription $subscribers - */ public function removeSubscriber(Subscription $subscribers) { $this->subscribers->removeElement($subscribers); } /** - * Get subscribers - * * @return Subscription[]|ArrayCollection */ public function getSubscribers() @@ -216,78 +157,14 @@ class User } /** - * Add subscriptions - * - * @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 + * @return Subscription[]|ArrayCollection */ public function getSubscriptions() { return $this->subscriptions; } - /** - * 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) + public function addNewSubscriberEvent(SubscriptionEvent $newSubscriberEvents): self { $this->newSubscriberEvents[] = $newSubscriberEvents; @@ -295,47 +172,26 @@ class User } /** - * Remove newSubscriberEvents - * - * @param SubscriptionEvent $newSubscriberEvents - */ - public function removeNewSubscriberEvent(SubscriptionEvent $newSubscriberEvents) - { - $this->newSubscriberEvents->removeElement($newSubscriberEvents); - } - - /** - * Get newSubscriberEvents - * - * @return ArrayCollection + * @return SubscriptionEvent[]|ArrayCollection */ public function getNewSubscriberEvents() { return $this->newSubscriberEvents; } - /** - * @return \DateTime - */ - public function getCreatedAt() + public function getCreatedAt(): \DateTime { return $this->createdAt; } - /** - * @param \DateTime $createdAt - * @return User - */ - public function setCreatedAt($createdAt) + public function setCreatedAt(\DateTime $createdAt): self { $this->createdAt = $createdAt; + return $this; } - /** - * @return \DateTime - */ - public function getUpdatedAt() + public function getUpdatedAt(): \DateTime { return $this->updatedAt; } diff --git a/src/Skobkin/Bundle/PointToolsBundle/Entity/UserRenameEvent.php b/src/Skobkin/Bundle/PointToolsBundle/Entity/UserRenameEvent.php index a5e697f..1d8e94e 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Entity/UserRenameEvent.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Entity/UserRenameEvent.php @@ -5,14 +5,11 @@ namespace Skobkin\Bundle\PointToolsBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** - * UserRenameEvent - * * @ORM\Table(name="rename_log", schema="users", indexes={ * @ORM\Index(name="idx_rename_log_date", columns={"date"}), * @ORM\Index(name="idx_rename_log_old_login", columns={"old_login"}) * }) - * @ORM\Entity - * @ORM\HasLifecycleCallbacks + * @ORM\Entity(readOnly=true) */ class UserRenameEvent { @@ -48,96 +45,30 @@ class UserRenameEvent private $oldLogin; - public function __construct(User $user, $old) + public function __construct(User $user, string $old) { $this->user = $user; $this->oldLogin = $old; - } - - /** - * @ORM\PrePersist - */ - public function prePersist() - { $this->date = new \DateTime(); } - /** - * Get id - * - * @return integer - */ - public function getId() + public function getId(): int { return $this->id; } - /** - * Set date - * - * @param \DateTime $date - * @return UserRenameEvent - */ - public function setDate($date) - { - $this->date = $date; - - return $this; - } - - /** - * Get date - * - * @return \DateTime - */ - public function getDate() + public function getDate(): \DateTime { return $this->date; } - /** - * 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() + public function getUser(): User { return $this->user; } + + public function getOldLogin(): string + { + return $this->oldLogin; + } }