<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Controller\Api\Alert\GetNearestAlertsController;
use App\Controller\Api\Alert\PostAlertController;
use App\Repository\AlertRepository;
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;
#[ApiResource(
collectionOperations: [
'get_nearest_alerts' => [
'normalization_context' => [
'groups' => 'read:alert:collection:getNearestAlerts',
],
'controller' => GetNearestAlertsController::class,
'method' => 'POST',
'path' => '/public/alerts/nearest-alerts',
'read' => false,
],
'get',
'post_alert' => [
'normalization_context' => [
'groups' => 'write:alert',
],
'controller' => PostAlertController::class,
'method' => 'POST',
'path' => '/alerts',
'read' => false,
],
],
itemOperations: [
'get',
],
denormalizationContext: [
'groups' => 'write:alert',
],
normalizationContext: [
'groups' => 'read:alert',
]
)]
#[ORM\Entity(repositoryClass: AlertRepository::class)]
class Alert
{
const ALERT_STATUS_NEW = 'new';
const ALERT_STATUS_CLOSED = 'closed';
const ALERT_STATUS_VALIDATED = 'validated';
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[Groups([
'read:alert',
'write:alert',
])]
private ?int $id = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
#[Groups([
'read:alert',
'write:alert',
])]
private ?string $description = null;
#[ORM\Column(length: 255)]
#[Groups([
'read:alert',
])]
private ?string $status = null;
#[ORM\Column(type: Types::FLOAT, nullable: true)]
#[Groups([
'read:alert',
'write:alert',
])]
private ?float $lat = null;
#[ORM\Column(nullable: true)]
#[Groups([
'read:alert',
'write:alert',
])]
private ?float $lon = null;
#[ORM\ManyToOne(inversedBy: 'alerts')]
#[ORM\JoinColumn(nullable: true, onDelete: "SET NULL")]
#[Groups([
'read:alert',
'write:alert',
])]
private ?AlertCategory $alertCategory = null;
#[ORM\OneToMany(mappedBy: 'alert', targetEntity: Photo::class)]
private Collection $photos;
#[ORM\ManyToOne(inversedBy: 'alerts')]
#[ORM\JoinColumn(nullable: true, onDelete: "SET NULL")]
#[Groups([
'read:alert',
'write:alert',
])]
private ?Club $club = null;
public function __construct()
{
$this->status = self::ALERT_STATUS_NEW;
$this->photos = new ArrayCollection();
}
public static function getStatusList(): array
{
return [
self::ALERT_STATUS_NEW => 'Nouvelle',
self::ALERT_STATUS_CLOSED => 'Fermée',
self::ALERT_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::ALERT_STATUS_NEW => 'info',
self::ALERT_STATUS_CLOSED => 'danger',
self::ALERT_STATUS_VALIDATED => 'success',
default => null,
};
}
public function getImageUrl(): string
{
$mainPhoto = $this->getMainPhoto();
if ($mainPhoto) {
return $mainPhoto->getImageLarge()->getUrl();
}
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:alert',
])]
public function getSmallImageUrl(): string
{
$mainPhoto = $this->getMainPhoto();
if ($mainPhoto) {
return $mainPhoto->getImageSmall()->getUrl();
}
return 'https://localbeez.ri7.fr/assets/img/image_placeholder.jpg';
}
#[Groups([
'read:alert',
])]
public function getMediumImageUrl(): string
{
$mainPhoto = $this->getMainPhoto();
if ($mainPhoto) {
return $mainPhoto->getImageMedium()->getUrl();
}
return 'https://localbeez.ri7.fr/assets/img/image_placeholder.jpg';
}
public function getId(): ?int
{
return $this->id;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): static
{
$this->description = $description;
return $this;
}
public function getStatus(): ?string
{
return $this->status;
}
public function setStatus(string $status): static
{
$this->status = $status;
return $this;
}
public function getLat(): ?float
{
return $this->lat;
}
public function setLat(?float $lat): static
{
$this->lat = $lat;
return $this;
}
public function getLon(): ?float
{
return $this->lon;
}
public function setLon(?float $lon): static
{
$this->lon = $lon;
return $this;
}
public function getAlertCategory(): ?AlertCategory
{
return $this->alertCategory;
}
public function setAlertCategory(?AlertCategory $alertCategory): static
{
$this->alertCategory = $alertCategory;
return $this;
}
/**
* @return Collection<int, Photo>
*/
public function getPhotos(): Collection
{
return $this->photos;
}
public function addPhoto(Photo $photo): static
{
if (!$this->photos->contains($photo)) {
$this->photos->add($photo);
$photo->setAlert($this);
}
return $this;
}
public function removePhoto(Photo $photo): static
{
if ($this->photos->removeElement($photo)) {
// set the owning side to null (unless already changed)
if ($photo->getAlert() === $this) {
$photo->setAlert(null);
}
}
return $this;
}
public function getClub(): ?Club
{
return $this->club;
}
public function setClub(?Club $club): static
{
$this->club = $club;
return $this;
}
}