src/Entity/Deal.php line 55

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiResource;
  4. use App\Controller\Api\Deal\DeleteDealController;
  5. use App\Controller\Api\Deal\GetNearestDealsController;
  6. use App\Controller\Api\Deal\PostDealController;
  7. use App\Repository\DealRepository;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\DBAL\Types\Types;
  11. use Doctrine\ORM\Mapping as ORM;
  12. use Symfony\Component\Serializer\Annotation\Groups;
  13. use Symfony\Component\Validator\Constraints as Assert;
  14. #[ApiResource(
  15.     collectionOperations: [
  16.         'post' => [
  17.             'normalization_context' => [
  18.                 'groups' => 'write:deal:collection:post',
  19.             ],
  20.             'controller' => PostDealController::class,
  21.             'method' => 'POST',
  22.             'path' => '/deals',
  23.         ],
  24.         'get_nearest_deals' => [
  25.             'controller' => GetNearestDealsController::class,
  26.             'method' => 'POST',
  27.             'path' => '/public/deals/nearest-deals',
  28.         ],
  29.         'get' => [
  30.             'normalization_context' => [
  31.                 'groups' => 'read:deal:collection:get',
  32.             ],
  33.             'path' => '/public/deals',
  34.         ],
  35.         'delete_companies_deals' => [
  36.             "method" => "DELETE",
  37.             "path" => "/deals/companies",
  38.             "controller" => DeleteDealController::class,
  39.         ],
  40.     ],
  41.     itemOperations: [
  42.         'get' => [
  43.             'normalization_context' => [
  44.                 'groups' => 'read:deal:collection:get',
  45.             ],
  46.             "path" => "/public/deals/{id}",
  47.         ],
  48.     ]
  49. )]
  50. #[ORM\Entity(repositoryClassDealRepository::class)]
  51. class Deal
  52. {
  53.     const ACCESSIBILITY_PUBLIC 'public';
  54.     const ACCESSIBILITY_CLUB 'club';
  55.     #[ORM\Id]
  56.     #[ORM\GeneratedValue]
  57.     #[ORM\Column]
  58.     #[Groups([
  59.         'read:deal:collection:get',
  60.         'write:deal:collection:post',
  61.     ])]
  62.     private ?int $id null;
  63.     #[ORM\Column(length255)]
  64.     #[Groups([
  65.         'read:deal:collection:get',
  66.         'write:deal:collection:post',
  67.     ])]
  68.     private ?string $title null;
  69.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  70.     #[Groups([
  71.         'read:deal:collection:get',
  72.         'write:deal:collection:post',
  73.     ])]
  74.     private ?string $description null;
  75.     #[ORM\ManyToOne(cascade: ["persist"], inversedBy'deals')]
  76.     #[ORM\JoinColumn(nullablefalseonDelete"CASCADE")]
  77.     #[Groups([
  78.         'read:deal:collection:get',
  79.         'write:deal:collection:post',
  80.     ])]
  81.     #[Assert\NotBlank]
  82.     private ?Company $company null;
  83.     #[ORM\Column(length255)]
  84.     #[Groups([
  85.         'read:deal:collection:get',
  86.         'write:deal:collection:post',
  87.     ])]
  88.     private ?string $accessibility null;
  89.     #[ORM\ManyToOne(inversedBy'deals')]
  90.     #[ORM\JoinColumn(nullabletrueonDelete"SET NULL")]
  91.     #[Groups([
  92.         'read:deal:collection:get',
  93.         'write:deal:collection:post',
  94.     ])]
  95.     private ?Club $club null;
  96.     #[ORM\OneToMany(mappedBy'deal'targetEntityPhoto::class, cascade: ["persist"])]
  97.     private Collection $photos;
  98.     public function __construct()
  99.     {
  100.         $this->photos = new ArrayCollection();
  101.         $this->accessibility self::ACCESSIBILITY_PUBLIC;
  102.     }
  103.     public static function getAccessibilityList(): array
  104.     {
  105.         return [
  106.             self::ACCESSIBILITY_PUBLIC => "L'ensemble des utilisateurs",
  107.             self::ACCESSIBILITY_CLUB => "Les salariĆ©s de la zone",
  108.         ];
  109.     }
  110.     public function getAccessibilityLabel(): ?string
  111.     {
  112.         $list self::getAccessibilityList();
  113.         return ($this->accessibility != null && array_key_exists($this->accessibility$list) ? $list[$this->accessibility] : null);
  114.     }
  115.     #[Groups([
  116.         'read:deal:collection:get',
  117.     ])]
  118.     public function getImageUrl(): string
  119.     {
  120.         $mainPhoto $this->getMainPhoto();
  121.         if ($mainPhoto) {
  122.             return $mainPhoto->getImageLarge()->getUrl();
  123.         }
  124.         return $this->getCompany()->getLogoUrl();
  125.     }
  126.     public function getMainPhoto()
  127.     {
  128.         foreach ($this->photos as $p) {
  129.             if ($p->isMain()) {
  130.                 return $p;
  131.             }
  132.         }
  133.         if (sizeof($this->photos) > 0) {
  134.             return $this->photos[0];
  135.         }
  136.         return false;
  137.     }
  138.     #[Groups([
  139.         'read:deal:collection:get',
  140.     ])]
  141.     public function getSmallImageUrl(): string
  142.     {
  143.         $mainPhoto $this->getMainPhoto();
  144.         if ($mainPhoto) {
  145.             return $mainPhoto->getImageMedium()->getUrl();
  146.         }
  147.         return $this->getCompany()->getSmallLogoUrl();
  148.     }
  149.     #[Groups([
  150.         'read:deal:collection:get',
  151.     ])]
  152.     public function getMediumImageUrl(): string
  153.     {
  154.         $mainPhoto $this->getMainPhoto();
  155.         if ($mainPhoto) {
  156.             return $mainPhoto->getImageMedium()->getUrl();
  157.         }
  158.         return $this->getCompany()->getMediumLogoUrl();
  159.     }
  160.     public function getId(): ?int
  161.     {
  162.         return $this->id;
  163.     }
  164.     public function getTitle(): ?string
  165.     {
  166.         return $this->title;
  167.     }
  168.     public function setTitle(string $title): self
  169.     {
  170.         $this->title $title;
  171.         return $this;
  172.     }
  173.     public function getDescription(): ?string
  174.     {
  175.         return $this->description;
  176.     }
  177.     public function setDescription(?string $description): self
  178.     {
  179.         $this->description $description;
  180.         return $this;
  181.     }
  182.     public function getCompany(): ?Company
  183.     {
  184.         return $this->company;
  185.     }
  186.     public function setCompany(?Company $company): self
  187.     {
  188.         $this->company $company;
  189.         return $this;
  190.     }
  191.     public function getAccessibility(): ?string
  192.     {
  193.         return $this->accessibility;
  194.     }
  195.     public function setAccessibility(string $accessibility): self
  196.     {
  197.         $this->accessibility $accessibility;
  198.         return $this;
  199.     }
  200.     public static function getAccessibilityStatus(): array
  201.     {
  202.         return [
  203.             self::ACCESSIBILITY_PUBLIC => 'public',
  204.             self::ACCESSIBILITY_CLUB => 'club',
  205.         ];
  206.     }
  207.     public function getClub(): ?Club
  208.     {
  209.         return $this->club;
  210.     }
  211.     public function setClub(?Club $club): self
  212.     {
  213.         $this->club $club;
  214.         return $this;
  215.     }
  216.     /**
  217.      * @return Collection<int, Photo>
  218.      */
  219.     public function getPhotos(): Collection
  220.     {
  221.         return $this->photos;
  222.     }
  223.     public function addPhoto(Photo $photo): self
  224.     {
  225.         if (!$this->photos->contains($photo)) {
  226.             $this->photos->add($photo);
  227.             $photo->setDeal($this);
  228.         }
  229.         return $this;
  230.     }
  231.     public function removePhoto(Photo $photo): self
  232.     {
  233.         if ($this->photos->removeElement($photo)) {
  234.             // set the owning side to null (unless already changed)
  235.             if ($photo->getDeal() === $this) {
  236.                 $photo->setDeal(null);
  237.             }
  238.         }
  239.         return $this;
  240.     }
  241. }