src/Entity/Notification.php line 73

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiFilter;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\BooleanFilter;
  6. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  7. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  8. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter;
  9. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\ExistsFilter;
  10. use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
  11. use App\Repository\NotificationRepository;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. use Doctrine\Common\Collections\Collection;
  14. use Doctrine\ORM\Mapping as ORM;
  15. use Gedmo\Mapping\Annotation as Gedmo;
  16. use Symfony\Component\Serializer\Annotation\Groups;
  17. use Symfony\Component\Validator\Constraints as Assert;
  18. /**
  19.  * @ORM\Entity(repositoryClass=NotificationRepository::class)
  20.  * @ApiResource(
  21.  *     normalizationContext={"groups"={"notification:read"}},
  22.  *     denormalizationContext={"groups"={"notification:write"}},
  23.  *     itemOperations={
  24.  *         "get"={
  25.  *              "security"="is_granted('ROLE_ADMIN') or is_granted('ROLE_COMPANY')",
  26.  *              "security_message"="You are not allowed to access this ressource"
  27.  *          },
  28.  *          "patch"={
  29.  *              "security"="is_granted('ROLE_ADMIN') or is_granted('ROLE_COMPANY')"
  30.  *          },
  31.  *          "delete"={
  32.  *              "security"="is_granted('ROLE_ADMIN') or is_granted('ROLE_COMPANY')"
  33.  *          }
  34.  *      },
  35.  *      collectionOperations={
  36.  *         "get"={
  37.  *              "security"="is_granted('ROLE_ADMIN') or is_granted('ROLE_COMPANY')"
  38.  *          },
  39.  *         "post"={
  40.  *              "security"="is_granted('ROLE_ADMIN') or is_granted('ROLE_COMPANY')"
  41.  *          },
  42.  *          "get_last_unread_by_user"={
  43.  *             "description"="Get last notifications by User (for App).",
  44.  *             "path"="/users/{id}/last-unread-notifications",
  45.  *             "method"="GET",
  46.  *             "controller"="App\Controller\Api\NotificationController::getLastUnreadNotificationsByUser",
  47.  *             "security"="is_granted('ROLE_ADMIN') or is_granted('ROLE_CLIENT')"
  48.  *          }
  49.  *      }
  50.  * )
  51.  * @ApiFilter(OrderFilter::class, 
  52.  *      properties={
  53.  *          "id", "title", "message", "planFor", "sendAt", "status", "template", "active", 
  54.  *          "createdAt", "updatedAt", "type.name"
  55.  *      }
  56.  * )
  57.  * @ApiFilter(SearchFilter::class, properties={
  58.  *      "type.name": "partial", "title": "partial", "message": "partial", "status"
  59.  * })
  60.  * @ApiFilter(BooleanFilter::class, properties={"active", "template"})
  61.  * @ApiFilter(DateFilter::class, properties={"planFor", "sendAt", "createdAt"})
  62.  * @ApiFilter(ExistsFilter::class, properties={"title", "message", "link", "planFor", "sendAt"})
  63.  * @ApiFilter(PropertyFilter::class, 
  64.  *      arguments={
  65.  *          "parameterName"="fields", 
  66.  *          "overrideDefaultProperties"=true
  67.  *     }
  68.  * )
  69.  */
  70. class Notification
  71. {
  72.     const STATUS_PENDING "pending";
  73.     const STATUS_SUCCESS "success";
  74.     const STATUS_FAILURE "failure";
  75.     const STATUS_CANCELED "canceled";
  76.     const STATUS_SENT "sent";
  77.     const STATUS_SCHEDULED "scheduled";
  78.     const STATUSES = [
  79.         self::STATUS_PENDING,
  80.         self::STATUS_SUCCESS,
  81.         self::STATUS_FAILURE,
  82.         self::STATUS_CANCELED,
  83.         self::STATUS_SENT,
  84.         self::STATUS_SCHEDULED,
  85.     ];
  86.     /**
  87.      * @ORM\Id
  88.      * @ORM\GeneratedValue
  89.      * @ORM\Column(type="integer")
  90.      * @Groups({"notification:read", "notification:read:id"})
  91.      */
  92.     private $id;
  93.     /**
  94.      * @ORM\ManyToOne(targetEntity=NotificationType::class, inversedBy="notifications")
  95.      * @Groups({"notification:read", "notification:write", "notification:read:type"})
  96.      */
  97.     private $type;
  98.     /**
  99.      * @ORM\Column(type="string", length=255, nullable=true)
  100.      * @Groups({"notification:read", "notification:write", "notification:read:title"})
  101.      */
  102.     private $title;
  103.     /**
  104.      * @ORM\Column(type="text", nullable=true)
  105.      * @Groups({"notification:read", "notification:write", "notification:read:message"})
  106.      */
  107.     private $message;
  108.     /**
  109.      * @ORM\Column(type="json", nullable=true)
  110.      * @Groups({"notification:read", "notification:write", "notification:read:body"})
  111.      */
  112.     private $body = [];
  113.     /**
  114.      * @ORM\Column(type="datetime", nullable=true)
  115.      * @Groups({"notification:read", "notification:write", "notification:read:planFor"})
  116.      */
  117.     private $planFor;
  118.     /**
  119.      * @ORM\Column(type="datetime", nullable=true)
  120.      * @Groups({"notification:read", "notification:write", "notification:read:sendAt"})
  121.      */
  122.     private $sendAt;
  123.     /**
  124.      * @ORM\Column(type="string", length=255, nullable=true)
  125.      * @Assert\Choice(choices=self::STATUSES)
  126.      * @Groups({"notification:read", "notification:write", "notification:read:status"})
  127.      */
  128.     private $status self::STATUS_PENDING;
  129.     /**
  130.      * @ORM\Column(type="boolean", nullable=true, options={"default": "0"})
  131.      * @Assert\NotNull()
  132.      * @Groups({"notification:read", "notification:write", "notification:read:template"})
  133.      */
  134.     private $template false;
  135.     /**
  136.      * @ORM\Column(type="string", length=255, nullable=true)
  137.      * @Groups({"notification:read", "notification:write", "notification:read:link"})
  138.      */
  139.     private $link;
  140.     /**
  141.      * @ORM\Column(type="boolean", nullable=true, options={"default": "1"})
  142.      * @Assert\NotNull()
  143.      * @Groups({"notification:read", "notification:write", "notification:read:active"})
  144.      */
  145.     private $active true;
  146.     /**
  147.      * @ORM\Column(type="json", nullable=true)
  148.      * @Groups({"notification:read"})
  149.      */
  150.     private $metadata = [];
  151.     /**
  152.      * @ORM\Column(type="datetime", nullable=true)
  153.      * @Gedmo\Timestampable(on="create")
  154.      * @Groups({"notification:read", "notification:read:createdAt"})
  155.      */
  156.     private $createdAt;
  157.     /**
  158.      * @ORM\Column(type="datetime", nullable=true)
  159.      * @Gedmo\Timestampable(on="update")
  160.      * @Groups({"notification:read"})
  161.      */
  162.     private $updatedAt;
  163.     /**
  164.      * @ORM\OneToMany(targetEntity=UserNotification::class, mappedBy="notification", orphanRemoval=true, cascade={"persist", "remove"})
  165.      */
  166.     private $userNotifications;
  167.     /**
  168.      * @ORM\Column(type="string", length=255, nullable=true)
  169.      * @Groups({"notification:read", "notification:write"})
  170.      */
  171.     private $onesignalId;
  172.     /**
  173.      * @ORM\Column(type="json", nullable=true)
  174.      * @Groups({"notification:read", "notification:write"})
  175.      */
  176.     private $deviceTokens = [];
  177.     /**
  178.      * Custom Fields
  179.      */
  180.     /**
  181.      * Summary of companies
  182.      * @var mixed
  183.      * @Groups({"notification:read", "notification:write"})
  184.      */
  185.     private $companies;
  186.     /**
  187.      * Summary of $users
  188.      * @var mixed
  189.      * @Groups({"notification:read", "notification:write"})
  190.      */
  191.     private $users;
  192.     /**
  193.      * @Groups({"notification:read"})
  194.      * @var int|null
  195.      */
  196.     private $usersCount 0;
  197.     /**
  198.      * @Groups({"notification:read"})
  199.      * @var int|null
  200.      */
  201.     private $companiesCount 0;
  202.     public function __construct()
  203.     {
  204.         $this->companies = new ArrayCollection();
  205.         $this->users = new ArrayCollection();
  206.         $this->userNotifications = new ArrayCollection();
  207.         $this->createdAt = (new \DateTime("now", new \DateTimeZone("Europe/Paris")));
  208.         $this->updatedAt = (new \DateTime("now", new \DateTimeZone("Europe/Paris")));
  209.     }
  210.     public function getId(): ?int
  211.     {
  212.         return $this->id;
  213.     }
  214.     public function getType(): ?NotificationType
  215.     {
  216.         return $this->type;
  217.     }
  218.     public function setType(?NotificationType $type): self
  219.     {
  220.         $this->type $type;
  221.         return $this;
  222.     }
  223.     public function getTitle(): ?string
  224.     {
  225.         return $this->title;
  226.     }
  227.     public function setTitle(?string $title): self
  228.     {
  229.         $this->title $title;
  230.         return $this;
  231.     }
  232.     public function getBody(): ?array
  233.     {
  234.         return $this->body;
  235.     }
  236.     public function setBody(?array $body): self
  237.     {
  238.         $this->body $body;
  239.         return $this;
  240.     }
  241.     public function getPlanFor(): ?\DateTimeInterface
  242.     {
  243.         return $this->planFor;
  244.     }
  245.     public function setPlanFor(?\DateTimeInterface $planFor): self
  246.     {
  247.         $this->planFor $planFor;
  248.         return $this;
  249.     }
  250.     public function getSendAt(): ?\DateTimeInterface
  251.     {
  252.         return $this->sendAt;
  253.     }
  254.     public function setSendAt(?\DateTimeInterface $sendAt): self
  255.     {
  256.         $this->sendAt $sendAt;
  257.         return $this;
  258.     }
  259.     public function getStatus(): ?string
  260.     {
  261.         return $this->status;
  262.     }
  263.     public function setStatus(?string $status): self
  264.     {
  265.         $this->status $status;
  266.         return $this;
  267.     }
  268.     public function isTemplate(): ?bool
  269.     {
  270.         return $this->template;
  271.     }
  272.     public function setTemplate(?bool $template): self
  273.     {
  274.         $this->template $template;
  275.         return $this;
  276.     }
  277.     public function isActive(): ?bool
  278.     {
  279.         return $this->active;
  280.     }
  281.     public function setActive(?bool $active): self
  282.     {
  283.         $this->active $active;
  284.         return $this;
  285.     }
  286.     public function getMetadata(): ?array
  287.     {
  288.         return $this->metadata;
  289.     }
  290.     public function setMetadata(?array $metadata): self
  291.     {
  292.         $this->metadata $metadata;
  293.         return $this;
  294.     }
  295.     public function getCreatedAt(): ?\DateTimeInterface
  296.     {
  297.         return $this->createdAt;
  298.     }
  299.     public function setCreatedAt(?\DateTimeInterface $createdAt): self
  300.     {
  301.         $this->createdAt $createdAt;
  302.         return $this;
  303.     }
  304.     public function getUpdatedAt(): ?\DateTimeInterface
  305.     {
  306.         return $this->updatedAt;
  307.     }
  308.     public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
  309.     {
  310.         $this->updatedAt $updatedAt;
  311.         return $this;
  312.     }
  313.     public function getMessage(): ?string
  314.     {
  315.         return $this->message;
  316.     }
  317.     public function setMessage(?string $message): self
  318.     {
  319.         $this->message $message;
  320.         return $this;
  321.     }
  322.     public function getLink(): ?string
  323.     {
  324.         return $this->link;
  325.     }
  326.     public function setLink(?string $link): self
  327.     {
  328.         $this->link $link;
  329.         return $this;
  330.     }
  331.     /**
  332.      * @return Collection<int, UserNotification>
  333.      */
  334.     public function getUserNotifications(): Collection
  335.     {
  336.         return $this->userNotifications;
  337.     }
  338.     public function setUserNotifications(array $userNotifications): self
  339.     {
  340.         $this->userNotifications = new ArrayCollection($userNotifications);
  341.         
  342.         return $this;
  343.     }
  344.     public function addUserNotification(UserNotification $userNotification): self
  345.     {
  346.         if (!$this->userNotifications->contains($userNotification)) {
  347.             $this->userNotifications[] = $userNotification;
  348.             $userNotification->setNotification($this);
  349.         }
  350.         return $this;
  351.     }
  352.     public function removeUserNotification(UserNotification $userNotification): self
  353.     {
  354.         if ($this->userNotifications->removeElement($userNotification)) {
  355.             // set the owning side to null (unless already changed)
  356.             if ($userNotification->getNotification() === $this) {
  357.                 $userNotification->setNotification(null);
  358.             }
  359.         }
  360.         return $this;
  361.     }
  362.     /**
  363.      * @Groups({"notification:read"})
  364.      * @return Collection<int, Company>
  365.      */
  366.     public function getCompanies(): Collection
  367.     {
  368.         if(empty($this->companies)) $this->companies = new ArrayCollection;
  369.         foreach($this->userNotifications as $userNotification) {
  370.             if(empty($userNotification->getUser()) && $userNotification->getCompany() instanceof Company) {
  371.                 $this->companies->add($userNotification->getCompany());
  372.             }
  373.         }
  374.         
  375.         return $this->companies ?? new ArrayCollection;
  376.     }
  377.     public function setCompanies(array $companies)
  378.     {
  379.         $this->companies = new ArrayCollection;
  380.         foreach ($companies as $company) {
  381.             $this->addCompany($company);
  382.         }
  383.         
  384.         return $this;
  385.     }
  386.     public function addCompany(Company $company)
  387.     {
  388.         if(!$this->companies->contains($company)) {
  389.             $userNotification = new UserNotification;
  390.             $userNotification
  391.                 ->setNotification($this)
  392.                 ->setCompany($company)
  393.             ;
  394.         }
  395.         return $this->addUserNotification($userNotification);
  396.     }
  397.     public function removeCompany(Company $company)
  398.     {
  399.         if($this->companies->contains($company)) {
  400.             foreach ($this->userNotifications as $userNotification) {
  401.                 if(empty($userNotification->getUser()) && $userNotification->getCompany() instanceof Company) {
  402.                     return $this->removeUserNotification($userNotification);
  403.                 }
  404.             }
  405.         }
  406.         return $this;
  407.     }
  408.     /**
  409.      * @Groups({"notification:read"})
  410.      * @return Collection<int, User>
  411.      */
  412.     public function getUsers(): Collection
  413.     {
  414.         if(empty($this->users)) $this->users = new ArrayCollection;
  415.         
  416.         foreach($this->userNotifications as $userNotification) {
  417.             if($userNotification->getUser() instanceof User) {
  418.                 $this->users->add($userNotification->getUser());
  419.             }
  420.         }
  421.         
  422.         return $this->users;
  423.     }
  424.     public function addUser(User $user)
  425.     {
  426.         foreach ($this->getUserNotifications() as $userNotification) {
  427.             if ($userNotification->getUser() === $user) {
  428.                 return $this;
  429.             }
  430.         }
  431.         $userNotification = (new UserNotification())
  432.             ->setUser($user)
  433.             ->setDeviceToken($user->getDeviceToken() ?:  null)
  434.             ->setCompany($user->getCompany())
  435.             ->setNotification($this);
  436.         return $this->addUserNotification($userNotification);
  437.     }
  438.     public function removeUser(User $user)
  439.     {
  440.         foreach ($this->getUserNotifications() as $userNotification) {
  441.             if ($userNotification->getUser() === $user) {
  442.                 return $this->removeUserNotification($userNotification);
  443.             }
  444.         }
  445.         return $this;
  446.     }
  447.     public function getOnesignalId(): ?string
  448.     {
  449.         return $this->onesignalId;
  450.     }
  451.     public function setOnesignalId(?string $onesignalId): self
  452.     {
  453.         $this->onesignalId $onesignalId;
  454.         return $this;
  455.     }
  456.     public function getDeviceTokens(): ?array
  457.     {
  458.         return $this->deviceTokens;
  459.     }
  460.     public function setDeviceTokens(?array $deviceTokens): self
  461.     {
  462.         $this->deviceTokens $deviceTokens;
  463.         return $this;
  464.     }
  465.     public function getUsersCount()
  466.     {
  467.         foreach ($this->getUserNotifications() as $userNotification) {
  468.             if($userNotification instanceof UserNotification
  469.                 && $userNotification->getUser() instanceof User
  470.             ) {
  471.                 $this->usersCount ++;
  472.             }
  473.         }
  474.         return $this->usersCount;
  475.     }
  476.     public function getCompaniesCount()
  477.     {
  478.         foreach ($this->getUserNotifications() as $userNotification) {
  479.             if($userNotification instanceof UserNotification
  480.                 && empty($userNotification->getUser())
  481.                 && $userNotification->getCompany() instanceof Company
  482.             ) {
  483.                 $this->companiesCount ++;
  484.             }
  485.         }
  486.         return $this->companiesCount;
  487.     }
  488.     public function metadataActionsCount(?string $action null)
  489.     {
  490.         $sentCount $canceledCount0;
  491.         foreach ($this->metadata as $date => $metadata) {
  492.             switch ($metadata['action'] ?? null) {
  493.                 case self::STATUS_SENT:
  494.                     $sentCount++;
  495.                     break;
  496.                 case self::STATUS_CANCELED:
  497.                     $canceledCount++;
  498.                     break;
  499.                 default:
  500.                     break;
  501.             }
  502.         }
  503.         switch ($action ?? null) {
  504.             case self::STATUS_SENT:
  505.                 return $sentCount;
  506.             case self::STATUS_CANCELED:
  507.                 return $canceledCount;
  508.             default:
  509.                 return [
  510.                     'sentCount' => $sentCount,
  511.                     'canceledCount' => $canceledCount,
  512.                 ];
  513.         }
  514.     }
  515.     /**
  516.      * @Groups({"notification:read"})
  517.      * @return int
  518.      */
  519.     public function getMetadataSentCount(): int
  520.     {
  521.         return $this->metadataActionsCount(self::STATUS_SENT);
  522.     }
  523.     /**
  524.      * @Groups({"notification:read"})
  525.      * @return int
  526.      */
  527.     public function getMetadataCanceledCount(): int
  528.     {
  529.         return $this->metadataActionsCount(self::STATUS_CANCELED);
  530.     }
  531. }