Skip to content

Commit

Permalink
Merge pull request #225 from dotkernel/fix/use-datetime-immutable
Browse files Browse the repository at this point in the history
Issue #224: Use DatetimeImmutable instead of Datetime
  • Loading branch information
arhimede committed Apr 21, 2021
2 parents 991895e + b7e1827 commit 28e2a45
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 70 deletions.
9 changes: 3 additions & 6 deletions src/App/src/Common/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

namespace Frontend\App\Common;

use DateTime;
use Exception;
use Ramsey\Uuid\UuidInterface;
use Doctrine\ORM\Mapping as ORM;
use DateTimeImmutable;

/**
* Class AbstractEntity
Expand All @@ -24,8 +21,8 @@ abstract class AbstractEntity implements UuidAwareInterface, TimestampAwareInter
public function __construct()
{
$this->uuid = UuidOrderedTimeGenerator::generateUuid();
$this->created = new DateTime('now');
$this->updated = new DateTime('now');
$this->created = new DateTimeImmutable();
$this->updated = new DateTimeImmutable();
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/App/src/Common/TimestampAwareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Frontend\App\Common;

use DateTime;
use DateTimeImmutable;

/**
* Interface TimestampAwareInterface
Expand All @@ -13,14 +13,14 @@
interface TimestampAwareInterface
{
/**
* @return DateTime|null
* @return DateTimeImmutable|null
*/
public function getCreated(): DateTime;
public function getCreated(): ?DateTimeImmutable;

/**
* @return DateTime|null
* @return DateTimeImmutable|null
*/
public function getUpdated(): ?DateTime;
public function getUpdated(): ?DateTimeImmutable;

/**
* Update internal timestamps
Expand Down
49 changes: 29 additions & 20 deletions src/App/src/Common/TimestampAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Frontend\App\Common;

use DateTime;
use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM;
use Exception;

Expand All @@ -20,77 +20,86 @@ trait TimestampAwareTrait
private $dateFormat = 'Y-m-d H:i:s';

/**
* @ORM\Column(name="created", type="datetime")
* @var DateTime
* @ORM\Column(name="created", type="datetime_immutable")
* @var DateTimeImmutable
*/
protected $created;

/**
* @ORM\Column(name="updated", type="datetime", nullable=true)
* @var DateTime
* @ORM\Column(name="updated", type="datetime_immutable", nullable=true)
* @var DateTimeImmutable
*/
protected $updated;

/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
* @return void
*/
public function updateTimestamps()
public function updateTimestamps(): void
{
$this->touch();
}

/**
* @return DateTime
* @return DateTimeImmutable|null
*/
public function getCreated(): DateTime
public function getCreated(): ?DateTimeImmutable
{
return $this->created;
}

/**
* @return null|string
* @return string|null
*/
public function getCreatedFormatted()
public function getCreatedFormatted(): ?string
{
if ($this->created instanceof DateTime) {
if ($this->created instanceof DateTimeImmutable) {
return $this->created->format($this->dateFormat);
}

return null;
}

/**
* @return DateTime
* @return DateTimeImmutable|null
*/
public function getUpdated(): ?DateTime
public function getUpdated(): ?DateTimeImmutable
{
return $this->updated;
}

/**
* @return null|string
* @return string|null
*/
public function getUpdatedFormatted()
public function getUpdatedFormatted(): ?string
{
if ($this->updated instanceof DateTime) {
if ($this->updated instanceof DateTimeImmutable) {
return $this->updated->format($this->dateFormat);
}

return null;
}

/**
* @return $this
* @param string $dateFormat
*/
public function setDateFormat(string $dateFormat): void
{
$this->dateFormat = $dateFormat;
}

/**
* @return void
*/
public function touch(): void
{
try {
if (!($this->created instanceof DateTime)) {
$this->created = new DateTime('now');
if (!($this->created instanceof DateTimeImmutable)) {
$this->created = new DateTimeImmutable();
}

$this->updated = new DateTime('now');
$this->updated = new DateTimeImmutable();
} catch (Exception $exception) {
#TODO save the error message
}
Expand Down
3 changes: 3 additions & 0 deletions src/App/src/Common/UuidAwareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@
*/
interface UuidAwareInterface
{
/**
* @return UuidInterface
*/
public function getUuid(): UuidInterface;
}
29 changes: 0 additions & 29 deletions src/User/src/Entity/UserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

namespace Frontend\User\Entity;

use DateTime;
use Ramsey\Uuid\UuidInterface;

/**
* Interface UserInterface
* @package Frontend\User\Entity
Expand Down Expand Up @@ -85,37 +82,11 @@ public function addRole(UserRole $role): UserInterface;
*/
public function removeRole(UserRole $role): UserInterface;

/**
* @return UuidInterface
*/
public function getUuid(): UuidInterface;

/**
* @return DateTime
*/
public function getCreated(): DateTime;

/**
* @return DateTime|null
*/
public function getUpdated(): ?DateTime;

/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function updateTimestamps();

/**
* @return bool
*/
public function getIsDeleted(): bool;

/**
* @return void
*/
public function touch(): void;

/**
* @return array
*/
Expand Down
24 changes: 14 additions & 10 deletions src/User/src/Entity/UserResetPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

namespace Frontend\User\Entity;

use DateInterval;
use DateTime;
use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM;
use Exception;
use Frontend\App\Common\AbstractEntity;

/**
Expand All @@ -32,8 +35,8 @@ class UserResetPassword extends AbstractEntity
protected $user;

/**
* @ORM\Column(name="expires", type="datetime", nullable=false)
* @var DateTime
* @ORM\Column(name="expires", type="datetime_immutable", nullable=false)
* @var DateTimeImmutable
*/
protected $expires;

Expand All @@ -56,8 +59,9 @@ public function __construct()
{
parent::__construct();

$this->expires = new DateTime();
$this->expires->add(new \DateInterval('P1D'));
$tomorrow = new DateTime();
$tomorrow->add(new DateInterval('P1D'));
$this->expires = DateTimeImmutable::createFromMutable($tomorrow);
}

/**
Expand All @@ -80,18 +84,18 @@ public function setUser(User $user)
}

/**
* @return DateTime
* @return DateTimeImmutable
*/
public function getExpires(): DateTime
public function getExpires(): DateTimeImmutable
{
return $this->expires;
}

/**
* @param DateTime $expires
* @param DateTimeImmutable $expires
* @return $this
*/
public function setExpires(DateTime $expires)
public function setExpires(DateTimeImmutable $expires)
{
$this->expires = $expires;

Expand Down Expand Up @@ -154,8 +158,8 @@ public function isCompleted()
public function isValid(): bool
{
try {
return $this->getExpires() > (new DateTime());
} catch (\Exception $exception) {
return $this->getExpires() > (new DateTimeImmutable());
} catch (Exception $exception) {
}

return false;
Expand Down

0 comments on commit 28e2a45

Please sign in to comment.