src/Controller/Api/V1/Profile/Message/GetAll.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api\V1\Profile\Message;
  3. use App\Entity\Profile\Helper\Article;
  4. use App\Entity\Profile\Helper\Helper;
  5. use App\Entity\Profile\Message\Message;
  6. use App\Entity\Profile\Profile;
  7. use App\Entity\Profile\ProfileRBAC;
  8. use App\Entity\User\User;
  9. use App\Enums\Constants;
  10. use App\Helpers\PaginationHelper;
  11. use App\Repository\Profile\Helper\ArticleRepository;
  12. use App\Repository\Profile\Helper\HelperRepository;
  13. use App\Repository\Profile\Message\MessageRepository;
  14. use App\Responders\CollectionResponder;
  15. use App\Service\RequestDataExtractorService;
  16. use Doctrine\Common\Collections\ArrayCollection;
  17. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  18. use Swagger\Annotations as SWG;
  19. use Symfony\Component\HttpFoundation\JsonResponse;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\Routing\Annotation\Route;
  22. class GetAll
  23. {
  24.     /**
  25.      * @var PaginationHelper
  26.      */
  27.     private $paginationHelper;
  28.     /**
  29.      * @var RequestDataExtractorService
  30.      */
  31.     private $requestDataExtractorService;
  32.     /** @var MessageRepository */
  33.     private $messageRepository;
  34.     /**
  35.      * GetAllUnderProfile constructor.
  36.      *
  37.      * @param PaginationHelper $paginationHelper
  38.      * @param RequestDataExtractorService $requestDataExtractorService
  39.      * @param MessageRepository $messageRepository
  40.      */
  41.     public function __construct(
  42.         PaginationHelper $paginationHelper,
  43.         RequestDataExtractorService $requestDataExtractorService,
  44.         MessageRepository $messageRepository
  45.     )
  46.     {
  47.         $this->paginationHelper $paginationHelper;
  48.         $this->requestDataExtractorService $requestDataExtractorService;
  49.         $this->messageRepository $messageRepository;
  50.     }
  51.     /**
  52.      * @Route(path="/v1/profile/{id}/message", methods={"GET"})
  53.      *
  54.      * @SWG\Get(
  55.      *   tags={"Profile Message"},
  56.      *   description="Get all profile Messages under some profile"
  57.      * )
  58.      *
  59.      * @SWG\Parameter(
  60.      *    name="limit",
  61.      *    in="query",
  62.      *    type="integer",
  63.      *    default="5",
  64.      *    description="Enter the number of result items to show on a page",
  65.      *    required=true
  66.      *   ),
  67.      *
  68.      * @SWG\Parameter(
  69.      *    name="page",
  70.      *    in="query",
  71.      *    type="integer",
  72.      *    default="1",
  73.      *    description="Enter the number of result page to show",
  74.      *    required=true
  75.      *   ),
  76.      *
  77.      * @SWG\Parameter(
  78.      *    name="search_term",
  79.      *    in="query",
  80.      *    type="string",
  81.      *    description="Search term"
  82.      *   ),
  83.      *
  84.      *
  85.      * @SWG\Response(
  86.      *     response=200,
  87.      *     description="Profile Messages list"
  88.      * )
  89.      *
  90.      *
  91.      *
  92.      * @param Profile $profile
  93.      * @param Request $request
  94.      * @param CollectionResponder $collectionResponder
  95.      * @return JsonResponse
  96.      *///@IsGranted("PROFILE_ACCESS", subject="profile")
  97.     public function __invoke(Profile $profileRequest $requestCollectionResponder $collectionResponder)
  98.     {
  99.         $searchTerm $this->requestDataExtractorService->extractQueryParameter('search_term');
  100.         $filteredCollection = [];
  101.         $messages $this->messageRepository->findBy(['profile' => $profile], ['createdAt' => 'DESC']);
  102.         $idToGetMessage = (is_null($profile->getParent())) ? $profile->getId() : $profile->getParent()->getId();
  103.         $serviceMessages $this->messageRepository->findBy(['profile' => $idToGetMessage], ['createdAt' => 'DESC']);
  104.         foreach($serviceMessages as $message) {
  105.             if(!in_array($message$messages)) {
  106.                 $messages[] = $message;
  107.             }
  108.         }
  109.         
  110.         if (! $searchTerm) {
  111.             $filteredCollection $messages;
  112.         }else{
  113.             /** @var Message $message */
  114.             foreach ($messages as $message) {
  115.                 if (stripos($message->getContent(), $searchTerm) !== false) {
  116.                     if(!in_array($message$filteredCollection)){
  117.                         $filteredCollection[] = $message;
  118.                     }
  119.                 }
  120.             }
  121.         }
  122.         $paginatedCollection $this->paginationHelper->buildPaginatedResult($filteredCollection);
  123.         return $collectionResponder($paginatedCollection, ['message:show']);
  124.     }
  125. }