src/Entity/Company/Company.php line 50

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Company;
  3. use ApiPlatform\Core\Annotation\ApiProperty;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use App\Entity\Operation\Operation;
  6. use App\Entity\Participation\Participation;
  7. use App\Entity\User\User;
  8. use App\Repository\Company\CompanyRepository;
  9. use Doctrine\Common\Collections\ArrayCollection;
  10. use Doctrine\Common\Collections\Collection;
  11. use Doctrine\ORM\Mapping as ORM;
  12. use Gedmo\Mapping\Annotation as Gedmo;
  13. use Gedmo\Timestampable\Traits\TimestampableEntity;
  14. use Ramsey\Uuid\Doctrine\UuidGenerator;
  15. use Ramsey\Uuid\Lazy\LazyUuidFromString;
  16. use Symfony\Component\Serializer\Annotation\Groups;
  17. use Symfony\Component\Validator\Constraints as Assert;
  18. /**
  19.  * @ApiResource(
  20.  *      attributes={
  21.  *          "normalization_context"={"groups"={"Company:io","Company:o"}},
  22.  *          "denormalization_context"={"groups"={"Company:io","Company:i"}}
  23.  *      },
  24.  *     collectionOperations={
  25.  *          "post"={
  26.  *              "method"="POST",
  27.  *              "access_control"="is_granted('ROLE_ADMIN_SOGEC')",
  28.  *          },
  29.  *          "get"={
  30.  *              "method"="GET",
  31.  *              "access_control"="is_granted('ROLE_ADMIN_SOGEC')",
  32.  *          },
  33.  *     },
  34.  *     itemOperations={
  35.  *          "put"={
  36.  *              "method"="PUT",
  37.  *              "access_control"="is_granted('ROLE_ADMIN_SOGEC')",
  38.  *          },
  39.  *          "get"={
  40.  *              "method"="GET",
  41.  *              "access_control"="is_granted('ROLE_ADMIN_SOGEC')",
  42.  *          },
  43.  *      }
  44.  * )
  45.  * @ORM\Entity(repositoryClass=CompanyRepository::class)
  46.  */
  47. class Company
  48. {
  49.     /**
  50.      * Hook timestampable behavior
  51.      * updates createdAt, updatedAt fields
  52.      */
  53.     use TimestampableEntity;
  54.     /**
  55.      * @ORM\Id
  56.      * @ORM\Column(type="uuid", unique=true)
  57.      * @ORM\GeneratedValue(strategy="CUSTOM")
  58.      * @ORM\CustomIdGenerator(class=UuidGenerator::class)
  59.      * @ApiProperty(identifier=true)
  60.      * @Groups({"Provider:o", "Operation:o", "Coupon:o"})
  61.      */
  62.     private $id;
  63.     /**
  64.      * @ORM\Column(type="string", length=50)
  65.      * @Assert\NotBlank()
  66.      */
  67.     private $name;
  68.     /**
  69.      * @ORM\Column(type="string", length=255, nullable=true)
  70.      * @Assert\Url()
  71.      */
  72.     private $logo;
  73.     /**
  74.      * @ORM\Column(type="string", length=255)
  75.      * @Gedmo\Slug(fields={"name"})
  76.      */
  77.     private $slug;
  78.     /**
  79.      * @ORM\OneToMany(targetEntity=Credentials::class, mappedBy="company", orphanRemoval=true)
  80.      */
  81.     private $credentials;
  82.     /**
  83.      * @ORM\OneToMany(targetEntity=User::class, mappedBy="company")
  84.      */
  85.     private $users;
  86.     /**
  87.      * @ORM\OneToMany(targetEntity=Participation::class, mappedBy="company")
  88.      */
  89.     private $participations;
  90.     /**
  91.      * @ORM\OneToMany(targetEntity=Operation::class, mappedBy="company")
  92.      */
  93.     private $operations;
  94.     /**
  95.      * @ORM\Column(type="string", length=255)
  96.      */
  97.     private $callbackUrl;
  98.     /**
  99.      * @ORM\OneToMany(targetEntity=Param::class, mappedBy="company", orphanRemoval=true)
  100.      */
  101.     private $params;
  102.     public function __construct()
  103.     {
  104.         $this->credentials = new ArrayCollection();
  105.         $this->users = new ArrayCollection();
  106.         $this->participations = new ArrayCollection();
  107.         $this->operations = new ArrayCollection();
  108.         $this->params = new ArrayCollection();
  109.     }
  110.     public function getId(): ?LazyUuidFromString
  111.     {
  112.         return $this->id;
  113.     }
  114.     /**
  115.      * We add this for fixtures only
  116.      */
  117.     public function setId($id)
  118.     {
  119.         $this->id $id;
  120.         return $this;
  121.     }
  122.     public function getName(): ?string
  123.     {
  124.         return $this->name;
  125.     }
  126.     public function setName(string $name): self
  127.     {
  128.         $this->name $name;
  129.         return $this;
  130.     }
  131.     public function getLogo(): ?string
  132.     {
  133.         return $this->logo;
  134.     }
  135.     public function setLogo(?string $logo): self
  136.     {
  137.         $this->logo $logo;
  138.         return $this;
  139.     }
  140.     public function getSlug(): ?string
  141.     {
  142.         return $this->slug;
  143.     }
  144.     public function setSlug(string $slug): self
  145.     {
  146.         $this->slug $slug;
  147.         return $this;
  148.     }
  149.     /**
  150.      * @return Collection|Credentials[]
  151.      */
  152.     public function getCredentials(): Collection
  153.     {
  154.         return $this->credentials;
  155.     }
  156.     public function addCredential(Credentials $credential): self
  157.     {
  158.         if (!$this->credentials->contains($credential)) {
  159.             $this->credentials[] = $credential;
  160.             $credential->setCompany($this);
  161.         }
  162.         return $this;
  163.     }
  164.     public function removeCredential(Credentials $credential): self
  165.     {
  166.         if ($this->credentials->removeElement($credential)) {
  167.             // set the owning side to null (unless already changed)
  168.             if ($credential->getCompany() === $this) {
  169.                 $credential->setCompany(null);
  170.             }
  171.         }
  172.         return $this;
  173.     }
  174.     /**
  175.      * @return Collection|User[]
  176.      */
  177.     public function getUsers(): Collection
  178.     {
  179.         return $this->users;
  180.     }
  181.     public function addUser(User $user): self
  182.     {
  183.         if (!$this->users->contains($user)) {
  184.             $this->users[] = $user;
  185.             $user->setCompany($this);
  186.         }
  187.         return $this;
  188.     }
  189.     public function removeUser(User $user): self
  190.     {
  191.         if ($this->users->removeElement($user)) {
  192.             // set the owning side to null (unless already changed)
  193.             if ($user->getCompany() === $this) {
  194.                 $user->setCompany(null);
  195.             }
  196.         }
  197.         return $this;
  198.     }
  199.     /**
  200.      * @return Collection|Participation[]
  201.      */
  202.     public function getParticipations(): Collection
  203.     {
  204.         return $this->participations;
  205.     }
  206.     public function addParticipation(Participation $participation): self
  207.     {
  208.         if (!$this->participations->contains($participation)) {
  209.             $this->participations[] = $participation;
  210.             $participation->setCompany($this);
  211.         }
  212.         return $this;
  213.     }
  214.     public function removeParticipation(Participation $participation): self
  215.     {
  216.         if ($this->participations->removeElement($participation)) {
  217.             // set the owning side to null (unless already changed)
  218.             if ($participation->getCompany() === $this) {
  219.                 $participation->setCompany(null);
  220.             }
  221.         }
  222.         return $this;
  223.     }
  224.     /**
  225.      * @return Collection|Operation[]
  226.      */
  227.     public function getOperations(): Collection
  228.     {
  229.         return $this->operations;
  230.     }
  231.     public function addOperation(Operation $operation): self
  232.     {
  233.         if (!$this->operations->contains($operation)) {
  234.             $this->operations[] = $operation;
  235.             $operation->setCompany($this);
  236.         }
  237.         return $this;
  238.     }
  239.     public function removeOperation(Operation $operation): self
  240.     {
  241.         if ($this->operations->removeElement($operation)) {
  242.             // set the owning side to null (unless already changed)
  243.             if ($operation->getCompany() === $this) {
  244.                 $operation->setCompany(null);
  245.             }
  246.         }
  247.         return $this;
  248.     }
  249.     public function getCallbackUrl(): ?string
  250.     {
  251.         return $this->callbackUrl;
  252.     }
  253.     public function setCallbackUrl(string $callbackUrl): self
  254.     {
  255.         $this->callbackUrl $callbackUrl;
  256.         return $this;
  257.     }
  258.     public function getParamByCode($code): ?Param
  259.     {
  260.         foreach ($this->params as $p) {
  261.             if( $p->getCode() === $code ) {
  262.                 return $p;
  263.             }
  264.         }
  265.         return null;
  266.     }
  267.     /**
  268.      * @return Collection|Param[]
  269.      */
  270.     public function getParams(): Collection
  271.     {
  272.         return $this->params;
  273.     }
  274.     public function addParam(Param $param): self
  275.     {
  276.         if (!$this->params->contains($param)) {
  277.             $this->params[] = $param;
  278.             $param->setCompany($this);
  279.         }
  280.         return $this;
  281.     }
  282.     public function removeParam(Param $param): self
  283.     {
  284.         if ($this->params->removeElement($param)) {
  285.             // set the owning side to null (unless already changed)
  286.             if ($param->getCompany() === $this) {
  287.                 $param->setCompany(null);
  288.             }
  289.         }
  290.         return $this;
  291.     }
  292.     public function __toString()
  293.     {
  294.         return $this->name;
  295.     }
  296. }