src/Entity/System/Notification.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Entity\System;
  3. use App\Repository\System\NotificationRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. /**
  8.  * @ORM\Entity(repositoryClass=NotificationRepository::class)
  9.  *
  10.  * @ORM\Table(name="notification")
  11.  */
  12. class Notification
  13. {
  14.     public const ID_WALLET_LOW_BALANCE 1;
  15.     public const ID_PACK_EXPIRED 2;
  16.     public const ID_PACK_ABOUT_TO_EXPIRE 3;
  17.     public const ID_SHOP_ENTER_DATA 4;
  18.     public const ID_SHOP_WRONG_DATA 5;
  19.     public const ID_RETURN_PENDING_ADDRESS 6;
  20.     public const ID_RETURN_PENDING_REVISION 7;
  21.     public const ID_ORDER_PENDING_INVOICE 8;
  22.     public const ID_ORDER_PENDING_BANKWIRE 9;
  23.     public const ID_RETURN_NEW_COMMENT 10;
  24.     public const ID_RETURN_DOWNLOAD_TAG 11;
  25.     public const ID_ADD_PAYMENT_METHOD 12;
  26.     public const ID_SHOP_FINISHED 13;
  27.     public const ID_SUSPENDED_SHOP 14;
  28.     public const ID_RETURN_PAID 15;
  29.     public const ID_RETURN_APPROVED_FOR_PAYMENT 16;
  30.     /**
  31.      * @var int
  32.      *
  33.      * @ORM\Id
  34.      *
  35.      * @ORM\GeneratedValue(strategy="AUTO")
  36.      *
  37.      * @ORM\Column(type="integer")
  38.      */
  39.     private $id;
  40.     /**
  41.      * @var \DateTime|null
  42.      *
  43.      * @ORM\Column(type="datetime", nullable=true)
  44.      */
  45.     private $expirationDate;
  46.     /**
  47.      * @var \DateTime
  48.      *
  49.      * @ORM\Column(type="datetime", nullable=false)
  50.      */
  51.     private $creationDate;
  52.     /**
  53.      * @var Employee
  54.      *
  55.      * @ORM\ManyToOne(targetEntity="App\Entity\System\Employee")
  56.      *
  57.      * @ORM\JoinColumn(name="creation_employee_id", referencedColumnName="id_employee", nullable=false)
  58.      */
  59.     private $creationEmployee;
  60.     /**
  61.      * @var \DateTime|null
  62.      *
  63.      * @ORM\Column(type="datetime", nullable=true)
  64.      */
  65.     private $notificationDate;
  66.     /**
  67.      * @var Employee|null
  68.      *
  69.      * @ORM\ManyToOne(targetEntity="App\Entity\System\Employee")
  70.      *
  71.      * @ORM\JoinColumn(name="notification_employee_id", referencedColumnName="id_employee")
  72.      */
  73.     private $notificationEmployee;
  74.     /**
  75.      * @var Employee|null
  76.      *
  77.      * @ORM\ManyToOne(targetEntity="App\Entity\System\Employee")
  78.      *
  79.      * @ORM\JoinColumn(name="deactivation_employee_id", referencedColumnName="id_employee")
  80.      */
  81.     private $deactivationEmployee;
  82.     /**
  83.      * @var \DateTime|null
  84.      *
  85.      * @ORM\Column(type="datetime", nullable=true)
  86.      */
  87.     private $deactivationDate;
  88.     /**
  89.      * @var NotificationType
  90.      *
  91.      * @ORM\ManyToOne(targetEntity="NotificationType")
  92.      *
  93.      * @ORM\JoinColumn(name="type_id", referencedColumnName="id", nullable=false)
  94.      */
  95.     private $type;
  96.     /**
  97.      * @var NotificationProduct
  98.      *
  99.      * @ORM\ManyToOne(targetEntity="NotificationProduct")
  100.      *
  101.      * @ORM\JoinColumn(name="notification_product_id", referencedColumnName="id", nullable=false)
  102.      */
  103.     private $notificationProduct;
  104.     /**
  105.      * @var NotificationSection|null
  106.      *
  107.      * @ORM\ManyToOne(targetEntity="NotificationSection")
  108.      *
  109.      * @ORM\JoinColumn(name="section_id", referencedColumnName="id", nullable=true)
  110.      */
  111.     private $section;
  112.     /**
  113.      * @var bool
  114.      *
  115.      * @ORM\Column(type="boolean")
  116.      */
  117.     private $active;
  118.     /**
  119.      * @var NotificationLang[]|Collection
  120.      *
  121.      * @ORM\OneToMany(targetEntity="NotificationLang", mappedBy="notification", cascade={"persist"})
  122.      */
  123.     private $translations;
  124.     /**
  125.      * @var NotificationHistory[]|Collection
  126.      *
  127.      * @ORM\OneToMany(targetEntity="NotificationHistory", mappedBy="notification", cascade={"persist"})
  128.      */
  129.     private $history;
  130.     /**
  131.      * @param Employee $employee
  132.      * @param \DateTime $creationDate
  133.      * @param \DateTime|null $expirationDate
  134.      * @param NotificationType $type
  135.      * @param NotificationProduct $product
  136.      *
  137.      * @return self
  138.      */
  139.     public static function createFromValues(Employee $employee, \DateTime $creationDate, ?\DateTime $expirationDateNotificationType $typeNotificationProduct $product, ?NotificationSection $section): self
  140.     {
  141.         $notification = new self();
  142.         $notification->creationEmployee $employee;
  143.         $notification->creationDate $creationDate;
  144.         $notification->active true;
  145.         $notification->expirationDate $expirationDate;
  146.         $notification->type $type;
  147.         $notification->notificationProduct $product;
  148.         $notification->section $section;
  149.         $notification->translations = new ArrayCollection();
  150.         $notification->history = new ArrayCollection();
  151.         return $notification;
  152.     }
  153.     public function notify(Employee $employee, \DateTime $currentDateTime): bool
  154.     {
  155.         if (null === $this->notificationDate) {
  156.             $this->notificationDate $currentDateTime;
  157.             $this->notificationEmployee $employee;
  158.             return true;
  159.         }
  160.         return false;
  161.     }
  162.     /**
  163.      * @return int
  164.      */
  165.     public function getId(): int
  166.     {
  167.         return $this->id;
  168.     }
  169.     /**
  170.      * @return \DateTime|null
  171.      */
  172.     public function getExpirationDate(): ?\DateTime
  173.     {
  174.         return $this->expirationDate;
  175.     }
  176.     /**
  177.      * @return \DateTime
  178.      */
  179.     public function getCreationDate(): \DateTime
  180.     {
  181.         return $this->creationDate;
  182.     }
  183.     /**
  184.      * @return Employee
  185.      */
  186.     public function getCreationEmployee(): Employee
  187.     {
  188.         return $this->creationEmployee;
  189.     }
  190.     /**
  191.      * @return \DateTime|null
  192.      */
  193.     public function getNotificationDate(): ?\DateTime
  194.     {
  195.         return $this->notificationDate;
  196.     }
  197.     /**
  198.      * @return Employee|null
  199.      */
  200.     public function getNotificationEmployee(): ?Employee
  201.     {
  202.         return $this->notificationEmployee;
  203.     }
  204.     /**
  205.      * @return Employee|null
  206.      */
  207.     public function getDeactivationEmployee(): ?Employee
  208.     {
  209.         return $this->deactivationEmployee;
  210.     }
  211.     /**
  212.      * @return \DateTime|null
  213.      */
  214.     public function getDeactivationDate(): ?\DateTime
  215.     {
  216.         return $this->deactivationDate;
  217.     }
  218.     /**
  219.      * @return NotificationType
  220.      */
  221.     public function getType(): NotificationType
  222.     {
  223.         return $this->type;
  224.     }
  225.     /**
  226.      * @return NotificationProduct
  227.      */
  228.     public function getNotificationProduct(): NotificationProduct
  229.     {
  230.         return $this->notificationProduct;
  231.     }
  232.     /**
  233.      * @return NotificationSection|null
  234.      */
  235.     public function getSection(): ?NotificationSection
  236.     {
  237.         return $this->section;
  238.     }
  239.     /**
  240.      * @return bool
  241.      */
  242.     public function isActive(): bool
  243.     {
  244.         return $this->active;
  245.     }
  246.     /**
  247.      * @return NotificationLang[]|Collection
  248.      */
  249.     public function getTranslations()
  250.     {
  251.         return $this->translations;
  252.     }
  253.     /**
  254.      * @param \DateTime $deactivationDate
  255.      * @param Employee $employee
  256.      *
  257.      * @return void
  258.      */
  259.     public function deactivate(\DateTime $deactivationDateEmployee $employee): void
  260.     {
  261.         $this->deactivationDate $deactivationDate;
  262.         $this->deactivationEmployee $employee;
  263.         $this->active false;
  264.     }
  265.     public function addTranslation(NotificationLang $notificationLang): void
  266.     {
  267.         $this->translations->add($notificationLang);
  268.     }
  269.     /**
  270.      * @param \DateTime|null $expirationDate
  271.      */
  272.     public function setExpirationDate(?\DateTime $expirationDate): void
  273.     {
  274.         $this->expirationDate $expirationDate;
  275.     }
  276.     /**
  277.      * @param Language $language
  278.      * @param Employee $employee
  279.      * @param \DateTime $currentDateTime
  280.      * @param string|null $title
  281.      * @param string|null $description
  282.      * @param string|null $link
  283.      * @param string|null $linkText
  284.      *
  285.      * @return void
  286.      */
  287.     public function updateTranslation(Language $languageEmployee $employee, \DateTime $currentDateTime, ?string $title, ?string $description, ?string $link, ?string $linkText): void
  288.     {
  289.         /** @var NotificationLang $notificationLang */
  290.         $notificationLang $this->translations->filter(function (NotificationLang $notificationLang) use ($language) {
  291.             return $notificationLang->getLanguage()->getId() === $language->getId();
  292.         })->first();
  293.         if ($notificationLang) {
  294.             $this->addHistoryIfChangesDetected($notificationLang$employee$currentDateTime$title$description);
  295.             $notificationLang->updateFromValues($title$description$link$linkText);
  296.         }
  297.     }
  298.     private function addHistoryIfChangesDetected(NotificationLang $notificationLangEmployee $employee, \DateTime $currentDateTime, ?string $title, ?string $description): void
  299.     {
  300.         $historyItem NotificationHistory::createFromNotificationFormTranslation(
  301.             $this,
  302.             $notificationLang,
  303.             $employee,
  304.             $currentDateTime,
  305.             $title,
  306.             $description
  307.         );
  308.         if ($historyItem) {
  309.             $this->history->add($historyItem);
  310.         }
  311.     }
  312. }