src/Service/Scholar/TrainingTransformerService.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Service\Scholar;
  3. use App\DTO\Channel\ChannelDTO;
  4. use App\DTO\Scholar\TrainingDTO;
  5. use App\Entity\Scholar\Training\Training;
  6. use App\Service\Advice\AdviceManager;
  7. use App\Service\Channel\Image\ImageUrlGenerator;
  8. use App\Service\ChannelUserData\CursusService;
  9. use Knp\Component\Pager\Pagination\PaginationInterface;
  10. use Nellapp\Bundle\SDKBundle\Twig\Runtime\ImageRunTime;
  11. class TrainingTransformerService
  12. {
  13. public function __construct(
  14. private ImageRunTime $imageRunTime,
  15. private ImageUrlGenerator $imageUrlGenerator,
  16. private TrainingService $trainingService,
  17. private CursusService $cursusService,
  18. private AdviceManager $adviceManager,
  19. )
  20. {
  21. }
  22. public function toDTO(Training $training): TrainingDTO
  23. {
  24. $channel = $training->getOwnerChannel();
  25. $channelImage = $this->imageRunTime->getChannelLogoThumbnail($channel);
  26. $channelDTO = new ChannelDTO(
  27. id: $channel->getId(),
  28. imagePath: $channel->getImagePath(),
  29. image: $channel->getImage(),
  30. name: $channel->getName(),
  31. logoUrl: $channelImage,
  32. trainingCount: $this->trainingService->countByChannel($channel),
  33. );
  34. $trainingImageUrl = $this->imageUrlGenerator->getImageUrl($training->getImage(), $channelImage);
  35. $trainingPrice = null;
  36. if (
  37. ($trainingProduct = $training->getTrainingProduct()) !== null
  38. && $trainingProduct->getPrice() !== null
  39. && $trainingProduct->getPrice() !== "0"
  40. && $trainingProduct->getPrice() !== "0.00" // Dirty, fix data
  41. && $trainingProduct->getPrice() !== 0
  42. ) {
  43. $trainingPrice = $trainingProduct->getPrice();
  44. }
  45. return new TrainingDTO(
  46. channel: $channelDTO,
  47. id: $training->getId(),
  48. name: $training->getName(),
  49. imageUrl: $trainingImageUrl,
  50. time: $training->getTotalHourDescription() ?: null,
  51. type: $training->getTrainingType(),
  52. diploma: $this->trainingService->getTrainingDiplomaStatusKey($training),
  53. price: $trainingPrice,
  54. freeLessonsCount: count($this->trainingService->getFreeLessonsInTraining($training)),
  55. cursusCount: $this->cursusService->countCursusByTraining($training),
  56. adviceAvg: $this->adviceManager->getAvgValueAdviceByTraining($training),
  57. );
  58. }
  59. /**
  60. * @param Training[] $trainings
  61. * @return TrainingDTO[]
  62. */
  63. public function toDTOs(iterable $trainings): array
  64. {
  65. return array_map([$this, 'toDTO'], $trainings);
  66. }
  67. public function toPaginationDTOs(PaginationInterface $trainingsPagination): PaginationInterface
  68. {
  69. $trainingsPagination->setItems($this->toDTOs($trainingsPagination->getItems()));
  70. return $trainingsPagination;
  71. }
  72. }