src/Entity/ShopReview.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\ShopReviewRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\DBAL\Types\Types;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Component\Validator\Constraints as Assert;
  9. #[ORM\Entity(repositoryClassShopReviewRepository::class)]
  10. #[ORM\Table(name'shop_reviews')]
  11. #[ORM\HasLifecycleCallbacks]
  12. class ShopReview
  13. {
  14.     #[ORM\Id]
  15.     #[ORM\GeneratedValue]
  16.     #[ORM\Column]
  17.     private ?int $id null;
  18.     #[ORM\ManyToOne(targetEntityUser::class)]
  19.     #[ORM\JoinColumn(nullablefalse)]
  20.     private ?User $user null;
  21.     #[ORM\ManyToOne(targetEntityShop::class, inversedBy'reviews')]
  22.     #[ORM\JoinColumn(nullablefalse)]
  23.     private ?Shop $shop null;
  24.     #[ORM\Column(typeTypes::TEXT)]
  25.     #[Assert\NotBlank(message'Le commentaire est obligatoire')]
  26.     #[Assert\Length(min10max1000minMessage'Le commentaire doit contenir au moins {{ limit }} caractères'maxMessage'Le commentaire ne peut pas dépasser {{ limit }} caractères')]
  27.     private ?string $comment null;
  28.     #[ORM\Column(typeTypes::INTEGER)]
  29.     #[Assert\Range(min1max5notInRangeMessage'La note doit être entre {{ min }} et {{ max }}')]
  30.     private ?int $rating null;
  31.     #[ORM\Column(typeTypes::BOOLEAN)]
  32.     private bool $isVerified false;
  33.     #[ORM\Column(typeTypes::BOOLEAN)]
  34.     private bool $isVisible true;
  35.     #[ORM\Column(typeTypes::BOOLEAN)]
  36.     private bool $isHelpful false;
  37.     #[ORM\Column(typeTypes::INTEGER)]
  38.     private int $helpfulCount 0;
  39.     #[ORM\Column(typeTypes::INTEGER)]
  40.     private int $notHelpfulCount 0;
  41.     #[ORM\Column(typeTypes::DATETIME_IMMUTABLE)]
  42.     private ?\DateTimeImmutable $createdAt null;
  43.     #[ORM\Column(typeTypes::DATETIME_IMMUTABLEnullabletrue)]
  44.     private ?\DateTimeImmutable $updatedAt null;
  45.     #[ORM\Column(typeTypes::DATETIME_IMMUTABLEnullabletrue)]
  46.     private ?\DateTimeImmutable $verifiedAt null;
  47.     // Constantes pour les types de review
  48.     public const TYPE_PRODUCT_QUALITY 'product_quality';
  49.     public const TYPE_SHIPPING_SPEED 'shipping_speed';
  50.     public const TYPE_CUSTOMER_SERVICE 'customer_service';
  51.     public const TYPE_COMMUNICATION 'communication';
  52.     public const TYPE_PACKAGING 'packaging';
  53.     #[ORM\Column(typeTypes::STRINGlength50nullabletrue)]
  54.     private ?string $reviewType null;
  55.     #[ORM\OneToMany(targetEntityShopReviewVote::class, mappedBy'review'cascade: ['remove'])]
  56.     private Collection $votes;
  57.     public function __construct()
  58.     {
  59.         $this->createdAt = new \DateTimeImmutable();
  60.         $this->votes = new ArrayCollection();
  61.     }
  62.     #[ORM\PreUpdate]
  63.     public function setUpdatedAtValue(): void
  64.     {
  65.         $this->updatedAt = new \DateTimeImmutable();
  66.     }
  67.     public function getId(): ?int
  68.     {
  69.         return $this->id;
  70.     }
  71.     public function getUser(): ?User
  72.     {
  73.         return $this->user;
  74.     }
  75.     public function setUser(?User $user): static
  76.     {
  77.         $this->user $user;
  78.         return $this;
  79.     }
  80.     public function getShop(): ?Shop
  81.     {
  82.         return $this->shop;
  83.     }
  84.     public function setShop(?Shop $shop): static
  85.     {
  86.         $this->shop $shop;
  87.         return $this;
  88.     }
  89.     public function getComment(): ?string
  90.     {
  91.         return $this->comment;
  92.     }
  93.     public function setComment(string $comment): static
  94.     {
  95.         $this->comment $comment;
  96.         return $this;
  97.     }
  98.     public function getRating(): ?int
  99.     {
  100.         return $this->rating;
  101.     }
  102.     public function setRating(int $rating): static
  103.     {
  104.         $this->rating $rating;
  105.         return $this;
  106.     }
  107.     public function isVerified(): bool
  108.     {
  109.         return $this->isVerified;
  110.     }
  111.     public function setIsVerified(bool $isVerified): static
  112.     {
  113.         $this->isVerified $isVerified;
  114.         if ($isVerified && !$this->verifiedAt) {
  115.             $this->verifiedAt = new \DateTimeImmutable();
  116.         }
  117.         return $this;
  118.     }
  119.     public function isVisible(): bool
  120.     {
  121.         return $this->isVisible;
  122.     }
  123.     public function setIsVisible(bool $isVisible): static
  124.     {
  125.         $this->isVisible $isVisible;
  126.         return $this;
  127.     }
  128.     public function isHelpful(): bool
  129.     {
  130.         return $this->isHelpful;
  131.     }
  132.     public function setIsHelpful(bool $isHelpful): static
  133.     {
  134.         $this->isHelpful $isHelpful;
  135.         return $this;
  136.     }
  137.     public function getHelpfulCount(): int
  138.     {
  139.         return $this->helpfulCount;
  140.     }
  141.     public function setHelpfulCount(int $helpfulCount): static
  142.     {
  143.         $this->helpfulCount $helpfulCount;
  144.         return $this;
  145.     }
  146.     public function incrementHelpfulCount(): static
  147.     {
  148.         $this->helpfulCount++;
  149.         return $this;
  150.     }
  151.     public function getNotHelpfulCount(): int
  152.     {
  153.         return $this->notHelpfulCount;
  154.     }
  155.     public function setNotHelpfulCount(int $notHelpfulCount): static
  156.     {
  157.         $this->notHelpfulCount $notHelpfulCount;
  158.         return $this;
  159.     }
  160.     public function incrementNotHelpfulCount(): static
  161.     {
  162.         $this->notHelpfulCount++;
  163.         return $this;
  164.     }
  165.     public function getCreatedAt(): ?\DateTimeImmutable
  166.     {
  167.         return $this->createdAt;
  168.     }
  169.     public function setCreatedAt(\DateTimeImmutable $createdAt): static
  170.     {
  171.         $this->createdAt $createdAt;
  172.         return $this;
  173.     }
  174.     public function getUpdatedAt(): ?\DateTimeImmutable
  175.     {
  176.         return $this->updatedAt;
  177.     }
  178.     public function setUpdatedAt(?\DateTimeImmutable $updatedAt): static
  179.     {
  180.         $this->updatedAt $updatedAt;
  181.         return $this;
  182.     }
  183.     public function getVerifiedAt(): ?\DateTimeImmutable
  184.     {
  185.         return $this->verifiedAt;
  186.     }
  187.     public function setVerifiedAt(?\DateTimeImmutable $verifiedAt): static
  188.     {
  189.         $this->verifiedAt $verifiedAt;
  190.         return $this;
  191.     }
  192.     public function getReviewType(): ?string
  193.     {
  194.         return $this->reviewType;
  195.     }
  196.     public function setReviewType(?string $reviewType): static
  197.     {
  198.         $this->reviewType $reviewType;
  199.         return $this;
  200.     }
  201.     public function getRatingStars(): string
  202.     {
  203.         $stars '';
  204.         for ($i 1$i <= 5$i++) {
  205.             $stars .= $i <= $this->rating '★' '☆';
  206.         }
  207.         return $stars;
  208.     }
  209.     public function getRatingPercentage(): float
  210.     {
  211.         return ($this->rating 5) * 100;
  212.     }
  213.     public function getHelpfulnessPercentage(): float
  214.     {
  215.         $total $this->helpfulCount $this->notHelpfulCount;
  216.         if ($total === 0) {
  217.             return 0;
  218.         }
  219.         return ($this->helpfulCount $total) * 100;
  220.     }
  221.     public function getTimeAgo(): string
  222.     {
  223.         $now = new \DateTimeImmutable();
  224.         $diff $now->diff($this->createdAt);
  225.         if ($diff->0) {
  226.             return $diff->' an' . ($diff->'s' '');
  227.         } elseif ($diff->0) {
  228.             return $diff->' mois';
  229.         } elseif ($diff->0) {
  230.             return $diff->' jour' . ($diff->'s' '');
  231.         } elseif ($diff->0) {
  232.             return $diff->' heure' . ($diff->'s' '');
  233.         } else {
  234.             return $diff->' minute' . ($diff->'s' '');
  235.         }
  236.     }
  237.     public static function getReviewTypes(): array
  238.     {
  239.         return [
  240.             self::TYPE_PRODUCT_QUALITY => 'Qualité du produit',
  241.             self::TYPE_SHIPPING_SPEED => 'Vitesse de livraison',
  242.             self::TYPE_CUSTOMER_SERVICE => 'Service client',
  243.             self::TYPE_COMMUNICATION => 'Communication',
  244.             self::TYPE_PACKAGING => 'Emballage'
  245.         ];
  246.     }
  247.     public function getReviewTypeLabel(): string
  248.     {
  249.         $types self::getReviewTypes();
  250.         return $types[$this->reviewType] ?? 'Non spécifié';
  251.     }
  252.     /**
  253.      * @return Collection<int, ShopReviewVote>
  254.      */
  255.     public function getVotes(): Collection
  256.     {
  257.         return $this->votes;
  258.     }
  259.     public function addVote(ShopReviewVote $vote): static
  260.     {
  261.         if (!$this->votes->contains($vote)) {
  262.             $this->votes->add($vote);
  263.             $vote->setReview($this);
  264.         }
  265.         return $this;
  266.     }
  267.     public function removeVote(ShopReviewVote $vote): static
  268.     {
  269.         if ($this->votes->removeElement($vote)) {
  270.             if ($vote->getReview() === $this) {
  271.                 $vote->setReview(null);
  272.             }
  273.         }
  274.         return $this;
  275.     }
  276. }