src/Entity/PointsCatalog/CatalogGift.php line 40

Open in your IDE?
  1. <?php
  2. namespace App\Entity\PointsCatalog;
  3. use App\Annotation\SiteAware;
  4. use App\Entity\AbstractBase;
  5. use App\Entity\Interfaces\SiteInterface;
  6. use App\Entity\MiniAbstractBase;
  7. use App\Entity\Traits\DescriptionTrait;
  8. use App\Entity\Traits\HasImageTrait;
  9. use App\Entity\Traits\NameTrait;
  10. use App\Entity\Traits\OnlyAdminTrait;
  11. use App\Entity\Traits\PublishedTrait;
  12. use App\Entity\Traits\SiteTrait;
  13. use App\Entity\User;
  14. use App\Enum\UserRolesEnum;
  15. use App\Persistence\Model\UserGroupAccessType;
  16. use App\Repository\PointsCatalog\CatalogGiftRepository;
  17. use Doctrine\Common\Collections\ArrayCollection;
  18. use Doctrine\Common\Collections\Collection;
  19. use Doctrine\ORM\Mapping as ORM;
  20. use Gedmo\Mapping\Annotation as Gedmo;
  21. use Money\Money;
  22. use Symfony\Component\HttpFoundation\File\File;
  23. use Symfony\Component\Security\Core\User\UserInterface;
  24. use Symfony\Component\Validator\Constraints as Assert;
  25. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  26. /**
  27.  * @ORM\Table(name="vulco_points_catalog_gift", indexes={@ORM\Index(name="catalog_gift_site_idx", columns={"site"})})
  28.  *
  29.  * @ORM\Entity(repositoryClass=CatalogGiftRepository::class)
  30.  *
  31.  * @Gedmo\SoftDeleteable(fieldName="removedAt", timeAware=false)
  32.  *
  33.  * @SiteAware(siteFieldName="site")
  34.  *
  35.  * @Vich\Uploadable
  36.  */
  37. class CatalogGift extends AbstractBase implements SiteInterface
  38. {
  39.     use DescriptionTrait;
  40.     use HasImageTrait;
  41.     use NameTrait;
  42.     use OnlyAdminTrait;
  43.     use PublishedTrait;
  44.     use SiteTrait;
  45.     /**
  46.      * @Vich\UploadableField(mapping="catalog_gift_image", fileNameProperty="imageName")
  47.      */
  48.     private ?File $image null;
  49.     /**
  50.      * @ORM\Column(type="string", length=255)
  51.      *
  52.      * @Assert\NotBlank()
  53.      */
  54.     private string $name;
  55.     /**
  56.      * @ORM\Column(type="text", length=10000, nullable=true)
  57.      */
  58.     private ?string $description null;
  59.     /**
  60.      * @ORM\Column(type="string", length=255, nullable=true)
  61.      */
  62.     private ?string $homepageName null;
  63.     /**
  64.      * @ORM\Column(type="string", length=255)
  65.      *
  66.      * @Assert\NotBlank()
  67.      */
  68.     private string $code;
  69.     /**
  70.      * @ORM\Column(type="integer")
  71.      *
  72.      * @Assert\NotBlank()
  73.      */
  74.     private int $pointsValue;
  75.     /**
  76.      * @ORM\Column(type="boolean")
  77.      */
  78.     private bool $showInHomepage;
  79.     /**
  80.      * @ORM\Column(type="boolean", nullable=true)
  81.      */
  82.     private ?bool $showInHomepageClub null;
  83.     /**
  84.      * @ORM\Column(type="boolean", options={"default": 0})
  85.      */
  86.     private bool $onlyAdminVisible false;
  87.     /**
  88.      * @ORM\ManyToOne(targetEntity="App\Entity\PointsCatalog\CatalogGiftCategory", fetch="EAGER")
  89.      *
  90.      * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
  91.      *
  92.      * @Assert\NotBlank()
  93.      */
  94.     private ?CatalogGiftCategory $category;
  95.     /**
  96.      * @ORM\ManyToOne(targetEntity="App\Entity\PointsCatalog\CatalogSizeGroup")
  97.      *
  98.      * @ORM\JoinColumn(name="size_group_id", referencedColumnName="id")
  99.      */
  100.     private ?CatalogSizeGroup $sizeGroup;
  101.     /**
  102.      * @ORM\OneToMany(targetEntity="App\Entity\PointsCatalog\CatalogGiftHasProvider", mappedBy="gift", cascade={"persist"}, orphanRemoval=true)
  103.      *
  104.      * @Assert\Valid()
  105.      *
  106.      * @Assert\Count(min=1, max=1)
  107.      */
  108.     private ?Collection $providers;
  109.     public function __construct()
  110.     {
  111.         $this->providers = new ArrayCollection();
  112.     }
  113.     public function getCategory(): ?CatalogGiftCategory
  114.     {
  115.         return $this->category;
  116.     }
  117.     public function setCategory(CatalogGiftCategory $category): self
  118.     {
  119.         $this->category $category;
  120.         return $this;
  121.     }
  122.     public function getNameAndSizeGroupString(): string
  123.     {
  124.         return $this->getName().($this->getSizeGroup() ? ' ('.$this->getSizeGroup()->getName().')' '');
  125.     }
  126.     public function getSizeGroup(): ?CatalogSizeGroup
  127.     {
  128.         return $this->sizeGroup;
  129.     }
  130.     public function setSizeGroup(CatalogSizeGroup $sizeGroup): self
  131.     {
  132.         $this->sizeGroup $sizeGroup;
  133.         return $this;
  134.     }
  135.     public function getHomepageName(): ?string
  136.     {
  137.         return $this->homepageName;
  138.     }
  139.     public function setHomepageName(?string $homepageName): self
  140.     {
  141.         $this->homepageName $homepageName;
  142.         return $this;
  143.     }
  144.     public function getCode(): string
  145.     {
  146.         return $this->code;
  147.     }
  148.     public function setCode(string $code): self
  149.     {
  150.         $this->code $code;
  151.         return $this;
  152.     }
  153.     public function getPointsValue(): int
  154.     {
  155.         return $this->pointsValue;
  156.     }
  157.     public function getPointsValueString(): string
  158.     {
  159.         return MiniAbstractBase::getIntegerAsString($this->pointsValue);
  160.     }
  161.     public function setPointsValue(int $pointsValue): self
  162.     {
  163.         $this->pointsValue $pointsValue;
  164.         return $this;
  165.     }
  166.     public function isShowInHomepage(): bool
  167.     {
  168.         return $this->showInHomepage;
  169.     }
  170.     public function getShowInHomepage(): bool
  171.     {
  172.         return $this->isShowInHomepage();
  173.     }
  174.     public function setShowInHomepage(bool $showInHomepage): self
  175.     {
  176.         $this->showInHomepage $showInHomepage;
  177.         return $this;
  178.     }
  179.     public function isShowInHomepageClub(): ?bool
  180.     {
  181.         return $this->showInHomepageClub;
  182.     }
  183.     public function setShowInHomepageClub(?bool $showInHomepageClub): self
  184.     {
  185.         $this->showInHomepageClub $showInHomepageClub;
  186.         return $this;
  187.     }
  188.     public function getProviders(): ?Collection
  189.     {
  190.         return $this->providers;
  191.     }
  192.     public function setProviders(?Collection $providers): self
  193.     {
  194.         $this->providers $providers;
  195.         return $this;
  196.     }
  197.     public function addProvider(CatalogGiftHasProvider $provider): self
  198.     {
  199.         if (!$this->providers->contains($provider)) {
  200.             $provider->setGift($this);
  201.             $this->providers->add($provider);
  202.         }
  203.         return $this;
  204.     }
  205.     public function removeProvider(CatalogGiftHasProvider $provider): self
  206.     {
  207.         $this->providers->removeElement($provider);
  208.         return $this;
  209.     }
  210.     public function firstProvider(): ?CatalogGiftProvider
  211.     {
  212.         return $this->getProviders() && $this->getProviders()->isEmpty() ? null $this->getProviders()->first()->getProvider();
  213.     }
  214.     public function price(): ?Money
  215.     {
  216.         return $this->getProviders() && $this->getProviders()->first() ? $this->getProviders()->first()->getPrice() : null;
  217.     }
  218.     public function userHasAccess(UserInterface $user): bool
  219.     {
  220.         /** @var User $user */
  221.         $category $this->getCategory();
  222.         // coordinator must be the first condition to evaluate because coordinators have the associated role as well
  223.         if ($user->hasRole(UserRolesEnum::ROLE_COORDINATOR_LONG)) {
  224.             switch ($category->getUserAccessType()) {
  225.                 case UserGroupAccessType::ENABLE:
  226.                     return $user->belongsToUsers($category->getUsers());
  227.                 case UserGroupAccessType::DISABLE:
  228.                     return !$user->belongsToUsers($category->getUsers());
  229.                 case UserGroupAccessType::IGNORE:
  230.                 default:
  231.                     return true;
  232.             }
  233.         } elseif ($user->hasRole(UserRolesEnum::ROLE_ASSOCIATED_LONG)) {
  234.             switch ($category->getUserGroupAccessType()) {
  235.                 case UserGroupAccessType::ENABLE:
  236.                     return $user->belongsToUserGroup($category->getUserGroups());
  237.                 case UserGroupAccessType::DISABLE:
  238.                     return !$user->belongsToUserGroup($category->getUserGroups());
  239.                 case UserGroupAccessType::IGNORE:
  240.                 default:
  241.                     return true;
  242.             }
  243.         } else {
  244.             return true;
  245.         }
  246.     }
  247.     public static function userHasAccessClosure(UserInterface $user): \Closure
  248.     {
  249.         return static function (CatalogGift $catalogGift) use ($user) {
  250.             $category $catalogGift->getCategory();
  251.             // coordinator must be the first condition to evaluate because coordinators have the associated role as well
  252.             if ($user->hasRole(UserRolesEnum::ROLE_COORDINATOR_LONG)) {
  253.                 switch ($category->getUserAccessType()) {
  254.                     case UserGroupAccessType::ENABLE:
  255.                         return $user->belongsToUsers($category->getUsers());
  256.                     case UserGroupAccessType::DISABLE:
  257.                         return !$user->belongsToUsers($category->getUsers());
  258.                     case UserGroupAccessType::IGNORE:
  259.                     default:
  260.                         return true;
  261.                 }
  262.             } elseif ($user->hasRole(UserRolesEnum::ROLE_ASSOCIATED_LONG)) {
  263.                 switch ($category->getUserGroupAccessType()) {
  264.                     case UserGroupAccessType::ENABLE:
  265.                         return $user->belongsToUserGroup($category->getUserGroups());
  266.                     case UserGroupAccessType::DISABLE:
  267.                         return !$user->belongsToUserGroup($category->getUserGroups());
  268.                     case UserGroupAccessType::IGNORE:
  269.                     default:
  270.                         return true;
  271.                 }
  272.             } else {
  273.                 return true;
  274.             }
  275.         };
  276.     }
  277.     public static function getCatalogGiftsUserHasAccess(UserInterface $useriterable $catalogGifts): iterable
  278.     {
  279.         $catalogGiftsUserHasAccess = [];
  280.         if (!empty($catalogGifts)) {
  281.             // get only catalogGifts that current user has access
  282.             /** @var CatalogGift $catalogGift */
  283.             foreach ($catalogGifts as $catalogGift) {
  284.                 if ($catalogGift->userHasAccess($user)) {
  285.                     $catalogGiftsUserHasAccess[] = $catalogGift;
  286.                 }
  287.             }
  288.         }
  289.         return $catalogGiftsUserHasAccess;
  290.     }
  291.     public function __toString(): string
  292.     {
  293.         return $this->id $this->name MiniAbstractBase::DEFAULT_EMPTY_STRING;
  294.     }
  295. }