<?php
namespace App\Entity\Company;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Entity\Operation\Operation;
use App\Entity\Participation\Participation;
use App\Entity\User\User;
use App\Repository\Company\CompanyRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Ramsey\Uuid\Doctrine\UuidGenerator;
use Ramsey\Uuid\Lazy\LazyUuidFromString;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ApiResource(
* attributes={
* "normalization_context"={"groups"={"Company:io","Company:o"}},
* "denormalization_context"={"groups"={"Company:io","Company:i"}}
* },
* collectionOperations={
* "post"={
* "method"="POST",
* "access_control"="is_granted('ROLE_ADMIN_SOGEC')",
* },
* "get"={
* "method"="GET",
* "access_control"="is_granted('ROLE_ADMIN_SOGEC')",
* },
* },
* itemOperations={
* "put"={
* "method"="PUT",
* "access_control"="is_granted('ROLE_ADMIN_SOGEC')",
* },
* "get"={
* "method"="GET",
* "access_control"="is_granted('ROLE_ADMIN_SOGEC')",
* },
* }
* )
* @ORM\Entity(repositoryClass=CompanyRepository::class)
*/
class Company
{
/**
* Hook timestampable behavior
* updates createdAt, updatedAt fields
*/
use TimestampableEntity;
/**
* @ORM\Id
* @ORM\Column(type="uuid", unique=true)
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class=UuidGenerator::class)
* @ApiProperty(identifier=true)
* @Groups({"Provider:o", "Operation:o", "Coupon:o"})
*/
private $id;
/**
* @ORM\Column(type="string", length=50)
* @Assert\NotBlank()
*/
private $name;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\Url()
*/
private $logo;
/**
* @ORM\Column(type="string", length=255)
* @Gedmo\Slug(fields={"name"})
*/
private $slug;
/**
* @ORM\OneToMany(targetEntity=Credentials::class, mappedBy="company", orphanRemoval=true)
*/
private $credentials;
/**
* @ORM\OneToMany(targetEntity=User::class, mappedBy="company")
*/
private $users;
/**
* @ORM\OneToMany(targetEntity=Participation::class, mappedBy="company")
*/
private $participations;
/**
* @ORM\OneToMany(targetEntity=Operation::class, mappedBy="company")
*/
private $operations;
/**
* @ORM\Column(type="string", length=255)
*/
private $callbackUrl;
/**
* @ORM\OneToMany(targetEntity=Param::class, mappedBy="company", orphanRemoval=true)
*/
private $params;
public function __construct()
{
$this->credentials = new ArrayCollection();
$this->users = new ArrayCollection();
$this->participations = new ArrayCollection();
$this->operations = new ArrayCollection();
$this->params = new ArrayCollection();
}
public function getId(): ?LazyUuidFromString
{
return $this->id;
}
/**
* We add this for fixtures only
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getLogo(): ?string
{
return $this->logo;
}
public function setLogo(?string $logo): self
{
$this->logo = $logo;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
/**
* @return Collection|Credentials[]
*/
public function getCredentials(): Collection
{
return $this->credentials;
}
public function addCredential(Credentials $credential): self
{
if (!$this->credentials->contains($credential)) {
$this->credentials[] = $credential;
$credential->setCompany($this);
}
return $this;
}
public function removeCredential(Credentials $credential): self
{
if ($this->credentials->removeElement($credential)) {
// set the owning side to null (unless already changed)
if ($credential->getCompany() === $this) {
$credential->setCompany(null);
}
}
return $this;
}
/**
* @return Collection|User[]
*/
public function getUsers(): Collection
{
return $this->users;
}
public function addUser(User $user): self
{
if (!$this->users->contains($user)) {
$this->users[] = $user;
$user->setCompany($this);
}
return $this;
}
public function removeUser(User $user): self
{
if ($this->users->removeElement($user)) {
// set the owning side to null (unless already changed)
if ($user->getCompany() === $this) {
$user->setCompany(null);
}
}
return $this;
}
/**
* @return Collection|Participation[]
*/
public function getParticipations(): Collection
{
return $this->participations;
}
public function addParticipation(Participation $participation): self
{
if (!$this->participations->contains($participation)) {
$this->participations[] = $participation;
$participation->setCompany($this);
}
return $this;
}
public function removeParticipation(Participation $participation): self
{
if ($this->participations->removeElement($participation)) {
// set the owning side to null (unless already changed)
if ($participation->getCompany() === $this) {
$participation->setCompany(null);
}
}
return $this;
}
/**
* @return Collection|Operation[]
*/
public function getOperations(): Collection
{
return $this->operations;
}
public function addOperation(Operation $operation): self
{
if (!$this->operations->contains($operation)) {
$this->operations[] = $operation;
$operation->setCompany($this);
}
return $this;
}
public function removeOperation(Operation $operation): self
{
if ($this->operations->removeElement($operation)) {
// set the owning side to null (unless already changed)
if ($operation->getCompany() === $this) {
$operation->setCompany(null);
}
}
return $this;
}
public function getCallbackUrl(): ?string
{
return $this->callbackUrl;
}
public function setCallbackUrl(string $callbackUrl): self
{
$this->callbackUrl = $callbackUrl;
return $this;
}
public function getParamByCode($code): ?Param
{
foreach ($this->params as $p) {
if( $p->getCode() === $code ) {
return $p;
}
}
return null;
}
/**
* @return Collection|Param[]
*/
public function getParams(): Collection
{
return $this->params;
}
public function addParam(Param $param): self
{
if (!$this->params->contains($param)) {
$this->params[] = $param;
$param->setCompany($this);
}
return $this;
}
public function removeParam(Param $param): self
{
if ($this->params->removeElement($param)) {
// set the owning side to null (unless already changed)
if ($param->getCompany() === $this) {
$param->setCompany(null);
}
}
return $this;
}
public function __toString()
{
return $this->name;
}
}