<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Controller\Api\Collaborator\DeleteCollaboratorsFromCompany;
use App\Controller\Api\Collaborator\GetCollaboratorsFromCompanyController;
use App\Controller\Api\Company\AddCollaboratorController;
use App\Controller\Api\Company\AddCompanyRequestController;
use App\Controller\Api\Company\AddCompanyWithClaimingRequestController;
use App\Controller\Api\Company\AddManagerFromCompanyController;
use App\Controller\Api\Company\GetCompanyByCodeController;
use App\Controller\Api\Company\GetDealsFromCompanyController;
use App\Controller\Api\Company\GetNearestCompaniesController;
use App\Controller\Api\Company\PutCompaniesController;
use App\Controller\Api\Company\UpdateActivityController;
use App\Controller\Api\Company\UpdateAddressController;
use App\Repository\CompanyRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Component\Serializer\Annotation\Groups;
#[ORM\Entity(repositoryClass: CompanyRepository::class)]
#[ApiResource(
collectionOperations: [
'get_nearest_companies' => [
'normalization_context' => [
'groups' => 'read:company:collection:getNearestCompanies',
],
'controller' => GetNearestCompaniesController::class,
'method' => 'POST',
'path' => '/public/nearest-companies',
'read' => false,
],
'get_company_by_code' => [
'controller' => GetCompanyByCodeController::class,
'method' => 'POST',
'path' => '/companies/get-company-by-code',
],
'add_collaborator' => [
'controller' => AddCollaboratorController::class,
'method' => 'POST',
'path' => '/companies/add-collaborator',
],
'add_company_with_claiming_request' => [
'controller' => AddCompanyWithClaimingRequestController::class,
'method' => 'POST',
'path' => '/companies/add-company-with-claiming-request',
],
'delete_collaborators_from_conpany' => [
'controller' => DeleteCollaboratorsFromCompany::class,
'method' => 'DELETE',
'path' => '/companies/{id}/delete_collaborators',
],
],
itemOperations: [
'get' => [
'normalization_context' => [
'groups' => 'read:company:collection:get',
],
'path' => '/public/companies/{id}',
],
'update_address' => [
'normalization_context' => [
'groups' => 'read:company:collection:get',
],
'method' => "PUT",
'controller' => UpdateAddressController::class,
'path' => '/companies/{id}/update-address',
],
'update_activity' => [
'normalization_context' => [
'groups' => 'read:company:collection:get',
],
'method' => "PUT",
'controller' => UpdateActivityController::class,
'path' => '/companies/{id}/update-activity',
],
"put" => [
"method" => "PUT",
"path" => "/companies/{id}",
'controller' => PutCompaniesController::class,
'denormalization_context' => [
'groups' => 'write:company:item',
],
],
'delete',
'get_collaborators' => [
'normalization_context' => [
'groups' => 'read:company:collection:getCollaborators',
],
'controller' => GetCollaboratorsFromCompanyController::class,
'method' => 'POST',
'path' => '/companies/{id}/collaborators',
],
'post_manager' => [
'controller' => AddManagerFromCompanyController::class,
'method' => 'POST',
'path' => '/companies/{id}/add-manager',
],
'get_deals' => [
'controller' => GetDealsFromCompanyController::class,
'method' => 'POST',
'path' => '/public/companies/{id}/deals',
],
],
normalizationContext: [
'groups' => 'read:company:collection',
]
)]
class Company
{
const STATUS_NEW = 'new';
const STATUS_CLAIMED = 'claimed';
const STATUS_VALIDATED = 'validated';
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[Groups([
'read:club:collection:getCompanies',
'read:user:item:get',
'read:user:collection:get',
'read:club:collection:getUsers',
'read:user:item:me',
'read:company:collection:getCollaborators',
'read:company:collection:get',
'read:deal:collection:get',
"read:claimingRequest:collection:getUserCompaniesClaimed",
'read:user:item:user:companies:management',
])]
private ?int $id = null;
use TimestampableEntity;
#[ORM\Column(length: 255)]
#[Groups([
'read:club:collection:getCompanies',
'read:user:item:get',
'read:user:collection:get',
'read:club:collection:getUsers',
'read:user:item:me',
'read:company:collection:getCollaborators',
'read:company:collection:get',
'write:company:item',
'read:deal:collection:get',
"read:claimingRequest:collection:getUserCompaniesClaimed",
'read:user:item:user:companies:management',
'write:deal:collection:post',
])]
private ?string $name = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
#[Groups([
'read:club:collection:getCompanies',
'read:company:collection:get',
'write:company:item',
])]
private ?string $description = null;
#[ORM\Column(length: 255, nullable: true)]
#[Groups([
'read:club:collection:getCompanies',
'read:company:collection:get',
'write:company:item',
])]
private ?string $website = null;
#[ORM\OneToMany(mappedBy: 'company', targetEntity: Collaborator::class, cascade: ["persist"])]
#[Groups([
'read:club:collection:getCompanies',
'read:company:collection:get',
])]
private Collection $collaborators;
#[ORM\OneToMany(mappedBy: 'company', targetEntity: Membership::class, cascade: ["persist"])]
#[Groups([
'read:club:collection:getCompanies',
'read:company:collection:get',
'read:user:item:me',
])]
private Collection $memberships;
#[ORM\OneToMany(mappedBy: 'company', targetEntity: Photo::class, cascade: ["persist"])]
#[Groups([
'read:club:collection:getCompanies',
'read:company:collection:get',
])]
private Collection $photos;
#[ORM\ManyToOne(cascade: ['persist'], inversedBy: 'companies')]
#[ORM\JoinColumn(nullable: true, onDelete: "SET NULL")]
#[Groups([
'read:club:collection:getCompanies',
'read:company:collection:get',
'read:user:item:me',
'read:user:item:user:companies:management',
])]
private ?Activity $activity = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $idNumber = null;
#[ORM\Column(length: 255, nullable: true)]
#[Groups([
'read:club:collection:getCompanies',
'read:company:collection:get',
'write:company:item',
])]
private ?string $contactEmail = null;
#[ORM\Column(length: 255, nullable: true)]
#[Groups([
'read:club:collection:getCompanies',
'read:company:collection:get',
'write:company:item',
])]
private ?string $contactPhone = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $rawAddress = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $rawCity = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $rawManager = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $rawStaff = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $fti = null;
#[Groups([
'read:club:collection:getCompanies',
'read:company:collection:get',
])]
private ?float $distance = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $rawPostalCode = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $rawLogoUrl = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $rawDescription = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $rawTitle = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $rawSitename = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $slug = null;
#[ORM\Column(nullable: true)]
private ?int $mauticId = null;
#[ORM\Column]
#[Groups([
'read:club:collection:getCompanies',
'read:company:collection:get',
])]
private ?bool $verified = null;
#[ORM\ManyToOne(inversedBy: 'companies')]
#[ORM\JoinColumn(nullable: true, onDelete: "SET NULL")]
#[Groups([
'read:club:collection:getCompanies',
'read:company:collection:get',
'read:user:item:me',
"read:claimingRequest:collection:getUserCompaniesClaimed",
'read:user:item:user:companies:management',
])]
private ?Address $address = null;
#[ORM\ManyToMany(targetEntity: Keyword::class, inversedBy: 'companies', cascade: ['persist'])]
#[Groups([
'read:club:collection:getCompanies',
'read:company:collection:get',
'write:company:item',
])]
private Collection $keywords;
#[ORM\ManyToOne(inversedBy: 'companies')]
#[ORM\JoinColumn(nullable: true, onDelete: "SET NULL")]
#[Groups([
'read:club:collection:getCompanies',
'read:company:collection:get',
])]
private ?User $creator = null;
#[ORM\Column(length: 255, nullable: true)]
#[Groups([
'read:club:collection:getCompanies',
'read:user:item:get',
'read:user:collection:get',
'read:club:collection:getUsers',
'read:user:item:me',
'read:company:collection:getCollaborators',
'read:company:collection:get',
'write:company:item',
'read:deal:collection:get',
])]
private ?string $status = null;
#[ORM\OneToMany(mappedBy: 'company', targetEntity: ClaimingRequest::class)]
#[Groups([
'read:club:collection:getCompanies',
'read:company:collection:get',
'read:user:item:user:companies:management',
])]
private Collection $claimingRequests;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $webFti = null;
#[ORM\OneToMany(mappedBy: 'company', targetEntity: Deal::class)]
private Collection $deals;
#[ORM\Column(length: 255, nullable: true)]
#[Groups([
'read:club:collection:getCompanies',
'read:company:collection:get',
])]
private ?string $code = null;
#[ORM\Column(nullable: true)]
private ?bool $raw404 = null;
#[ORM\Column(nullable: true)]
private ?bool $rawAddressKo = null;
#[ORM\Column(nullable: true)]
#[Groups([
'read:club:collection:getCompanies',
'read:company:collection:get',
'write:company:item',
])]
private ?bool $visible = null;
public function __construct()
{
$this->collaborators = new ArrayCollection();
$this->memberships = new ArrayCollection();
$this->photos = new ArrayCollection();
$this->keywords = new ArrayCollection();
$this->claimingRequests = new ArrayCollection();
$this->deals = new ArrayCollection();
$this->verified = false;
$this->visible = true;
}
public function __toString(): string
{
return $this->name;
}
public function getMembershipsCount(): ?int
{
return count($this->memberships);
}
public static function getStatusList(): array
{
return [
self::STATUS_NEW => 'Nouvelle',
self::STATUS_CLAIMED => 'Revendiquée',
self::STATUS_VALIDATED => 'Validée',
];
}
public function getStatusLabel(): ?string
{
$list = self::getStatusList();
return ($this->status != null && array_key_exists($this->status, $list) ? $list[$this->status] : null);
}
public function getStatusClass(): ?string
{
return match ($this->status) {
self::STATUS_NEW => 'info',
self::STATUS_CLAIMED => 'primary',
self::STATUS_VALIDATED => 'success',
default => null,
};
}
public function getOwner(): ?array
{
$owners = [];
foreach ($this->collaborators as $collaborator) {
if ($collaborator->isOwner()) {
$owners[] = $collaborator->getUser();
}
}
return $owners ?: null;
}
#[Groups([
'read:club:collection:getCompanies',
'read:company:collection:get',
])]
public function getActiveClub(): ?Club
{
foreach ($this->memberships as $membership) {
if ($membership->getStatus() === Membership::STATUS_ACTIVE) {
return $membership->getClub();
}
}
return null;
}
#[Groups([
'read:company:collection:get',
'read:club:collection:getCompanies',
'read:company:collection:get',
])]
public function getLogoUrl(): string
{
$mainPhoto = $this->getMainPhoto();
if ($mainPhoto) {
return $mainPhoto->getImageLarge()->getUrl();
}
if ($this->rawLogoUrl) {
return $this->rawLogoUrl;
}
return 'https://localbeez.ri7.fr/assets/img/image_placeholder.jpg';
}
public function getMainPhoto()
{
foreach ($this->photos as $p) {
if ($p->isMain()) {
return $p;
}
}
if (sizeof($this->photos) > 0) {
return $this->photos[0];
}
return false;
}
#[Groups([
'read:company:collection:get',
'read:club:collection:getCompanies',
'read:user:item:me',
'read:user:item:user:companies:management',
'write:deal:collection:post',
])]
public function getSmallLogoUrl(): string
{
$mainPhoto = $this->getMainPhoto();
if ($mainPhoto) {
return $mainPhoto->getImageMedium()->getUrl();
}
if ($this->rawLogoUrl) {
return $this->rawLogoUrl;
}
return 'https://localbeez.ri7.fr/assets/img/image_placeholder.jpg';
}
#[Groups([
'read:company:collection:get',
'read:club:collection:getCompanies',
'read:company:collection:get',
'read:user:item:me',
'read:deal:collection:get',
"read:claimingRequest:collection:getUserCompaniesClaimed",
])]
public function getMediumLogoUrl(): string
{
$mainPhoto = $this->getMainPhoto();
if ($mainPhoto) {
return $mainPhoto->getImageMedium()->getUrl();
}
if ($this->rawLogoUrl) {
return $this->rawLogoUrl;
}
return 'https://localbeez.ri7.fr/assets/img/image_placeholder.jpg';
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getWebsite(): ?string
{
return $this->website;
}
public function setWebsite(?string $website): self
{
$this->website = $website;
return $this;
}
/**
* @return Collection<int, Collaborator>
*/
public function getCollaborators(): Collection
{
return $this->collaborators;
}
public function addCollaborator(Collaborator $collaborator): self
{
if (!$this->collaborators->contains($collaborator)) {
$this->collaborators->add($collaborator);
$collaborator->setCompany($this);
}
return $this;
}
public function removeCollaborator(Collaborator $collaborator): self
{
if ($this->collaborators->removeElement($collaborator)) {
// set the owning side to null (unless already changed)
if ($collaborator->getCompany() === $this) {
$collaborator->setCompany(null);
}
}
return $this;
}
/**
* @return Collection<int, Membership>
*/
public function getMemberships(): Collection
{
return $this->memberships;
}
public function addMembership(Membership $membership): self
{
if (!$this->memberships->contains($membership)) {
$this->memberships->add($membership);
$membership->setCompany($this);
}
return $this;
}
public function removeMembership(Membership $membership): self
{
if ($this->memberships->removeElement($membership)) {
// set the owning side to null (unless already changed)
if ($membership->getCompany() === $this) {
$membership->setCompany(null);
}
}
return $this;
}
/**
* @return Collection<int, Photo>
*/
public function getPhotos(): Collection
{
return $this->photos;
}
public function addPhoto(Photo $photo): self
{
if (!$this->photos->contains($photo)) {
$this->photos->add($photo);
$photo->setCompany($this);
}
return $this;
}
public function removePhoto(Photo $photo): self
{
if ($this->photos->removeElement($photo)) {
// set the owning side to null (unless already changed)
if ($photo->getCompany() === $this) {
$photo->setCompany(null);
}
}
return $this;
}
public function getActivity(): ?Activity
{
return $this->activity;
}
public function setActivity(?Activity $activity): self
{
$this->activity = $activity;
return $this;
}
public function getIdNumber(): ?string
{
return $this->idNumber;
}
public function setIdNumber(?string $idNumber): self
{
$this->idNumber = $idNumber;
return $this;
}
public function getContactEmail(): ?string
{
return $this->contactEmail;
}
public function setContactEmail(?string $contactEmail): self
{
$this->contactEmail = $contactEmail;
return $this;
}
public function getContactPhone(): ?string
{
return $this->contactPhone;
}
public function setContactPhone(?string $contactPhone): self
{
$this->contactPhone = $contactPhone;
return $this;
}
public function getRawAddress(): ?string
{
return $this->rawAddress;
}
public function setRawAddress(?string $rawAddress): self
{
$this->rawAddress = $rawAddress;
return $this;
}
public function getRawCity(): ?string
{
return $this->rawCity;
}
public function setRawCity(?string $rawCity): self
{
$this->rawCity = $rawCity;
return $this;
}
public function getRawManager(): ?string
{
return $this->rawManager;
}
public function setRawManager(?string $rawManager): self
{
$this->rawManager = $rawManager;
return $this;
}
public function getRawStaff(): ?string
{
return $this->rawStaff;
}
public function setRawStaff(?string $rawStaff): self
{
$this->rawStaff = $rawStaff;
return $this;
}
public function getFti(): ?string
{
return $this->fti;
}
public function setFti(?string $fti): self
{
$this->fti = $fti;
return $this;
}
public function getDistance(): ?float
{
return $this->distance;
}
public function setDistance(?float $distance): self
{
$this->distance = $distance;
return $this;
}
public function getRawPostalCode(): ?string
{
return $this->rawPostalCode;
}
public function setRawPostalCode(?string $rawPostalCode): self
{
$this->rawPostalCode = $rawPostalCode;
return $this;
}
public function getRawLogoUrl(): ?string
{
return $this->rawLogoUrl;
}
public function setRawLogoUrl(?string $rawLogoUrl): self
{
$this->rawLogoUrl = $rawLogoUrl;
return $this;
}
public function getRawDescription(): ?string
{
return $this->rawDescription;
}
public function setRawDescription(?string $rawDescription): self
{
$this->rawDescription = $rawDescription;
return $this;
}
public function getRawTitle(): ?string
{
return $this->rawTitle;
}
public function setRawTitle(?string $rawTitle): self
{
$this->rawTitle = $rawTitle;
return $this;
}
public function getRawSitename(): ?string
{
return $this->rawSitename;
}
public function setRawSitename(?string $rawSitename): self
{
$this->rawSitename = $rawSitename;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(?string $slug): self
{
$this->slug = $slug;
return $this;
}
public function getMauticId(): ?int
{
return $this->mauticId;
}
public function setMauticId(?int $mauticId): self
{
$this->mauticId = $mauticId;
return $this;
}
public function isVerified(): ?bool
{
return $this->verified;
}
public function setVerified(bool $verified): self
{
$this->verified = $verified;
return $this;
}
public function getAddress(): ?Address
{
return $this->address;
}
public function setAddress(?Address $address): self
{
$this->address = $address;
return $this;
}
/**
* @return Collection|array
*/
public function getKeywords(): Collection|array
{
return $this->keywords->toArray();
}
public function addKeyword(Keyword $keyword): self
{
if (!$this->keywords->contains($keyword)) {
$this->keywords->add($keyword);
}
return $this;
}
public function removeKeyword(Keyword $keyword): self
{
$this->keywords->removeElement($keyword);
return $this;
}
public function getCreator(): ?User
{
return $this->creator;
}
public function setCreator(?User $creator): self
{
$this->creator = $creator;
return $this;
}
public function getStatus(): ?string
{
return $this->status;
}
public function setStatus(?string $status): self
{
$this->status = $status;
return $this;
}
/**
* @return Collection<int, ClaimingRequest>
*/
public function getClaimingRequests(): Collection
{
return $this->claimingRequests;
}
public function addClaimingRequest(ClaimingRequest $claimingRequest): self
{
if (!$this->claimingRequests->contains($claimingRequest)) {
$this->claimingRequests->add($claimingRequest);
$claimingRequest->setCompany($this);
}
return $this;
}
public function removeClaimingRequest(ClaimingRequest $claimingRequest): self
{
if ($this->claimingRequests->removeElement($claimingRequest)) {
// set the owning side to null (unless already changed)
if ($claimingRequest->getCompany() === $this) {
$claimingRequest->setCompany(null);
}
}
return $this;
}
public function getWebFti(): ?string
{
return $this->webFti;
}
public function setWebFti(?string $webFti): self
{
$this->webFti = $webFti;
return $this;
}
/**
* @return Collection<int, Deal>
*/
#[Groups([
'read:company:collection:get',
])]
public function getDeals(): Collection
{
return $this->deals;
}
public function addDeal(Deal $deal): self
{
if (!$this->deals->contains($deal)) {
$this->deals->add($deal);
$deal->setCompany($this);
}
return $this;
}
public function removeDeal(Deal $deal): self
{
if ($this->deals->removeElement($deal)) {
// set the owning side to null (unless already changed)
if ($deal->getCompany() === $this) {
$deal->setCompany(null);
}
}
return $this;
}
public function getCode(): ?string
{
return $this->code;
}
public function setCode(?string $code): self
{
$this->code = $code;
return $this;
}
public function isRaw404(): ?bool
{
return $this->raw404;
}
public function setRaw404(?bool $raw404): static
{
$this->raw404 = $raw404;
return $this;
}
public function isRawAddressKo(): ?bool
{
return $this->rawAddressKo;
}
public function setRawAddressKo(?bool $rawAddressKo): static
{
$this->rawAddressKo = $rawAddressKo;
return $this;
}
public function isVisible(): ?bool
{
return $this->visible;
}
public function setVisible(?bool $visible): static
{
$this->visible = $visible;
return $this;
}
}