<?php
namespace Products\NotificationsBundle\Entity;
use Cms\CoreBundle\Model\Interfaces\Loggable\LoggableInterface;
use Cms\TenantBundle\Entity\TenantedEntity;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping as ORM;
use Platform\SecurityBundle\Entity\Identity\Account;
/**
* @ORM\Entity(
* repositoryClass = "Products\NotificationsBundle\Doctrine\Repository\CheckupRepository",
* )
* @ORM\Table(
* name = "notis__checkup",
* )
*/
class Checkup extends TenantedEntity implements LoggableInterface
{
/**
* @var Profile|null
*
* @ORM\ManyToOne(
* targetEntity = "Products\NotificationsBundle\Entity\Profile",
* inversedBy = "checkups",
* )
* @ORM\JoinColumn(
* name = "profile",
* referencedColumnName = "id",
* nullable = false,
* onDelete = "CASCADE",
* )
*/
protected ?Profile $profile = null;
/**
* @var bool
*
* @ORM\Column(
* type = "boolean",
* nullable = false,
* options = {
* "default" = false,
* }
* )
*/
protected bool $ok = false;
/**
* @var DateTime|null
*
* @ORM\Column(
* type = "datetime",
* nullable = true,
* )
*/
protected ?DateTime $fixedAt = null;
/**
* @var Account|null
*
* @ORM\ManyToOne(
* targetEntity = "Platform\SecurityBundle\Entity\Identity\Account",
* )
* @ORM\JoinColumn(
* name = "fixedBy",
* referencedColumnName = "id",
* nullable = true,
* onDelete = "SET NULL",
* )
*/
protected ?Account $fixedBy = null;
/**
* @var Collection|CheckupRelationship[]
*
* @ORM\OneToMany(
* targetEntity = "Products\NotificationsBundle\Entity\CheckupRelationship",
* mappedBy = "checkup",
* )
*/
protected Collection $relationships;
/**
* @var string|null
*
* @ORM\Column(
* type = "string",
* nullable = true,
* )
*/
protected ?string $firstName = null;
/**
* @var string|null
*
* @ORM\Column(
* type = "string",
* nullable = true,
* )
*/
protected ?string $lastName = null;
/**
* @var string|null
*
* @ORM\Column(
* type = "string",
* nullable = true,
* )
*/
protected ?string $language = null;
/**
* @var string|null
*
* @ORM\Column(
* type = "text",
* nullable = true,
* )
*/
protected ?string $note = null;
/**
* @var string|null
*
* @ORM\Column(
* type = "string",
* nullable = true,
* )
*/
protected ?string $notified = null;
/**
*
*/
public function __construct()
{
$this->relationships = new ArrayCollection();
}
/**
* @return Profile|null
*/
public function getProfile(): ?Profile
{
return $this->profile;
}
/**
* @param Profile $profile
* @return $this
*/
public function setProfile(Profile $profile): self
{
$this->profile = $profile;
return $this;
}
/**
* @return bool
*/
public function isFixed(): bool
{
return ! empty($this->getFixedAt());
}
/**
* @return DateTime|null
*/
public function getFixedAt(): ?DateTime
{
return $this->fixedAt;
}
/**
* @param DateTime|null $fixedAt
* @return $this
*/
public function setFixedAt(?DateTime $fixedAt): self
{
$this->fixedAt = $fixedAt;
return $this;
}
/**
* @return Account|null
*/
public function getFixedBy(): ?Account
{
return $this->fixedBy;
}
/**
* @param Account|null $fixedBy
* @return $this
*/
public function setFixedBy(?Account $fixedBy): self
{
$this->fixedBy = $fixedBy;
return $this;
}
/**
* @param Criteria|bool|null $criteria
* @return ArrayCollection|CheckupRelationship[]
*/
public function getRelationships($criteria = null): iterable
{
if ( ! $this->relationships instanceof Collection) {
$this->relationships = new ArrayCollection();
}
$criteria = ($criteria === true) ? new Criteria() : ($criteria ?: null);
if ( ! empty($criteria)) {
return $this->relationships->matching($criteria);
}
return $this->relationships;
}
/**
* @param ProfileRelationship $relationship
* @return CheckupRelationship|null
*/
public function getRelationshipFor(ProfileRelationship $relationship): ?CheckupRelationship
{
if ($relationship->getProfile() !== $this->getProfile()) {
throw new \Exception();
}
return $this->getRelationships(
(new Criteria())
->andWhere(Criteria::expr()->eq('relationship', $relationship))
)->first() ?: null;
}
/**
* @return string|null
*/
public function getFirstName(): ?string
{
return $this->firstName;
}
/**
* @param string|null $firstName
* @return $this
*/
public function setFirstName(?string $firstName): self
{
$this->firstName = $firstName;
return $this;
}
/**
* @return string|null
*/
public function getLastName(): ?string
{
return $this->lastName;
}
/**
* @param string|null $lastName
* @return $this
*/
public function setLastName(?string $lastName): self
{
$this->lastName = $lastName;
return $this;
}
/**
* @return string|null
*/
public function getLanguage(): ?string
{
return $this->language;
}
/**
* @param string|null $language
* @return $this
*/
public function setLanguage(?string $language): self
{
$this->language = $language;
return $this;
}
/**
* @return string|null
*/
public function getNote(): ?string
{
return $this->note;
}
/**
* @param string|null $note
* @return $this
*/
public function setNote(?string $note): self
{
$this->note = $note;
return $this;
}
/**
* @return bool
*/
public function isOk(): bool
{
return $this->ok;
}
/**
* @param bool $ok
* @return $this
*/
public function setOk(bool $ok): self
{
$this->ok = $ok;
return $this;
}
/**
* @return bool
*/
public function isLocked(): bool
{
return ( ! $this->isOk());
}
/**
* @return bool
*/
public function wasNotified(): bool
{
return (bool) $this->notified;
}
/**
* @return string|null
*/
public function getNotified(): ?string
{
return $this->notified ?: null;
}
/**
* @param string|null $notified
* @return $this
*/
public function setNotified(?string $notified): self
{
$this->notified = $notified ?: null;
return $this;
}
/**
* @return array|array[]
*/
public function diffs(): array
{
$diffs = [];
if (($new = $this->getFirstName()) && $new !== ($old = $this->getProfile()->getFirstName())) {
$diffs[] = [
'icon' => null,
'label' => 'First Name',
'old' => $old,
'new' => $new,
];
}
if (($new = $this->getLastName()) && $new !== ($old = $this->getProfile()->getLastName())) {
$diffs[] = [
'icon' => null,
'label' => 'Last Name',
'old' => $old,
'new' => $new,
];
}
foreach ($this->getRelationships() as $relationship) {
$diffs[] = [
'icon' => null,
'label' => 'Student',
'old' => $relationship->getRelationship()->getStudent()->getFullName(),
'new' => 'Incorrect student',
];
}
return $diffs;
}
/**
* {@inheritDoc}
*/
public function getLoggableDetails(): array
{
return [
'id' => (string) $this->getId(),
'title' => $this->getProfile() ? $this->getProfile()->getCensoredName() : null,
];
}
}