src/Entity/Promotion/UniquePromotionCode/UniquePromotion.php line 56

  1. <?php
  2. /*
  3.  * This file is part of the Pellipop project.
  4.  *
  5.  * (c) Mobizel
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace App\Entity\Promotion\UniquePromotionCode;
  12. use App\Controller\DownloadUniquePromotionCode;
  13. use App\Entity\IdentifiableTrait;
  14. use App\Entity\Promotion\PromotionCode;
  15. use Doctrine\Common\Collections\ArrayCollection;
  16. use Doctrine\Common\Collections\Collection;
  17. use Doctrine\ORM\Mapping as ORM;
  18. use Sylius\Component\Resource\Annotation\SyliusCrudRoutes;
  19. use Sylius\Component\Resource\Annotation\SyliusRoute;
  20. use Sylius\Component\Resource\Model\ResourceInterface;
  21. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  22. use Symfony\Component\Validator\Constraints as Assert;
  23. #[ORM\Entity]
  24. #[UniqueEntity('name')]
  25. #[ORM\Table(name'app_unique_promotion')]
  26. #[SyliusCrudRoutes(
  27.     alias'app.unique_promotion',
  28.     path'/admin/unique-promotion',
  29.     section'backend',
  30.     templates'backend/unique_promotion',
  31.     grid'app_backend_unique_promotion',
  32.     only: ['index''show''create'],
  33.     vars: [
  34.     'all' => [
  35.         'subheader' => 'app.ui.manage_unique_promotions',
  36.         'templates' => [
  37.             'form' => 'backend/unique_promotion/_form.html.twig',
  38.         ],
  39.     ],
  40.     'index' => [
  41.         'icon' => 'percent',
  42.     ],
  43. ],
  44. )]
  45. #[SyliusRoute(
  46.     name'app_backend_unique_promotion_download',
  47.     path'/admin/unique-promotion/{id}/download',
  48.     methods: ['GET'],
  49.     controllerDownloadUniquePromotionCode::class,
  50. )]
  51. class UniquePromotion implements ResourceInterface
  52. {
  53.     use IdentifiableTrait;
  54.     #[ORM\Column(type'string'nullabletrue)]
  55.     #[Assert\Length(min3max10), Assert\Regex('/^[\w-]*$/')]
  56.     private ?string $prefix null;
  57.     #[ORM\Column(type'string')]
  58.     #[Assert\NotBlankAssert\Length(min3max50)]
  59.     private ?string $name null;
  60.     #[ORM\Column(type'integer')]
  61.     #[Assert\NotBlank]
  62.     private ?int $credits null;
  63.     #[ORM\Column(type'integer')]
  64.     #[Assert\NotBlank]
  65.     private ?int $number null;
  66.     #[ORM\OneToMany(mappedBy'uniquePromotion'targetEntityPromotionCode::class, cascade: ['persist'])]
  67.     private ?Collection $promotionCodes;
  68.     public function __construct()
  69.     {
  70.         $this->promotionCodes = new ArrayCollection();
  71.     }
  72.     public function getPrefix(): ?string
  73.     {
  74.         return $this->prefix;
  75.     }
  76.     public function setPrefix(?string $prefix): void
  77.     {
  78.         $this->prefix $prefix;
  79.     }
  80.     public function getCredits(): ?int
  81.     {
  82.         return $this->credits;
  83.     }
  84.     public function setCredits(?int $credits): void
  85.     {
  86.         $this->credits $credits;
  87.     }
  88.     public function getNumber(): ?int
  89.     {
  90.         return $this->number;
  91.     }
  92.     public function setNumber(?int $number): void
  93.     {
  94.         $this->number $number;
  95.     }
  96.     public function getName(): ?string
  97.     {
  98.         return $this->name;
  99.     }
  100.     public function setName(?string $name): void
  101.     {
  102.         $this->name $name;
  103.     }
  104.     public function getPromotionCodes(): ?Collection
  105.     {
  106.         return $this->promotionCodes;
  107.     }
  108.     public function addPromotionCode(PromotionCode $promotionCode): self
  109.     {
  110.         if (!$this->promotionCodes->contains($promotionCode)) {
  111.             $this->promotionCodes[] = $promotionCode;
  112.             $promotionCode->setUniquePromotion($this);
  113.         }
  114.         return $this;
  115.     }
  116.     public function removePromotionCode(PromotionCode $promotionCode): self
  117.     {
  118.         if ($this->promotionCodes->removeElement($promotionCode)) {
  119.             // set the owning side to null (unless already changed)
  120.             if ($promotionCode->getUniquePromotion() === $this) {
  121.                 $promotionCode->setUniquePromotion(null);
  122.             }
  123.         }
  124.         return $this;
  125.     }
  126. }