src/Entity/Token.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiResource;
  4. use App\Repository\TokenRepository;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Component\Serializer\Annotation\Groups;
  9. /**
  10.  * @ORM\Entity(repositoryClass=TokenRepository::class)
  11.  * @ORM\HasLifecycleCallbacks()
  12.  */
  13. #[ApiResource(
  14.     attributes: ['security' => "is_granted('ROLE_USER')"],
  15.     normalizationContext: ['groups' => ['cstoken:read']],
  16.     denormalizationContext: ['groups' => ['cstoken:write']],
  17.     collectionOperations: [
  18.         'get',
  19.         'post' => [
  20.             'security' => "is_granted('ROLE_ADMIN')",
  21.         ],
  22.     ],
  23.     itemOperations: [
  24.         'get',
  25.         'put' => ['security' => "is_granted('ROLE_ADMIN') or object.owner == user"],
  26.     ],
  27. )]
  28. class Token
  29. {
  30.     /**
  31.      * @ORM\Id
  32.      * @ORM\GeneratedValue
  33.      * @ORM\Column(type="integer")
  34.      */
  35.     private $id;
  36.     /**
  37.      * @ORM\Column(type="string", length=128)
  38.      * @Groups({"cstoken:read", "cstoken:write"})
  39.      */
  40.     private $name;
  41.     /**
  42.      * @ORM\OneToMany(targetEntity=SurveyCompanyToken::class, mappedBy="token", orphanRemoval=true)
  43.      * @Groups({"cstoken:read", "cstoken:write"})
  44.      */
  45.     private $surveyCompanyTokens;
  46.     public function __construct()
  47.     {
  48.         $this->surveyCompanyTokens = new ArrayCollection();
  49.     }
  50.     public function __toString(): string
  51.     {
  52.         return $this->getName();
  53.     }
  54.     public function getId(): ?int
  55.     {
  56.         return $this->id;
  57.     }
  58.     public function getName(): ?string
  59.     {
  60.         return $this->name;
  61.     }
  62.     public function setName(string $name): self
  63.     {
  64.         $this->name $name;
  65.         return $this;
  66.     }
  67.     /**
  68.      * @return Collection|SurveyCompanyToken[]
  69.      */
  70.     public function getCompanyToken(): Collection
  71.     {
  72.         return $this->companyToken;
  73.     }
  74.     public function addCompanyToken(SurveyCompanyToken $companyToken): self
  75.     {
  76.         if (!$this->companyToken->contains($companyToken)) {
  77.             $this->companyToken[] = $companyToken;
  78.             $companyToken->setToken($this);
  79.         }
  80.         return $this;
  81.     }
  82.     public function removeCompanyToken(SurveyCompanyToken $companyToken): self
  83.     {
  84.         if ($this->companyToken->removeElement($companyToken)) {
  85.             // set the owning side to null (unless already changed)
  86.             if ($companyToken->getToken() === $this) {
  87.                 $companyToken->setToken(null);
  88.             }
  89.         }
  90.         return $this;
  91.     }
  92. }