src/Response/Beauty/Offer/Factory/MasterResponseFactory.php line 47

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Slivki\Response\Beauty\Offer\Factory;
  4. use Slivki\Dao\Category\Rating\DirectorRatingDaoInterface;
  5. use Slivki\Entity\BeautyMaster;
  6. use Slivki\Entity\Category;
  7. use Slivki\Entity\Director;
  8. use Slivki\Enum\Beauty\MasterWorkPlaceType;
  9. use Slivki\Response\Beauty\Offer\MasterLocationResponse;
  10. use Slivki\Response\Beauty\Offer\MasterResponse;
  11. use Slivki\Response\OnlineOrder\Director\DirectorResponse;
  12. use Slivki\Services\ImageService;
  13. use function array_map;
  14. use function array_values;
  15. use function implode;
  16. use function round;
  17. final class MasterResponseFactory
  18. {
  19.     private ImageService $imageService;
  20.     private DirectorRatingDaoInterface $directorRatingDao;
  21.     private ?array $ratingDirectors null;
  22.     public function __construct(ImageService $imageServiceDirectorRatingDaoInterface $directorRatingDao)
  23.     {
  24.         $this->imageService $imageService;
  25.         $this->directorRatingDao $directorRatingDao;
  26.     }
  27.     public function create(BeautyMaster $beautyMaster): MasterResponse
  28.     {
  29.         return $this->createResponse($beautyMaster0);
  30.     }
  31.     public function createForCategory(BeautyMaster $beautyMasterint $categoryId): MasterResponse
  32.     {
  33.         return $this->createResponse(
  34.             $beautyMaster,
  35.             $this->getRating($categoryId$beautyMaster->getDirector()),
  36.         );
  37.     }
  38.     private function createResponse(BeautyMaster $beautyMasterfloat $rating): MasterResponse
  39.     {
  40.         $director $beautyMaster->getDirector();
  41.         $locations = [];
  42.         $offerIds = [];
  43.         foreach ($beautyMaster->getLocations() as $location) {
  44.             $locations[] = new MasterLocationResponse(
  45.                 $location->getId(),
  46.                 implode(', ', [$location->getCity(), $location->getStreet(), $location->getHouse()]),
  47.             );
  48.             $locationId $location->firstOffer()->getID();
  49.             $offerIds[$locationId] = $locationId;
  50.         }
  51.         return new MasterResponse(
  52.             $beautyMaster->getId(),
  53.             MasterWorkPlaceType::getByStatus($beautyMaster->getStatus()),
  54.             $beautyMaster->getFirstName(),
  55.             $beautyMaster->getLastName(),
  56.             $this->imageService->getImageURLCachedWithDomain($beautyMaster->getPhoto(), 00),
  57.             $rating,
  58.             $beautyMaster->getDescription(),
  59.             $locations,
  60.             array_map(static fn (Category $tag) => $tag->getName(), $beautyMaster->getTags()->toArray()),
  61.             array_values($offerIds),
  62.             $director !== null
  63.                 ? new DirectorResponse(
  64.                     $director->getID(),
  65.                     $this->imageService->getImageURLCachedWithDomain($director->getOnlinePaymentLogo(), 00),
  66.                     $director->getName(),
  67.                     $director->getLegalEntity(),
  68.                     $director->getTaxID(),
  69.                 )
  70.                 : null,
  71.         );
  72.     }
  73.     private function getRating(int $categoryId, ?Director $director): float
  74.     {
  75.         if ($director === null) {
  76.             return 0;
  77.         }
  78.         if ($this->ratingDirectors === null) {
  79.             $this->ratingDirectors $this->directorRatingDao->findDirectorRatingByCategoryId($categoryId);
  80.         }
  81.         foreach ($this->ratingDirectors as $ratingDirector) {
  82.             if ($ratingDirector->getDirectorId() === $director->getID()) {
  83.                 return round($ratingDirector->getAvgCommentRating(), 2);
  84.             }
  85.         }
  86.         return 0;
  87.     }
  88. }