src/Entity/Alert.php line 49

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiResource;
  4. use App\Controller\Api\Alert\GetNearestAlertsController;
  5. use App\Controller\Api\Alert\PostAlertController;
  6. use App\Repository\AlertRepository;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Doctrine\DBAL\Types\Types;
  10. use Doctrine\ORM\Mapping as ORM;
  11. use Symfony\Component\Serializer\Annotation\Groups;
  12. #[ApiResource(
  13.     collectionOperations: [
  14.         'get_nearest_alerts' => [
  15.             'normalization_context' => [
  16.                 'groups' => 'read:alert:collection:getNearestAlerts',
  17.             ],
  18.             'controller' => GetNearestAlertsController::class,
  19.             'method' => 'POST',
  20.             'path' => '/public/alerts/nearest-alerts',
  21.             'read' => false,
  22.         ],
  23.         'get',
  24.         'post_alert' => [
  25.             'normalization_context' => [
  26.                 'groups' => 'write:alert',
  27.             ],
  28.             'controller' => PostAlertController::class,
  29.             'method' => 'POST',
  30.             'path' => '/alerts',
  31.             'read' => false,
  32.         ],
  33.     ],
  34.     itemOperations: [
  35.         'get',
  36.     ],
  37.     denormalizationContext: [
  38.         'groups' => 'write:alert',
  39.     ],
  40.     normalizationContext: [
  41.         'groups' => 'read:alert',
  42.     ]
  43. )]
  44. #[ORM\Entity(repositoryClassAlertRepository::class)]
  45. class Alert
  46. {
  47.     const ALERT_STATUS_NEW 'new';
  48.     const ALERT_STATUS_CLOSED 'closed';
  49.     const ALERT_STATUS_VALIDATED 'validated';
  50.     #[ORM\Id]
  51.     #[ORM\GeneratedValue]
  52.     #[ORM\Column]
  53.     #[Groups([
  54.         'read:alert',
  55.         'write:alert',
  56.     ])]
  57.     private ?int $id null;
  58.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  59.     #[Groups([
  60.         'read:alert',
  61.         'write:alert',
  62.     ])]
  63.     private ?string $description null;
  64.     #[ORM\Column(length255)]
  65.     #[Groups([
  66.         'read:alert',
  67.     ])]
  68.     private ?string $status null;
  69.     #[ORM\Column(typeTypes::FLOATnullabletrue)]
  70.     #[Groups([
  71.         'read:alert',
  72.         'write:alert',
  73.     ])]
  74.     private ?float $lat null;
  75.     #[ORM\Column(nullabletrue)]
  76.     #[Groups([
  77.         'read:alert',
  78.         'write:alert',
  79.     ])]
  80.     private ?float $lon null;
  81.     #[ORM\ManyToOne(inversedBy'alerts')]
  82.     #[ORM\JoinColumn(nullabletrueonDelete"SET NULL")]
  83.     #[Groups([
  84.         'read:alert',
  85.         'write:alert',
  86.     ])]
  87.     private ?AlertCategory $alertCategory null;
  88.     #[ORM\OneToMany(mappedBy'alert'targetEntityPhoto::class)]
  89.     private Collection $photos;
  90.     #[ORM\ManyToOne(inversedBy'alerts')]
  91.     #[ORM\JoinColumn(nullabletrueonDelete"SET NULL")]
  92.     #[Groups([
  93.         'read:alert',
  94.         'write:alert',
  95.     ])]
  96.     private ?Club $club null;
  97.     public function __construct()
  98.     {
  99.         $this->status self::ALERT_STATUS_NEW;
  100.         $this->photos = new ArrayCollection();
  101.     }
  102.     public static function getStatusList(): array
  103.     {
  104.         return [
  105.             self::ALERT_STATUS_NEW => 'Nouvelle',
  106.             self::ALERT_STATUS_CLOSED => 'Fermée',
  107.             self::ALERT_STATUS_VALIDATED => 'Validée',
  108.         ];
  109.     }
  110.     public function getStatusLabel(): ?string
  111.     {
  112.         $list self::getStatusList();
  113.         return ($this->status != null && array_key_exists($this->status$list) ? $list[$this->status] : null);
  114.     }
  115.     public function getStatusClass(): ?string
  116.     {
  117.         return match ($this->status) {
  118.             self::ALERT_STATUS_NEW => 'info',
  119.             self::ALERT_STATUS_CLOSED => 'danger',
  120.             self::ALERT_STATUS_VALIDATED => 'success',
  121.             default => null,
  122.         };
  123.     }
  124.     public function getImageUrl(): string
  125.     {
  126.         $mainPhoto $this->getMainPhoto();
  127.         if ($mainPhoto) {
  128.             return $mainPhoto->getImageLarge()->getUrl();
  129.         }
  130.         return 'https://localbeez.ri7.fr/assets/img/image_placeholder.jpg';
  131.     }
  132.     public function getMainPhoto()
  133.     {
  134.         foreach ($this->photos as $p) {
  135.             if ($p->isMain()) {
  136.                 return $p;
  137.             }
  138.         }
  139.         if (sizeof($this->photos) > 0) {
  140.             return $this->photos[0];
  141.         }
  142.         return false;
  143.     }
  144.     #[Groups([
  145.         'read:alert',
  146.     ])]
  147.     public function getSmallImageUrl(): string
  148.     {
  149.         $mainPhoto $this->getMainPhoto();
  150.         if ($mainPhoto) {
  151.             return $mainPhoto->getImageSmall()->getUrl();
  152.         }
  153.         return 'https://localbeez.ri7.fr/assets/img/image_placeholder.jpg';
  154.     }
  155.     #[Groups([
  156.         'read:alert',
  157.     ])]
  158.     public function getMediumImageUrl(): string
  159.     {
  160.         $mainPhoto $this->getMainPhoto();
  161.         if ($mainPhoto) {
  162.             return $mainPhoto->getImageMedium()->getUrl();
  163.         }
  164.         return 'https://localbeez.ri7.fr/assets/img/image_placeholder.jpg';
  165.     }
  166.     public function getId(): ?int
  167.     {
  168.         return $this->id;
  169.     }
  170.     public function getDescription(): ?string
  171.     {
  172.         return $this->description;
  173.     }
  174.     public function setDescription(?string $description): static
  175.     {
  176.         $this->description $description;
  177.         return $this;
  178.     }
  179.     public function getStatus(): ?string
  180.     {
  181.         return $this->status;
  182.     }
  183.     public function setStatus(string $status): static
  184.     {
  185.         $this->status $status;
  186.         return $this;
  187.     }
  188.     public function getLat(): ?float
  189.     {
  190.         return $this->lat;
  191.     }
  192.     public function setLat(?float $lat): static
  193.     {
  194.         $this->lat $lat;
  195.         return $this;
  196.     }
  197.     public function getLon(): ?float
  198.     {
  199.         return $this->lon;
  200.     }
  201.     public function setLon(?float $lon): static
  202.     {
  203.         $this->lon $lon;
  204.         return $this;
  205.     }
  206.     public function getAlertCategory(): ?AlertCategory
  207.     {
  208.         return $this->alertCategory;
  209.     }
  210.     public function setAlertCategory(?AlertCategory $alertCategory): static
  211.     {
  212.         $this->alertCategory $alertCategory;
  213.         return $this;
  214.     }
  215.     /**
  216.      * @return Collection<int, Photo>
  217.      */
  218.     public function getPhotos(): Collection
  219.     {
  220.         return $this->photos;
  221.     }
  222.     public function addPhoto(Photo $photo): static
  223.     {
  224.         if (!$this->photos->contains($photo)) {
  225.             $this->photos->add($photo);
  226.             $photo->setAlert($this);
  227.         }
  228.         return $this;
  229.     }
  230.     public function removePhoto(Photo $photo): static
  231.     {
  232.         if ($this->photos->removeElement($photo)) {
  233.             // set the owning side to null (unless already changed)
  234.             if ($photo->getAlert() === $this) {
  235.                 $photo->setAlert(null);
  236.             }
  237.         }
  238.         return $this;
  239.     }
  240.     public function getClub(): ?Club
  241.     {
  242.         return $this->club;
  243.     }
  244.     public function setClub(?Club $club): static
  245.     {
  246.         $this->club $club;
  247.         return $this;
  248.     }
  249. }