Files
myep/src/Entity/Groups/ContingentSyncState.php
T

169 lines
4.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Entity\Groups;
use App\Repository\Groups\ContingentSyncStateRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
/**
* Bookkeeping for the scheduled contingent sync of a single hotel code.
*
* `changedAt` records when the sync last saw a day appear, vanish or switch status within the
* horizon it had already reached; `horizonTo` is how far that reach extends.
*/
#[ORM\Entity(repositoryClass: ContingentSyncStateRepository::class)]
#[ORM\Table(name: 'contingent_sync_state')]
class ContingentSyncState
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 16, unique: true)]
private ?string $hotelCode = null;
/**
* Superseded by $hotelCode, see {@see ContingentDay::$accommodation}.
*/
#[ORM\OneToOne]
#[ORM\JoinColumn(nullable: true, unique: true, onDelete: 'CASCADE')]
private ?Accommodation $accommodation = null;
/**
* Last time the stored snapshot actually changed. Kept for operators, not exposed via the API.
*/
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: true)]
private ?\DateTimeImmutable $changedAt = null;
/**
* Last successful sync, regardless of whether anything changed.
*/
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: true)]
private ?\DateTimeImmutable $syncedAt = null;
#[ORM\Column(type: Types::DATE_IMMUTABLE, nullable: true)]
private ?\DateTimeImmutable $horizonTo = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $lastError = null;
#[ORM\Column]
private int $failureCount = 0;
public function __construct(?string $hotelCode = null)
{
$this->hotelCode = $hotelCode;
}
public function getId(): ?int
{
return $this->id;
}
public function getHotelCode(): ?string
{
return $this->hotelCode;
}
public function getAccommodation(): ?Accommodation
{
return $this->accommodation;
}
public function setAccommodation(?Accommodation $accommodation): self
{
$this->accommodation = $accommodation;
return $this;
}
public function getChangedAt(): ?\DateTimeImmutable
{
return $this->changedAt;
}
public function setChangedAt(?\DateTimeImmutable $changedAt): self
{
$this->changedAt = $changedAt;
return $this;
}
public function getSyncedAt(): ?\DateTimeImmutable
{
return $this->syncedAt;
}
public function setSyncedAt(?\DateTimeImmutable $syncedAt): self
{
$this->syncedAt = $syncedAt;
return $this;
}
public function getHorizonTo(): ?\DateTimeImmutable
{
return $this->horizonTo;
}
public function setHorizonTo(?\DateTimeImmutable $horizonTo): self
{
$this->horizonTo = $horizonTo;
return $this;
}
public function getLastError(): ?string
{
return $this->lastError;
}
public function setLastError(?string $lastError): self
{
$this->lastError = $lastError;
return $this;
}
public function getFailureCount(): int
{
return $this->failureCount;
}
public function setFailureCount(int $failureCount): self
{
$this->failureCount = $failureCount;
return $this;
}
public function recordFailure(string $error): self
{
$this->lastError = $error;
++$this->failureCount;
return $this;
}
/**
* Records a successful sync of a window ending at $horizonTo.
*
* The horizon only ever grows, so an ad-hoc run over a narrow window — say
* `--horizon-months=1` while debugging — cannot discard the reach the scheduled full sync
* established.
*/
public function recordSuccess(\DateTimeImmutable $syncedAt, \DateTimeImmutable $horizonTo): self
{
$this->syncedAt = $syncedAt;
$this->horizonTo = null === $this->horizonTo || $horizonTo > $this->horizonTo ? $horizonTo : $this->horizonTo;
$this->lastError = null;
$this->failureCount = 0;
return $this;
}
}