src/Entity/Client.php line 64

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\Annotation\ApiSubresource;
  6. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  7. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  8. use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
  9. use App\Repository\ClientRepository;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Doctrine\Common\Collections\Collection;
  12. use Doctrine\ORM\Mapping as ORM;
  13. use Doctrine\ORM\Mapping\InheritanceType;
  14. use Gedmo\Mapping\Annotation as Gedmo;
  15. use Symfony\Component\HttpFoundation\File\File;
  16. use Symfony\Component\HttpFoundation\File\UploadedFile;
  17. use Symfony\Component\Serializer\Annotation\Groups;
  18. use Symfony\Component\Validator\Constraints as Assert;
  19. /**
  20.  * @InheritanceType("SINGLE_TABLE")
  21.  * @ApiResource(
  22.  *      normalizationContext={
  23.  *          "groups"={"client:read"}
  24.  *      },
  25.  *      denormalizationContext={
  26.  *          "groups"={"client:write"}
  27.  *      },
  28.  *     itemOperations={
  29.  *          "get"={
  30.  *              "security"="is_granted('ROLE_USER')"
  31.  *          }, 
  32.  *          "patch"={
  33.  *              "security"="is_granted('ROLE_ADMIN') or user == object.getUser()"
  34.  *          }
  35.  *      },
  36.  *      collectionOperations={
  37.  *          "get"={
  38.  *              "security"="is_granted('ROLE_USER')"
  39.  *          },
  40.  *          "post_update"={
  41.  *              "path"="/clients/{id}",
  42.  *              "security"="is_granted('ROLE_ADMIN') or is_granted('ROLE_CLIENT')",
  43.  *              "description"="Update a Client with method POST (using content-type: 'multipart')",
  44.  *              "method"="POST",
  45.  *              "controller"="App\Controller\Api\ClientController::update"
  46.  *          }
  47.  *     }
  48.  * )
  49.  * @ApiFilter(PropertyFilter::class, 
  50.  *      arguments={
  51.  *          "parameterName"="fields", 
  52.  *          "overrideDefaultProperties"=true
  53.  *     }
  54.  * )
  55.  * @ApiFilter(OrderFilter::class, properties={"id", "user.id", "user.active", "user.lastLogin", "user.status", "user.quotas"})
  56.  * @ApiFilter(SearchFilter::class, properties={"user.email": "partial", "user.status": "exact", "user.active": "partial", "company.name": "partial", "company.id": "exact"})
  57.  * @ORM\Entity(repositoryClass=ClientRepository::class)
  58.  * @ORM\HasLifecycleCallbacks()
  59.  */
  60. class Client
  61. {
  62.     /**
  63.      * @ORM\Id
  64.      * @ORM\GeneratedValue
  65.      * @ORM\Column(type="integer")
  66.      * @Groups({"client:read", "user:read", "company:read", "team_user:read", "chat_message:read", "chat:read", "message:read"})
  67.      */
  68.     private $id;
  69.     /**
  70.      * @ORM\Column(type="string", length=255, nullable=true)
  71.      * @Groups({"client:read", "client:write", "user:read", "user:write", "company:read", "team_user:read", "chat_message:read", "chat:read", "message:read"})
  72.      * @Assert\NotBlank
  73.      */
  74.     private $firstName;
  75.     /**
  76.      * @ORM\Column(type="string", length=255, nullable=true)
  77.      * @Groups({"client:read", "client:write", "user:read", "user:write", "company:read", "team_user:read", "chat_message:read", "chat:read", "message:read"})
  78.      * @Assert\NotBlank
  79.      */
  80.     private $lastName;
  81.     /**
  82.      * @ORM\Column(type="string", length=255, nullable=true)
  83.      * @Groups({"client:read", "client:write", "user:read", "user:write", "company:read"})
  84.      */
  85.     private $paymentUid;
  86.     /**
  87.      * @ORM\OneToOne(targetEntity=User::class, inversedBy="client", cascade={"persist", "refresh", "remove"})
  88.      * @ORM\JoinColumn(nullable=false)
  89.      * @Groups({"client:read", "client:write", "company:read"})
  90.      * @var User
  91.      */
  92.     private $user;
  93.     /**
  94.      * @ORM\OneToMany(targetEntity=EmailProgrammation::class, mappedBy="client", orphanRemoval=true)
  95.      */
  96.     private $emailProgrammations;
  97.     /**
  98.      * @ORM\OneToMany(targetEntity=Registration::class, mappedBy="client", orphanRemoval=true)
  99.      */
  100.     private $registrations;
  101.     /**
  102.      * @ORM\OneToMany(targetEntity=PubClient::class, mappedBy="client", orphanRemoval=true, cascade={"persist", "refresh", "remove"})
  103.      */
  104.     private $pubClients;
  105.     /**
  106.      * @ORM\ManyToOne(targetEntity=Company::class, inversedBy="clients")
  107.      * @ORM\JoinColumn(nullable=false)
  108.      * @Assert\NotBlank
  109.      * @Groups({"client:read", "client:write", "user:read", "user:write", "message:read"})
  110.      */
  111.     private $company;
  112.     /**
  113.      * @ORM\Column(type="boolean", nullable=true , options={"default": "0"})
  114.      * @Groups({"client:read", "client:write", "user:read", "user:write", "company:read"})
  115.      */
  116.     private $isCgu = false;
  117.     /**
  118.      * Date d'acceptation des CGU de l'app (clause de tolérance zéro, guideline Apple 1.2), en lecture seule.
  119.      * Écrite à l'inscription et par POST /api/me/cgu/accept.
  120.      *
  121.      * @ORM\Column(type="datetime", nullable=true)
  122.      * @Groups({"client:read", "client:read:cguAcceptedAt"})
  123.      */
  124.     private ?\DateTimeInterface $cguAcceptedAt = null;
  125.     /**
  126.      * Version des CGU acceptée ; les écritures de contenu sont refusées tant qu'elle diffère de
  127.      * %moderation.cgu_version% (App\Service\Moderation\CguGuard). Lecture seule.
  128.      *
  129.      * @ORM\Column(type="string", length=20, nullable=true)
  130.      * @Groups({"client:read", "client:read:cguVersion"})
  131.      */
  132.     private ?string $cguVersion = null;
  133.     /**
  134.      * @ORM\ManyToOne(targetEntity=Segmentation::class, inversedBy="clients")
  135.      * @Groups({"client:read", "client:write", "user:read", "user:write", "company:read", "chat_message:read", "chat:read"})
  136.      */
  137.     private $segmentation;
  138.     /**
  139.      * @ORM\ManyToOne(targetEntity=Association::class, inversedBy="clients")
  140.      * @Groups({"client:read", "client:write", "user:read", "user:write", "company:read"})
  141.      * @ApiSubresource(maxDepth=1)
  142.      */
  143.     private $association;
  144.     /**
  145.      * @ORM\ManyToOne(targetEntity=MediaObject::class, cascade={"persist", "refresh", "remove"})
  146.      * @Groups({"client:read", "team_user:read", "user:read", "chat_message:read", "chat:read"})
  147.      */
  148.     private $avatar;
  149.     /**
  150.      * @ORM\Column(type="datetime", options={"default": "CURRENT_TIMESTAMP"})
  151.      * @Gedmo\Timestampable(on="create")
  152.      * @Groups({"client:read", "client:read:createdAt"})
  153.      */
  154.     private $createdAt;
  155.     /**
  156.      * @ORM\Column(type="datetime", nullable=true)
  157.      * @Gedmo\Timestampable(on="update")
  158.      * @Groups({"client:read", "client:read:updatedAt"})
  159.      */
  160.     private $updatedAt;
  161.     
  162.     public function __construct()
  163.     {
  164.         $this->user = new User();
  165.         $this->emailProgrammations = new ArrayCollection();
  166.         $this->registrations = new ArrayCollection();
  167.         $this->pubClients = new ArrayCollection();
  168.     }
  169.     public function getId(): ?int
  170.     {
  171.         return $this->id;
  172.     }
  173.     public function getFirstName(): ?string
  174.     {
  175.         return $this->firstName;
  176.     }
  177.     public function setFirstName(?string $firstName): self
  178.     {
  179.         $this->firstName = $firstName;
  180.         $this->user->setFirstName($this->firstName);
  181.         $this->user->setUpdatedAt(new \DateTime);
  182.         return $this;
  183.     }
  184.     public function getLastName(): ?string
  185.     {
  186.         return $this->lastName;
  187.     }
  188.     public function setLastName(?string $lastName): self
  189.     {
  190.         $this->lastName = $lastName;
  191.         $this->user->setLastName($this->lastName);
  192.         $this->user->setUpdatedAt(new \DateTime);
  193.         return $this;
  194.     }
  195.     public function getPaymentUid(): ?string
  196.     {
  197.         return $this->paymentUid;
  198.     }
  199.     public function setPaymentUid(?string $paymentUid): self
  200.     {
  201.         $this->paymentUid = $paymentUid;
  202.         return $this;
  203.     }
  204.     public function getUser(): ?User
  205.     {
  206.         return $this->user;
  207.     }
  208.     public function setUser(User $user): self
  209.     {
  210.         $this->user = $user;
  211.         return $this;
  212.     }
  213.     /**
  214.      * @return Collection|EmailProgrammation[]
  215.      */
  216.     public function getEmailProgrammations(): Collection
  217.     {
  218.         return $this->emailProgrammations;
  219.     }
  220.     public function addEmailProgrammation(EmailProgrammation $emailProgrammation): self
  221.     {
  222.         if (!$this->emailProgrammations->contains($emailProgrammation)) {
  223.             $this->emailProgrammations[] = $emailProgrammation;
  224.             $emailProgrammation->setClient($this);
  225.         }
  226.         return $this;
  227.     }
  228.     public function removeEmailProgrammation(EmailProgrammation $emailProgrammation): self
  229.     {
  230.         if ($this->emailProgrammations->removeElement($emailProgrammation)) {
  231.             // set the owning side to null (unless already changed)
  232.             if ($emailProgrammation->getClient() === $this) {
  233.                 $emailProgrammation->setClient(null);
  234.             }
  235.         }
  236.         return $this;
  237.     }
  238.     /**
  239.      * @return Collection|Registration[]
  240.      */
  241.     public function getRegistrations(): Collection
  242.     {
  243.         return $this->registrations;
  244.     }
  245.     public function addRegistration(Registration $registration): self
  246.     {
  247.         if (!$this->registrations->contains($registration)) {
  248.             $this->registrations[] = $registration;
  249.             $registration->setClient($this);
  250.         }
  251.         return $this;
  252.     }
  253.     public function removeRegistration(Registration $registration): self
  254.     {
  255.         if ($this->registrations->removeElement($registration)) {
  256.             // set the owning side to null (unless already changed)
  257.             if ($registration->getClient() === $this) {
  258.                 $registration->setClient(null);
  259.             }
  260.         }
  261.         return $this;
  262.     }
  263.     /**
  264.      * @return Collection|PubClient[]
  265.      */
  266.     public function getPubClients(): Collection
  267.     {
  268.         return $this->pubClients;
  269.     }
  270.     public function addPubClient(PubClient $pubClient): self
  271.     {
  272.         if (!$this->pubClients->contains($pubClient)) {
  273.             $this->pubClients[] = $pubClient;
  274.             $pubClient->setClient($this);
  275.         }
  276.         return $this;
  277.     }
  278.     public function removePubClient(PubClient $pubClient): self
  279.     {
  280.         if ($this->pubClients->removeElement($pubClient)) {
  281.             // set the owning side to null (unless already changed)
  282.             if ($pubClient->getClient() === $this) {
  283.                 $pubClient->setClient(null);
  284.             }
  285.         }
  286.         return $this;
  287.     }
  288.     public function __toString()
  289.     {
  290.         return $this->firstName . ' ' . $this->lastName;
  291.     }
  292.     /**
  293.      * @ORM\PrePersist()
  294.      */
  295.     public function prePersist()
  296.     {
  297.         $this->user->setCategory(User::GROUP_CLIENT);
  298.         $this->user->addRole(User::ROLE_CLIENT);
  299.     }
  300.     public function getCompany(): ?Company
  301.     {
  302.         return $this->company;
  303.     }
  304.     public function setCompany(?Company $company): self
  305.     {
  306.         $this->company = $company;
  307.         return $this;
  308.     }
  309.     /**
  310.      * Return in coming events
  311.      */
  312.     public function getInComingEvents()
  313.     {
  314.         $inComingEvents = [];
  315.         if(!empty($this->getRegistrations()->toArray())) {
  316.             $today = new \DateTime("now");
  317.             foreach ($this->getRegistrations()->toArray() as $key => $registration) {
  318.                 if($registration instanceof Registration && $registration->getEvent() instanceof Event) {
  319.                     if(in_array($registration->getStatus(), [Registration::STATUS_REGISTER]) 
  320.                         && in_array($registration->getEvent()->getStatus(), [Event::STATUS_VALID]) 
  321.                         && $registration->getEvent()->getStart() >= $today) {
  322.                         $inComingEvents[] = $registration->getEvent();
  323.                     }
  324.                 }
  325.             }
  326.             $inComingEvents = array_unique($inComingEvents, SORT_REGULAR);
  327.         }
  328.         return $inComingEvents;
  329.     }
  330.     /**
  331.      * Return past events
  332.      */
  333.     public function getPastEvents()
  334.     {
  335.         $pastEvents = [];
  336.         if(!empty($this->getRegistrations()->toArray())) {
  337.             $today = new \DateTime("now");
  338.             foreach ($this->getRegistrations()->toArray() as $key => $registration) {
  339.                 if($registration instanceof Registration && $registration->getEvent() instanceof Event) {
  340.                     if($registration->getEvent()->getEnd() <= $today) {
  341.                         $pastEvents[] = $registration->getEvent();
  342.                     }
  343.                 }
  344.             }
  345.             $pastEvents = array_unique($pastEvents, SORT_REGULAR);
  346.         }
  347.         return $pastEvents;
  348.     }
  349.     public function getIsCgu(): ?bool
  350.     {
  351.         return $this->isCgu;
  352.     }
  353.     public function setIsCgu(?bool $isCgu): self
  354.     {
  355.         $this->isCgu = $isCgu;
  356.         return $this;
  357.     }
  358.     public function getCguAcceptedAt(): ?\DateTimeInterface
  359.     {
  360.         return $this->cguAcceptedAt;
  361.     }
  362.     public function getCguVersion(): ?string
  363.     {
  364.         return $this->cguVersion;
  365.     }
  366.     /**
  367.      * Seul point d'écriture de l'acceptation (pas de setters séparés : ToolsService::insertDataFromPostRequest
  368.      * appelle tout setter présent dans la requête).
  369.      */
  370.     public function acceptCgu(string $version, \DateTimeInterface $at): self
  371.     {
  372.         $this->cguVersion = $version;
  373.         $this->cguAcceptedAt = $at;
  374.         return $this;
  375.     }
  376.     public function getSegmentation(): ?Segmentation
  377.     {
  378.         return $this->segmentation;
  379.     }
  380.     public function setSegmentation(?Segmentation $segmentation): self
  381.     {
  382.         $this->segmentation = $segmentation;
  383.         // Entonnoir multi-axes : toute écriture du miroir client (ex : PATCH API de l'app)
  384.         // doit synchroniser le miroir user et le pivot user_segmentation.
  385.         // Comparaison d'instance : User::setSegmentation a déjà posé sa valeur quand il
  386.         // redescend ici, ce qui coupe la récursion.
  387.         if ($this->user instanceof User && $this->user->getSegmentation() !== $segmentation) {
  388.             $this->user->setSegmentation($segmentation);
  389.         }
  390.         return $this;
  391.     }
  392.     public function getAssociation(): ?Association
  393.     {
  394.         return $this->association;
  395.     }
  396.     public function setAssociation(?Association $association): self
  397.     {
  398.         $this->association = $association;
  399.         return $this;
  400.     }
  401.     public function getAvatar(): ?MediaObject
  402.     {
  403.         return $this->avatar;
  404.     }
  405.     public function setAvatar(?MediaObject $avatar): self
  406.     {
  407.         $this->avatar = $avatar;
  408.         return $this;
  409.     }
  410.     /**
  411.      * @Groups({"client:write"})
  412.      */
  413.     public function setAvatarFile($file = null): self
  414.     {
  415.         if($file instanceof UploadedFile) {
  416.             $avatar = empty($this->avatar) ? new MediaObject : $this->avatar;
  417.             $avatar->setFile($file);
  418.             $this->setAvatar($avatar);
  419.         }
  420.         return $this;
  421.     }
  422.     public function getCreatedAt(): ?\DateTime
  423.     {
  424.         return $this->createdAt;
  425.     }
  426.     public function setCreatedAt(?\DateTime $createdAt): self
  427.     {
  428.         $this->createdAt = $createdAt;
  429.         return $this;
  430.     }
  431.     public function getUpdatedAt(): ?\DateTime
  432.     {
  433.         return $this->updatedAt;
  434.     }
  435.     public function setUpdatedAt(?\DateTime $updatedAt): self
  436.     {
  437.         $this->updatedAt = $updatedAt;
  438.         return $this;
  439.     }
  440.     /**
  441.      * @Groups({"client:write"})
  442.      */
  443.     public function setEmail(string $email): self
  444.     {
  445.         if($this->getUser()) {
  446.             $this->getUser()->setEmail(strtolower($email));
  447.             $this->getUser()->setUpdatedAt(new \DateTime());
  448.         }
  449.         return $this;
  450.     }
  451. }