src/Services/Comment/CommentService.php line 88

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Slivki\Services\Comment;
  4. use Slivki\Dao\Order\OfferOrderPurchaseCountDaoInterface;
  5. use Slivki\Dto\Comment\CommentDto;
  6. use Slivki\Factory\Comment\CommentDtoFactory;
  7. use Slivki\Message\Query\Comment\GetOffersCommentsQuery;
  8. use Slivki\Paginator\Comment\CommentPaginatorInterface;
  9. use Slivki\Response\Comment\GetCommentsResponse;
  10. use function array_map;
  11. use function array_values;
  12. final class CommentService
  13. {
  14.     private CommentPaginatorInterface $commentPaginator;
  15.     private CommentDtoFactory $commentDtoFactory;
  16.     private OfferOrderPurchaseCountDaoInterface $offerOrderPurchaseCountDao;
  17.     private ?array $childCommentsRuntimeCache null;
  18.     public function __construct(
  19.         CommentPaginatorInterface $commentPaginator,
  20.         CommentDtoFactory $commentDtoFactory,
  21.         OfferOrderPurchaseCountDaoInterface $offerOrderPurchaseCountDao
  22.     ) {
  23.         $this->commentPaginator $commentPaginator;
  24.         $this->commentDtoFactory $commentDtoFactory;
  25.         $this->offerOrderPurchaseCountDao $offerOrderPurchaseCountDao;
  26.     }
  27.     public function getOffersCommentsTree(GetOffersCommentsQuery $query): GetCommentsResponse
  28.     {
  29.         $paginator $this->commentPaginator->getOffersComments($query);
  30.         $parentComments array_map(
  31.             [$this->commentDtoFactory'create'],
  32.             (array) $paginator->getItems(),
  33.         );
  34.         $childCommentsQuery = new GetOffersCommentsQuery(
  35.             1,
  36.             PHP_INT_MAX,
  37.             false,
  38.             $query->getOfferId(),
  39.             $query->getUserId()
  40.         );
  41.         $this->childCommentsRuntimeCache = (array) $this->commentPaginator->getOffersComments($childCommentsQuery)->getItems();
  42.         return new GetCommentsResponse(
  43.             $this->buildTree($parentComments),
  44.             $paginator->getTotalItemCount(),
  45.         );
  46.     }
  47.     /**
  48.      * @param array<CommentDto> $comments
  49.      * @return array<CommentDto>
  50.      */
  51.     private function buildTree(array $comments): array
  52.     {
  53.         $branch = [];
  54.         foreach ($comments as $comment) {
  55.             $children =  array_map(
  56.                 [$this->commentDtoFactory'create'],
  57.                 array_filter(
  58.                     $this->childCommentsRuntimeCache,
  59.                     static fn (array $childComment) => $childComment['parent_id'] === $comment->getId(),
  60.                 ),
  61.             );
  62.             $comment->setChildren($this->buildTree(array_values($children)));
  63.             $branch[] = $comment;
  64.         }
  65.         return $branch;
  66.     }
  67.     public function isUserAllowToRateInOffer(int $userIdint $offerId): bool
  68.     {
  69.         return $this->offerOrderPurchaseCountDao->findPurchaseCountByUserAndOffer($userId$offerId) > 0;
  70.     }
  71.     public function calcFakeCommentRating(float $rating): float
  72.     {
  73.         if ($rating == 0) {
  74.             return 0.0;
  75.         }
  76.         return min(5round(2.5 + ($rating 1) * 0.6252));
  77.     }
  78. }