src/Controller/Api/V1/Song/GetAllUnderProfile.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api\V1\Song;
  3. use App\DTO\MMPZ\Song\DownloadSongParametersDTO;
  4. use App\Entity\Profile\Profile;
  5. use App\Entity\Song\Song;
  6. use App\Enums\Constants;
  7. use App\Helpers\PaginationHelper;
  8. use App\Repository\Song\SongRepository;
  9. use App\Responders\CollectionResponder;
  10. use App\Service\ElasticSearch\SongElasticSearchService;
  11. use App\Service\RequestDataExtractorService;
  12. use App\Service\SongService;
  13. use Doctrine\Common\Collections\ArrayCollection;
  14. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  15. use Swagger\Annotations as SWG;
  16. use Symfony\Component\HttpFoundation\JsonResponse;
  17. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. class GetAllUnderProfile
  20. {
  21.     /**
  22.      * @var SongService
  23.      */
  24.     private $songService;
  25.     /**
  26.      * @var PaginationHelper
  27.      */
  28.     private $paginationHelper;
  29.     /**
  30.      * @var RequestDataExtractorService
  31.      */
  32.     private $requestDataExtractorService;
  33.     /**
  34.      * @var SongRepository
  35.      */
  36.     private $songRepository;
  37.     public function __construct(
  38.         SongService $songService,
  39.         PaginationHelper $paginationHelper,
  40.         RequestDataExtractorService $requestDataExtractorService,
  41.         SongRepository $songRepository
  42.     )
  43.     {
  44.         $this->songService $songService;
  45.         $this->paginationHelper $paginationHelper;
  46.         $this->requestDataExtractorService $requestDataExtractorService;
  47.         $this->songRepository $songRepository;
  48.     }
  49.     /**
  50.      * @Route(path="/v1/profile/{id}/song", methods={"GET"})
  51.      *
  52.      * @SWG\Get(
  53.      *   tags={"Song"},
  54.      *   description="Get all songs under some profile"
  55.      * )
  56.      *
  57.      * @SWG\Parameter(
  58.      *    name="limit",
  59.      *    in="query",
  60.      *    type="integer",
  61.      *    default="5",
  62.      *    description="Enter the number of result items to show on a page",
  63.      *    required=true
  64.      *   ),
  65.      *
  66.      * * @SWG\Parameter(
  67.      *    name="search_term",
  68.      *    in="query",
  69.      *    type="string",
  70.      *    default="",
  71.      *    description="Enter the search terms",
  72.      *   ),
  73.      *
  74.      * @SWG\Parameter(
  75.      *    name="page",
  76.      *    in="query",
  77.      *    type="integer",
  78.      *    default="1",
  79.      *    description="Enter the number of result page to show",
  80.      *    required=true
  81.      *   )
  82.      *
  83.      * @SWG\Parameter(
  84.      *     name="sort",
  85.      *     in="query",
  86.      *     type="string",
  87.      *     default="",
  88.      *     description="Enter the number of result page to show",
  89.      *    )
  90.      *
  91.      * @SWG\Parameter(
  92.      *      name="sort_order",
  93.      *      in="query",
  94.      *      type="string",
  95.      *      default="",
  96.      *      description="Enter the number of result page to show",
  97.      *     )
  98.      *
  99.      * @SWG\Response(
  100.      *     response=200,
  101.      *     description="Profiles list"
  102.      * )
  103.      *
  104.      * @IsGranted("PROFILE_ACCESS", subject="profile")
  105.      *
  106.      * @param Profile $profile
  107.      * @param CollectionResponder $collectionResponder
  108.      * @param SongElasticSearchService $songElasticSearchService
  109.      * @return JsonResponse
  110.      * @throws \Exception
  111.      */
  112.     public function __invoke(Profile $profileCollectionResponder $collectionResponderSongElasticSearchService $songElasticSearchService)
  113.     {
  114.         $searchTerm $this->requestDataExtractorService->extractQueryParameter('search_term');
  115.         $searchTerm $this->escapeElasticSearchQuery($searchTerm);
  116.         $status $this->requestDataExtractorService->extractQueryParameter('status');
  117.         $sort $this->requestDataExtractorService->extractQueryParameter('sort');
  118.         $sortOrder $this->requestDataExtractorService->extractQueryParameter('sort_order');
  119.         /** @var DownloadSongParametersDTO $downloadSongParameters */
  120.         $downloadSongParameters = (new DownloadSongParametersDTO())
  121.             ->setAll(true);
  122.         $allSongsUnderProfile = new ArrayCollection($this->songService->findAllUnderProfile($profile$downloadSongParameters));
  123.         if ($profile->getProfileType()->getName() === Constants::PROFILE_TYPE_SERVICE) {
  124.             $allSongsUnderProfile = new ArrayCollection($this->songRepository->findAll());
  125.         }
  126.         if(!is_null($searchTerm)) {
  127.             $params = [];
  128.             $params['search_term'] = $searchTerm;
  129.             $elasticCollection $songElasticSearchService->search($params);
  130.         }
  131.         if (! $searchTerm) {
  132.             $elasticCollection $allSongsUnderProfile;
  133.         }
  134.         /** @var ArrayCollection $elasticCollection */
  135.         $elasticCollection $elasticCollection->filter(function (Song $song) use ($status$allSongsUnderProfile) {
  136.             if (! $allSongsUnderProfile->contains($song)) {
  137.                 return null;
  138.             }
  139.             if (! $status) {
  140.                 return $song;
  141.             }
  142.             if ($song->getComposition()->getStatus() === $status) {
  143.                 return $song;
  144.             }
  145.             return null;
  146.         });
  147.         /** @var \Iterator $iterator */
  148.         $iterator $elasticCollection->getIterator();
  149.         $iterator->uasort(function (Song $firstSong $second) use ($sort$sortOrder) {
  150.             if(!is_null($sort) && !is_null($sortOrder)){
  151.                 if($sortOrder === "asc"){
  152.                     if($sort === "published_at"){
  153.                         return $first->getComposition()->getPublishedAt() <=> $second->getComposition()->getPublishedAt();
  154.                     }else if($sort === "submitted_at"){
  155.                         return $first->getComposition()->getSubmittedAt() <=> $second->getComposition()->getSubmittedAt();
  156.                     }
  157.                 }else if($sortOrder === "desc"){
  158.                     if($sort === "published_at"){
  159.                         return  $second->getComposition()->getPublishedAt() <=> $first->getComposition()->getPublishedAt();
  160.                     }else if($sort === "submitted_at"){
  161.                         return  $second->getComposition()->getSubmittedAt() <=> $first->getComposition()->getSubmittedAt();
  162.                     }
  163.                 }
  164.             }
  165.             return strnatcasecmp($first->getComposition()->getTitle(), $second->getComposition()->getTitle());
  166.         });
  167.         $sortedCollection iterator_to_array($iterator);
  168.         $paginatedCollection $this->paginationHelper->buildPaginatedResult($sortedCollection);
  169.         return $collectionResponder($paginatedCollection, ['song:index']);
  170.     }
  171.     private function escapeElasticSearchQuery(string $query): string
  172.     {
  173.         return addcslashes($query'+-=&|><!(){}[]^"~*?:\\/');
  174.     }
  175. }