8000 Feat - Introduce `readonly` properties by tarlepp · Pull Request #1820 · tarlepp/symfony-flex-backend · GitHub
[go: up one dir, main page]

Skip to content

Feat - Introduce readonly properties #1820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
24 changes: 12 additions & 12 deletions src/Entity/DateDimension.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class DateDimension implements EntityInterface
'DateDimension',
'DateDimension.id',
])]
private UuidInterface $id;
private readonly UuidInterface $id;

#[ORM\Column(
name: 'year',
Expand All @@ -71,7 +71,7 @@ class DateDimension implements EntityInterface
'DateDimension',
'DateDimension.year',
])]
private int $year;
private readonly int $year;

#[ORM\Column(
name: 'month',
Expand All @@ -84,7 +84,7 @@ class DateDimension implements EntityInterface
'DateDimension',
'DateDimension.month',
])]
private int $month;
private readonly int $month;

#[ORM\Column(
name: 'day',
Expand All @@ -97,7 +97,7 @@ class DateDimension implements EntityInterface
'DateDimension',
'DateDimension.day',
])]
private int $day;
private readonly int $day;

#[ORM\Column(
name: 'quarter',
Expand All @@ -110,7 +110,7 @@ class DateDimension implements EntityInterface
'DateDimension',
'DateDimension.quarter',
])]
private int $quarter;
private readonly int $quarter;

#[ORM\Column(
name: 'week_number',
Expand All @@ -123,7 +123,7 @@ class DateDimension implements EntityInterface
'DateDimension',
'DateDimension.weekNumber',
])]
private int $weekNumber;
private readonly int $weekNumber;

#[ORM\Column(
name: 'day_number_of_week',
Expand All @@ -136,7 +136,7 @@ class DateDimension implements EntityInterface
'DateDimension',
'DateDimension.dayNumber',
])]
private int $dayNumberOfWeek;
private readonly int $dayNumberOfWeek;

#[ORM\Column(
name: 'day_number_of_year',
Expand All @@ -149,7 +149,7 @@ class DateDimension implements EntityInterface
'DateDimension',
'DateDimension.dayNumberOfYear',
])]
private int $dayNumberOfYear;
private readonly int $dayNumberOfYear;

#[ORM\Column(
name: 'leap_year',
Expand All @@ -162,7 +162,7 @@ class DateDimension implements EntityInterface
'DateDimension',
'DateDimension.leapYear',
])]
private bool $leapYear;
private readonly bool $leapYear;

#[ORM\Column(
name: 'week_numbering_year',
Expand All @@ -175,7 +175,7 @@ class DateDimension implements EntityInterface
'DateDimension',
'DateDimension.weekNumberingYear',
])]
private int $weekNumberingYear;
private readonly int $weekNumberingYear;

#[ORM\Column(
name: 'unix_time',
Expand All @@ -188,7 +188,7 @@ class DateDimension implements EntityInterface
'DateDimension',
'DateDimension.unixTime',
])]
private int $unixTime;
private readonly int $unixTime;

public function __construct(
#[ORM\Column(
Expand All @@ -199,7 +199,7 @@ public function __construct(
'DateDimension',
'DateDimension.date',
])]
private DateTimeImmutable $date
private readonly DateTimeImmutable $date
) {
$this->id = $this->createUuid();

Expand Down
31 changes: 11 additions & 20 deletions src/Entity/Healthz.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,23 @@ class Healthz implements EntityInterface
])]
private UuidInterface $id;

#[ORM\Column(
name: 'timestamp',
type: Types::DATETIME_IMMUTABLE,
)]
#[Groups([
'Healthz',
'Healthz.timestamp',
])]
private DateTimeImmutable $timestamp;

/**
* Healthz constructor.
*
* @throws Throwable
*/
public function __construct()
{
public function __construct(
#[ORM\Column(
name: 'timestamp',
type: Types::DATETIME_IMMUTABLE,
)]
#[Groups([
'Healthz',
'Healthz.timestamp',
])]
private readonly DateTimeImmutable $timestamp = new DateTimeImmutable(timezone: new DateTimeZone('UTC')),
) {
$this->id = $this->createUuid();
$this->timestamp = new DateTimeImmutable(timezone: new DateTimeZone('UTC'));
}

public function getId(): string
Expand All @@ -81,13 +79,6 @@ public function getTimestamp(): DateTimeImmutable
return $this->getCreatedAt();
}

public function setTimestamp(DateTimeImmutable $timestamp): self
{
$this->timestamp = $timestamp;

return $this;
}

public function getCreatedAt(): DateTimeImmutable
{
return $this->timestamp;
Expand Down
57 changes: 25 additions & 32 deletions src/Entity/Traits/LogEntityTrait.php → src/Entity/LogEntity.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<?php
declare(strict_types = 1);
/**
* /src/Entity/Traits/LogEntityTrait.php
* /src/Entity/LogEntity.php
*
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
*/

namespace App\Entity\Traits;
namespace App\Entity;

use App\Entity\User;
use DateTimeImmutable;
use DateTimeZone;
use Doctrine\DBAL\Types\Types;
Expand All @@ -18,14 +17,13 @@
use Throwable;

/**
* Trait LogEntityTrait
* Class LogEntity
*
* @package App\Entity\Traits
* @package App\Entity
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
*
* @property User|null $user
*/
trait LogEntityTrait
#[ORM\MappedSuperclass]
abstract class LogEntity
{
#[ORM\Column(
name: 'time',
Expand All @@ -38,7 +36,7 @@ trait LogEntityTrait
'LogRequest',
'LogRequest.time',
])]
protected DateTimeImmutable $time;
protected readonly DateTimeImmutable $time;

#[ORM\Column(
name: '`date`',
Expand All @@ -51,7 +49,7 @@ trait LogEntityTrait
'LogRequest',
'LogRequest.date',
])]
protected DateTimeImmutable $date;
protected readonly DateTimeImmutable $date;

#[ORM\Column(
name: 'agent',
Expand All @@ -64,7 +62,7 @@ trait LogEntityTrait
'LogRequest',
'LogRequest.agent',
])]
protected string $agent = '';
protected readonly string $agent;

#[ORM\Column(
name: 'http_host',
Expand All @@ -78,7 +76,7 @@ trait LogEntityTrait
'LogRequest',
'LogRequest.httpHost',
])]
protected string $httpHost = '';
protected readonly string $httpHost;

#[ORM\Column(
name: 'client_ip',
Expand All @@ -92,7 +90,21 @@ trait LogEntityTrait
'LogRequest',
'LogRequest.clientIp',
])]
private string $clientIp = '';
protected readonly string $clientIp;

/**
* @throws Throwable
*/
public function __construct(?Request $request)
{
$now = new DateTimeImmutable(timezone: new DateTimeZone('UTC'));

$this->time = $now;
$this->date = $now;
$this->clientIp = $request?->getClientIp() ?? '';
$this->httpHost = $request?->getHttpHost() ?? '';
$this->agent = $request?->headers->get('User-Agent') ?? '';
}

public function getTime(): DateTimeImmutable
{
Expand Down Expand Up @@ -123,23 +135,4 @@ public function getCreatedAt(): ?DateTimeImmutable
{
return $this->getDate();
}

private function processRequestData(Request $request): void
{
$this->clientIp = $request->getClientIp() ?? '';
$this->httpHost = $request->getHttpHost();
$this->agent = $request->headers->get('User-Agent') ?? '';
}

/**
* @throws Throwable
*/
#[ORM\PrePersist]
private function processTimeAndDate(): void
{
$now = new DateTimeImmutable(timezone: new DateTimeZone('UTC'));

$this->time = $now;
$this->date = $now;
}
}
Loading
0