src/Products/NotificationsBundle/Entity/Checkup.php line 22

Open in your IDE?
  1. <?php
  2. namespace Products\NotificationsBundle\Entity;
  3. use Cms\CoreBundle\Model\Interfaces\Loggable\LoggableInterface;
  4. use Cms\TenantBundle\Entity\TenantedEntity;
  5. use DateTime;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\Common\Collections\Criteria;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Platform\SecurityBundle\Entity\Identity\Account;
  11. /**
  12.  * @ORM\Entity(
  13.  *     repositoryClass = "Products\NotificationsBundle\Doctrine\Repository\CheckupRepository",
  14.  * )
  15.  * @ORM\Table(
  16.  *     name = "notis__checkup",
  17.  * )
  18.  */
  19. class Checkup extends TenantedEntity implements LoggableInterface
  20. {
  21.     /**
  22.      * @var Profile|null
  23.      *
  24.      * @ORM\ManyToOne(
  25.      *     targetEntity = "Products\NotificationsBundle\Entity\Profile",
  26.      *     inversedBy = "checkups",
  27.      * )
  28.      * @ORM\JoinColumn(
  29.      *     name = "profile",
  30.      *     referencedColumnName = "id",
  31.      *     nullable = false,
  32.      *     onDelete = "CASCADE",
  33.      * )
  34.      */
  35.     protected ?Profile $profile null;
  36.     /**
  37.      * @var bool
  38.      *
  39.      * @ORM\Column(
  40.      *     type = "boolean",
  41.      *     nullable = false,
  42.      *     options = {
  43.      *         "default" = false,
  44.      *     }
  45.      * )
  46.      */
  47.     protected bool $ok false;
  48.     /**
  49.      * @var DateTime|null
  50.      *
  51.      * @ORM\Column(
  52.      *     type = "datetime",
  53.      *     nullable = true,
  54.      * )
  55.      */
  56.     protected ?DateTime $fixedAt null;
  57.     /**
  58.      * @var Account|null
  59.      *
  60.      * @ORM\ManyToOne(
  61.      *     targetEntity = "Platform\SecurityBundle\Entity\Identity\Account",
  62.      * )
  63.      * @ORM\JoinColumn(
  64.      *     name = "fixedBy",
  65.      *     referencedColumnName = "id",
  66.      *     nullable = true,
  67.      *     onDelete = "SET NULL",
  68.      * )
  69.      */
  70.     protected ?Account $fixedBy null;
  71.     /**
  72.      * @var Collection|CheckupRelationship[]
  73.      *
  74.      * @ORM\OneToMany(
  75.      *     targetEntity = "Products\NotificationsBundle\Entity\CheckupRelationship",
  76.      *     mappedBy = "checkup",
  77.      * )
  78.      */
  79.     protected Collection $relationships;
  80.     /**
  81.      * @var string|null
  82.      *
  83.      * @ORM\Column(
  84.      *     type = "string",
  85.      *     nullable = true,
  86.      * )
  87.      */
  88.     protected ?string $firstName null;
  89.     /**
  90.      * @var string|null
  91.      *
  92.      * @ORM\Column(
  93.      *     type = "string",
  94.      *     nullable = true,
  95.      * )
  96.      */
  97.     protected ?string $lastName null;
  98.     /**
  99.      * @var string|null
  100.      *
  101.      * @ORM\Column(
  102.      *     type = "string",
  103.      *     nullable = true,
  104.      * )
  105.      */
  106.     protected ?string $language null;
  107.     /**
  108.      * @var string|null
  109.      *
  110.      * @ORM\Column(
  111.      *     type = "text",
  112.      *     nullable = true,
  113.      * )
  114.      */
  115.     protected ?string $note null;
  116.     /**
  117.      * @var string|null
  118.      *
  119.      * @ORM\Column(
  120.      *     type = "string",
  121.      *     nullable = true,
  122.      * )
  123.      */
  124.     protected ?string $notified null;
  125.     /**
  126.      *
  127.      */
  128.     public function __construct()
  129.     {
  130.         $this->relationships = new ArrayCollection();
  131.     }
  132.     /**
  133.      * @return Profile|null
  134.      */
  135.     public function getProfile(): ?Profile
  136.     {
  137.         return $this->profile;
  138.     }
  139.     /**
  140.      * @param Profile $profile
  141.      * @return $this
  142.      */
  143.     public function setProfile(Profile $profile): self
  144.     {
  145.         $this->profile $profile;
  146.         return $this;
  147.     }
  148.     /**
  149.      * @return bool
  150.      */
  151.     public function isFixed(): bool
  152.     {
  153.         return ! empty($this->getFixedAt());
  154.     }
  155.     /**
  156.      * @return DateTime|null
  157.      */
  158.     public function getFixedAt(): ?DateTime
  159.     {
  160.         return $this->fixedAt;
  161.     }
  162.     /**
  163.      * @param DateTime|null $fixedAt
  164.      * @return $this
  165.      */
  166.     public function setFixedAt(?DateTime $fixedAt): self
  167.     {
  168.         $this->fixedAt $fixedAt;
  169.         return $this;
  170.     }
  171.     /**
  172.      * @return Account|null
  173.      */
  174.     public function getFixedBy(): ?Account
  175.     {
  176.         return $this->fixedBy;
  177.     }
  178.     /**
  179.      * @param Account|null $fixedBy
  180.      * @return $this
  181.      */
  182.     public function setFixedBy(?Account $fixedBy): self
  183.     {
  184.         $this->fixedBy $fixedBy;
  185.         return $this;
  186.     }
  187.     /**
  188.      * @param Criteria|bool|null $criteria
  189.      * @return ArrayCollection|CheckupRelationship[]
  190.      */
  191.     public function getRelationships($criteria null): iterable
  192.     {
  193.         if ( ! $this->relationships instanceof Collection) {
  194.             $this->relationships = new ArrayCollection();
  195.         }
  196.         $criteria = ($criteria === true) ? new Criteria() : ($criteria ?: null);
  197.         if ( ! empty($criteria)) {
  198.             return $this->relationships->matching($criteria);
  199.         }
  200.         return $this->relationships;
  201.     }
  202.     /**
  203.      * @param ProfileRelationship $relationship
  204.      * @return CheckupRelationship|null
  205.      */
  206.     public function getRelationshipFor(ProfileRelationship $relationship): ?CheckupRelationship
  207.     {
  208.         if ($relationship->getProfile() !== $this->getProfile()) {
  209.             throw new \Exception();
  210.         }
  211.         return $this->getRelationships(
  212.             (new Criteria())
  213.                 ->andWhere(Criteria::expr()->eq('relationship'$relationship))
  214.         )->first() ?: null;
  215.     }
  216.     /**
  217.      * @return string|null
  218.      */
  219.     public function getFirstName(): ?string
  220.     {
  221.         return $this->firstName;
  222.     }
  223.     /**
  224.      * @param string|null $firstName
  225.      * @return $this
  226.      */
  227.     public function setFirstName(?string $firstName): self
  228.     {
  229.         $this->firstName $firstName;
  230.         return $this;
  231.     }
  232.     /**
  233.      * @return string|null
  234.      */
  235.     public function getLastName(): ?string
  236.     {
  237.         return $this->lastName;
  238.     }
  239.     /**
  240.      * @param string|null $lastName
  241.      * @return $this
  242.      */
  243.     public function setLastName(?string $lastName): self
  244.     {
  245.         $this->lastName $lastName;
  246.         return $this;
  247.     }
  248.     /**
  249.      * @return string|null
  250.      */
  251.     public function getLanguage(): ?string
  252.     {
  253.         return $this->language;
  254.     }
  255.     /**
  256.      * @param string|null $language
  257.      * @return $this
  258.      */
  259.     public function setLanguage(?string $language): self
  260.     {
  261.         $this->language $language;
  262.         return $this;
  263.     }
  264.     /**
  265.      * @return string|null
  266.      */
  267.     public function getNote(): ?string
  268.     {
  269.         return $this->note;
  270.     }
  271.     /**
  272.      * @param string|null $note
  273.      * @return $this
  274.      */
  275.     public function setNote(?string $note): self
  276.     {
  277.         $this->note $note;
  278.         return $this;
  279.     }
  280.     /**
  281.      * @return bool
  282.      */
  283.     public function isOk(): bool
  284.     {
  285.         return $this->ok;
  286.     }
  287.     /**
  288.      * @param bool $ok
  289.      * @return $this
  290.      */
  291.     public function setOk(bool $ok): self
  292.     {
  293.         $this->ok $ok;
  294.         return $this;
  295.     }
  296.     /**
  297.      * @return bool
  298.      */
  299.     public function isLocked(): bool
  300.     {
  301.         return ( ! $this->isOk());
  302.     }
  303.     /**
  304.      * @return bool
  305.      */
  306.     public function wasNotified(): bool
  307.     {
  308.         return (bool) $this->notified;
  309.     }
  310.     /**
  311.      * @return string|null
  312.      */
  313.     public function getNotified(): ?string
  314.     {
  315.         return $this->notified ?: null;
  316.     }
  317.     /**
  318.      * @param string|null $notified
  319.      * @return $this
  320.      */
  321.     public function setNotified(?string $notified): self
  322.     {
  323.         $this->notified $notified ?: null;
  324.         return $this;
  325.     }
  326.     /**
  327.      * @return array|array[]
  328.      */
  329.     public function diffs(): array
  330.     {
  331.         $diffs = [];
  332.         if (($new $this->getFirstName()) && $new !== ($old $this->getProfile()->getFirstName())) {
  333.             $diffs[] = [
  334.                 'icon' => null,
  335.                 'label' => 'First Name',
  336.                 'old' => $old,
  337.                 'new' => $new,
  338.             ];
  339.         }
  340.         if (($new $this->getLastName()) && $new !== ($old $this->getProfile()->getLastName())) {
  341.             $diffs[] = [
  342.                 'icon' => null,
  343.                 'label' => 'Last Name',
  344.                 'old' => $old,
  345.                 'new' => $new,
  346.             ];
  347.         }
  348.         foreach ($this->getRelationships() as $relationship) {
  349.             $diffs[] = [
  350.                 'icon' => null,
  351.                 'label' => 'Student',
  352.                 'old' => $relationship->getRelationship()->getStudent()->getFullName(),
  353.                 'new' => 'Incorrect student',
  354.             ];
  355.         }
  356.         return $diffs;
  357.     }
  358.     /**
  359.      * {@inheritDoc}
  360.      */
  361.     public function getLoggableDetails(): array
  362.     {
  363.         return [
  364.             'id' => (string) $this->getId(),
  365.             'title' => $this->getProfile() ? $this->getProfile()->getCensoredName() : null,
  366.         ];
  367.     }
  368. }