<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Controller\Api\Deal\DeleteDealController;
use App\Controller\Api\Deal\GetNearestDealsController;
use App\Controller\Api\Deal\PostDealController;
use App\Repository\DealRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
#[ApiResource(
collectionOperations: [
'post' => [
'normalization_context' => [
'groups' => 'write:deal:collection:post',
],
'controller' => PostDealController::class,
'method' => 'POST',
'path' => '/deals',
],
'get_nearest_deals' => [
'controller' => GetNearestDealsController::class,
'method' => 'POST',
'path' => '/public/deals/nearest-deals',
],
'get' => [
'normalization_context' => [
'groups' => 'read:deal:collection:get',
],
'path' => '/public/deals',
],
'delete_companies_deals' => [
"method" => "DELETE",
"path" => "/deals/companies",
"controller" => DeleteDealController::class,
],
],
itemOperations: [
'get' => [
'normalization_context' => [
'groups' => 'read:deal:collection:get',
],
"path" => "/public/deals/{id}",
],
]
)]
#[ORM\Entity(repositoryClass: DealRepository::class)]
class Deal
{
const ACCESSIBILITY_PUBLIC = 'public';
const ACCESSIBILITY_CLUB = 'club';
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[Groups([
'read:deal:collection:get',
'write:deal:collection:post',
])]
private ?int $id = null;
#[ORM\Column(length: 255)]
#[Groups([
'read:deal:collection:get',
'write:deal:collection:post',
])]
private ?string $title = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
#[Groups([
'read:deal:collection:get',
'write:deal:collection:post',
])]
private ?string $description = null;
#[ORM\ManyToOne(cascade: ["persist"], inversedBy: 'deals')]
#[ORM\JoinColumn(nullable: false, onDelete: "CASCADE")]
#[Groups([
'read:deal:collection:get',
'write:deal:collection:post',
])]
#[Assert\NotBlank]
private ?Company $company = null;
#[ORM\Column(length: 255)]
#[Groups([
'read:deal:collection:get',
'write:deal:collection:post',
])]
private ?string $accessibility = null;
#[ORM\ManyToOne(inversedBy: 'deals')]
#[ORM\JoinColumn(nullable: true, onDelete: "SET NULL")]
#[Groups([
'read:deal:collection:get',
'write:deal:collection:post',
])]
private ?Club $club = null;
#[ORM\OneToMany(mappedBy: 'deal', targetEntity: Photo::class, cascade: ["persist"])]
private Collection $photos;
public function __construct()
{
$this->photos = new ArrayCollection();
$this->accessibility = self::ACCESSIBILITY_PUBLIC;
}
public static function getAccessibilityList(): array
{
return [
self::ACCESSIBILITY_PUBLIC => "L'ensemble des utilisateurs",
self::ACCESSIBILITY_CLUB => "Les salariƩs de la zone",
];
}
public function getAccessibilityLabel(): ?string
{
$list = self::getAccessibilityList();
return ($this->accessibility != null && array_key_exists($this->accessibility, $list) ? $list[$this->accessibility] : null);
}
#[Groups([
'read:deal:collection:get',
])]
public function getImageUrl(): string
{
$mainPhoto = $this->getMainPhoto();
if ($mainPhoto) {
return $mainPhoto->getImageLarge()->getUrl();
}
return $this->getCompany()->getLogoUrl();
}
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:deal:collection:get',
])]
public function getSmallImageUrl(): string
{
$mainPhoto = $this->getMainPhoto();
if ($mainPhoto) {
return $mainPhoto->getImageMedium()->getUrl();
}
return $this->getCompany()->getSmallLogoUrl();
}
#[Groups([
'read:deal:collection:get',
])]
public function getMediumImageUrl(): string
{
$mainPhoto = $this->getMainPhoto();
if ($mainPhoto) {
return $mainPhoto->getImageMedium()->getUrl();
}
return $this->getCompany()->getMediumLogoUrl();
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): self
{
$this->company = $company;
return $this;
}
public function getAccessibility(): ?string
{
return $this->accessibility;
}
public function setAccessibility(string $accessibility): self
{
$this->accessibility = $accessibility;
return $this;
}
public static function getAccessibilityStatus(): array
{
return [
self::ACCESSIBILITY_PUBLIC => 'public',
self::ACCESSIBILITY_CLUB => 'club',
];
}
public function getClub(): ?Club
{
return $this->club;
}
public function setClub(?Club $club): self
{
$this->club = $club;
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->setDeal($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->getDeal() === $this) {
$photo->setDeal(null);
}
}
return $this;
}
}