Merge pull request #3264 from wallabag/trait-timestamps

Add EntityTimestampsTrait to handle dates
This commit is contained in:
Jérémy Benoist 2017-07-11 07:30:08 +02:00 committed by GitHub
commit 9aa11176b8
5 changed files with 36 additions and 48 deletions

View file

@ -10,6 +10,7 @@ use JMS\Serializer\Annotation\SerializedName;
use JMS\Serializer\Annotation\VirtualProperty; use JMS\Serializer\Annotation\VirtualProperty;
use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Constraints as Assert;
use Wallabag\CoreBundle\Entity\Entry; use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Helper\EntityTimestampsTrait;
use Wallabag\UserBundle\Entity\User; use Wallabag\UserBundle\Entity\User;
/** /**
@ -22,6 +23,8 @@ use Wallabag\UserBundle\Entity\User;
*/ */
class Annotation class Annotation
{ {
use EntityTimestampsTrait;
/** /**
* @var int * @var int
* *
@ -133,18 +136,6 @@ class Annotation
return $this->text; return $this->text;
} }
/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function timestamps()
{
if (null === $this->createdAt) {
$this->createdAt = new \DateTime();
}
$this->updatedAt = new \DateTime();
}
/** /**
* Get created. * Get created.
* *

View file

@ -12,6 +12,7 @@ use JMS\Serializer\Annotation\VirtualProperty;
use JMS\Serializer\Annotation\XmlRoot; use JMS\Serializer\Annotation\XmlRoot;
use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Constraints as Assert;
use Wallabag\AnnotationBundle\Entity\Annotation; use Wallabag\AnnotationBundle\Entity\Annotation;
use Wallabag\CoreBundle\Helper\EntityTimestampsTrait;
use Wallabag\UserBundle\Entity\User; use Wallabag\UserBundle\Entity\User;
/** /**
@ -32,6 +33,8 @@ use Wallabag\UserBundle\Entity\User;
*/ */
class Entry class Entry
{ {
use EntityTimestampsTrait;
/** @Serializer\XmlAttribute */ /** @Serializer\XmlAttribute */
/** /**
* @var int * @var int
@ -472,19 +475,6 @@ class Entry
return $this->updatedAt; return $this->updatedAt;
} }
/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function timestamps()
{
if (null === $this->createdAt) {
$this->createdAt = new \DateTime();
}
$this->updatedAt = new \DateTime();
}
/** /**
* @return ArrayCollection<Annotation> * @return ArrayCollection<Annotation>
*/ */

View file

@ -4,6 +4,7 @@ namespace Wallabag\CoreBundle\Entity;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Constraints as Assert;
use Wallabag\CoreBundle\Helper\EntityTimestampsTrait;
use Wallabag\UserBundle\Entity\User; use Wallabag\UserBundle\Entity\User;
/** /**
@ -15,6 +16,8 @@ use Wallabag\UserBundle\Entity\User;
*/ */
class SiteCredential class SiteCredential
{ {
use EntityTimestampsTrait;
/** /**
* @var int * @var int
* *
@ -182,14 +185,4 @@ class SiteCredential
{ {
return $this->user; return $this->user;
} }
/**
* @ORM\PrePersist
*/
public function timestamps()
{
if (null === $this->createdAt) {
$this->createdAt = new \DateTime();
}
}
} }

View file

@ -0,0 +1,24 @@
<?php
namespace Wallabag\CoreBundle\Helper;
use Doctrine\ORM\Mapping as ORM;
/**
* Trait to handle created & updated date of an Entity.
*/
trait EntityTimestampsTrait
{
/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function timestamps()
{
if (null === $this->createdAt) {
$this->createdAt = new \DateTime();
}
$this->updatedAt = new \DateTime();
}
}

View file

@ -15,6 +15,7 @@ use Symfony\Component\Security\Core\User\UserInterface;
use Wallabag\ApiBundle\Entity\Client; use Wallabag\ApiBundle\Entity\Client;
use Wallabag\CoreBundle\Entity\Config; use Wallabag\CoreBundle\Entity\Config;
use Wallabag\CoreBundle\Entity\Entry; use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Helper\EntityTimestampsTrait;
/** /**
* User. * User.
@ -29,6 +30,8 @@ use Wallabag\CoreBundle\Entity\Entry;
*/ */
class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterface class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterface
{ {
use EntityTimestampsTrait;
/** @Serializer\XmlAttribute */ /** @Serializer\XmlAttribute */
/** /**
* @var int * @var int
@ -138,19 +141,6 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
$this->roles = ['ROLE_USER']; $this->roles = ['ROLE_USER'];
} }
/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function timestamps()
{
if (null === $this->createdAt) {
$this->createdAt = new \DateTime();
}
$this->updatedAt = new \DateTime();
}
/** /**
* Set name. * Set name.
* *