src/Entity/Task.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Traits\TimeStampable;
  4. use App\Repository\TaskRepository;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\DBAL\Types\Types;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Symfony\Component\Serializer\Annotation\Groups;
  10. #[ORM\Entity(repositoryClass: TaskRepository::class)]
  11. #[ORM\HasLifecycleCallbacks]
  12. class Task
  13. {
  14.     use TimeStampable;
  15.     #[ORM\Id]
  16.     #[ORM\GeneratedValue]
  17.     #[Groups(['taskProjet:read', 'task:read'])]
  18.     #[ORM\Column]
  19.     private ?int $id = null;
  20.     #[ORM\ManyToOne(inversedBy: 'tasks')]
  21.     private ?Projet $projet = null;
  22.     #[ORM\Column(length: 255)]
  23.     #[Groups(['taskProjet:read', 'task:read'])]
  24.     private ?string $titre = null;
  25.     #[ORM\Column(length: 2000, nullable: true)]
  26.     #[Groups(['taskProjet:read', 'task:read'])]
  27.     private ?string $description = null;
  28.     #[ORM\Column(type: Types::DATE_MUTABLE)]
  29.     #[Groups(['task:read'])]
  30.     private ?\DateTimeInterface $dateStart = null;
  31.     #[ORM\Column(type: Types::DATE_MUTABLE)]
  32.     #[Groups(['task:read'])]
  33.     private ?\DateTimeInterface $dateEnd = null;
  34.     #[ORM\ManyToOne(inversedBy: 'tasks')]
  35.     #[Groups(['taskProjet:read', 'task:read'])]
  36.     private ?Priority $priority = null;
  37.     #[ORM\OneToMany(mappedBy: 'task', targetEntity: TaskAssign::class, cascade: ['remove'])]
  38.     private Collection $taskAssigns;
  39.     #[ORM\Column(length: 255, nullable: true)]
  40.     // #[Groups(['taskProjet:read'])]
  41.     private ?string $status = null;
  42.     #[ORM\OneToMany(mappedBy: 'tache', targetEntity: Document::class)]
  43.     private Collection $documents;
  44.     #[ORM\OneToMany(mappedBy: 'task', targetEntity: Comment::class, cascade: ['remove'])]
  45.     private Collection $comments;
  46.     #[ORM\OneToMany(mappedBy: 'tache', targetEntity: RateOfProgress::class,  cascade: ['persist', 'remove'])]
  47.     #[Groups(['task:read'])]
  48.     private Collection $rateOfProgress;
  49.     #[ORM\Column]
  50.     private ?bool $isValided = null;
  51.     public function __construct()
  52.     {
  53.         $this->status = "nouvelle";
  54.         $this->taskAssigns = new ArrayCollection();
  55.         $this->createdAt = new \DateTimeImmutable();
  56.         $this->documents = new ArrayCollection();
  57.         $this->comments = new ArrayCollection();
  58.         $this->rateOfProgress = new ArrayCollection();
  59.         $this->isValided = false;
  60.     }
  61.     public function getId(): ?int
  62.     {
  63.         return $this->id;
  64.     }
  65.     public function getProjet(): ?Projet
  66.     {
  67.         return $this->projet;
  68.     }
  69.     public function setProjet(?Projet $projet): self
  70.     {
  71.         $this->projet = $projet;
  72.         return $this;
  73.     }
  74.     public function getTitre(): ?string
  75.     {
  76.         return $this->titre;
  77.     }
  78.     public function setTitre(string $titre): self
  79.     {
  80.         $this->titre = $titre;
  81.         return $this;
  82.     }
  83.     public function getDescription(): ?string
  84.     {
  85.         return $this->description;
  86.     }
  87.     public function setDescription(string $description): self
  88.     {
  89.         $this->description = $description;
  90.         return $this;
  91.     }
  92.     public function getDateStart(): ?\DateTimeInterface
  93.     {
  94.         return $this->dateStart;
  95.     }
  96.     public function setDateStart(\DateTimeInterface $dateStart): self
  97.     {
  98.         $this->dateStart = $dateStart;
  99.         return $this;
  100.     }
  101.     public function getDateEnd(): ?\DateTimeInterface
  102.     {
  103.         return $this->dateEnd;
  104.     }
  105.     public function setDateEnd(\DateTimeInterface $dateEnd): self
  106.     {
  107.         $this->dateEnd = $dateEnd;
  108.         return $this;
  109.     }
  110.     public function getPriority(): ?Priority
  111.     {
  112.         return $this->priority;
  113.     }
  114.     public function setPriority(?Priority $priority): self
  115.     {
  116.         $this->priority = $priority;
  117.         $this->priority->setIsDeleted(false);
  118.         return $this;
  119.     }
  120.     /**
  121.      * @return Collection<int, TaskAssign>
  122.      */
  123.     public function getTaskAssigns(): Collection
  124.     {
  125.         return $this->taskAssigns;
  126.     }
  127.     public function addTaskAssign(TaskAssign $taskAssign): self
  128.     {
  129.         if (!$this->taskAssigns->contains($taskAssign)) {
  130.             $this->taskAssigns->add($taskAssign);
  131.             $taskAssign->setTask($this);
  132.         }
  133.         return $this;
  134.     }
  135.     public function removeTaskAssign(TaskAssign $taskAssign): self
  136.     {
  137.         if ($this->taskAssigns->removeElement($taskAssign)) {
  138.             // set the owning side to null (unless already changed)
  139.             if ($taskAssign->getTask() === $this) {
  140.                 $taskAssign->setTask(null);
  141.             }
  142.         }
  143.         return $this;
  144.     }
  145.     public function getStatus(): ?string
  146.     {
  147.         return $this->status;
  148.     }
  149.     public function setStatus(string $status): self
  150.     {
  151.         $this->status = $status;
  152.         return $this;
  153.     }
  154.     public function getUsersAssign(): array
  155.     {
  156.         $userAssing = [];
  157.         foreach ($this->taskAssigns as $assig) {
  158.             $userAssing[] = $assig->getUser();
  159.         }
  160.         return $userAssing;
  161.     }
  162.     public function getUserNotAssign(array $users): array
  163.     {
  164.         $userAssign = $this->getUsersAssign();
  165.         $userNotAssing = array_diff($users, $userAssign);
  166.         return $userNotAssing;
  167.     }
  168.     /**
  169.      * @return Collection<int, Document>
  170.      */
  171.     public function getDocuments(): Collection
  172.     {
  173.         return $this->documents;
  174.     }
  175.     public function addDocument(Document $document): static
  176.     {
  177.         if (!$this->documents->contains($document)) {
  178.             $this->documents->add($document);
  179.             $document->setTache($this);
  180.         }
  181.         return $this;
  182.     }
  183.     public function removeDocument(Document $document): static
  184.     {
  185.         if ($this->documents->removeElement($document)) {
  186.             // set the owning side to null (unless already changed)
  187.             if ($document->getTache() === $this) {
  188.                 $document->setTache(null);
  189.             }
  190.         }
  191.         return $this;
  192.     }
  193.     /**
  194.      * @return Collection<int, Comment>
  195.      */
  196.     public function getComments(): Collection
  197.     {
  198.         return $this->comments;
  199.     }
  200.     public function addComment(Comment $comment): static
  201.     {
  202.         if (!$this->comments->contains($comment)) {
  203.             $this->comments->add($comment);
  204.             $comment->setTask($this);
  205.         }
  206.         return $this;
  207.     }
  208.     public function removeComment(Comment $comment): static
  209.     {
  210.         if ($this->comments->removeElement($comment)) {
  211.             // set the owning side to null (unless already changed)
  212.             if ($comment->getTask() === $this) {
  213.                 $comment->setTask(null);
  214.             }
  215.         }
  216.         return $this;
  217.     }
  218.     /**
  219.      * @return Collection<int, RateOfProgress>
  220.      */
  221.     public function getRateOfProgress(): Collection
  222.     {
  223.         return $this->rateOfProgress;
  224.     }
  225.     public function addRateOfProgress(RateOfProgress $rateOfProgress): static
  226.     {
  227.         if (!$this->rateOfProgress->contains($rateOfProgress)) {
  228.             $this->rateOfProgress->add($rateOfProgress);
  229.             $rateOfProgress->setTache($this);
  230.         }
  231.         return $this;
  232.     }
  233.     public function removeRateOfProgress(RateOfProgress $rateOfProgress): static
  234.     {
  235.         if ($this->rateOfProgress->removeElement($rateOfProgress)) {
  236.             // set the owning side to null (unless already changed)
  237.             if ($rateOfProgress->getTache() === $this) {
  238.                 $rateOfProgress->setTache(null);
  239.             }
  240.         }
  241.         return $this;
  242.     }
  243.     public function isIsValided(): ?bool
  244.     {
  245.         return $this->isValided;
  246.     }
  247.     public function setIsValided(bool $isValided): static
  248.     {
  249.         $this->isValided = $isValided;
  250.         return $this;
  251.     }
  252.     #[Groups(['taskProjet:read', 'task:read'])]
  253.     public function getTauxAvancement(): int
  254.     {
  255.         $taux = [];
  256.         $countAvaluation = count($this->getRateOfProgress());
  257.         foreach ($this->rateOfProgress as $rate) {
  258.             if ($rate->isIsValided()) {
  259.                 $taux[] = 100;
  260.             } else {
  261.                 $taux[] = 0;
  262.             }
  263.         }
  264.  
  265.         $sumTaux = array_sum($taux) > 0 ? array_sum($taux)/$countAvaluation: 0;
  266.         return $sumTaux;
  267.     }
  268. }