]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ApiBundle/Controller/SearchRestController.php
Factorize sendResponse between Api controllers
[github/wallabag/wallabag.git] / src / Wallabag / ApiBundle / Controller / SearchRestController.php
CommitLineData
b3205798
CR
1<?php
2
3namespace Wallabag\ApiBundle\Controller;
4
5use Hateoas\Configuration\Route;
6use Hateoas\Representation\Factory\PagerfantaFactory;
b3205798
CR
7use Nelmio\ApiDocBundle\Annotation\ApiDoc;
8use Pagerfanta\Adapter\DoctrineORMAdapter;
9use Pagerfanta\Pagerfanta;
10use Symfony\Component\HttpFoundation\JsonResponse;
11use Symfony\Component\HttpFoundation\Request;
b3205798 12use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
b3205798
CR
13
14class SearchRestController extends WallabagRestController
15{
16 /**
17 * Search all entries by term.
18 *
19 * @ApiDoc(
20 * parameters={
21 * {"name"="term", "dataType"="string", "required"=false, "format"="any", "description"="Any query term"},
22 * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."},
23 * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."}
24 * }
25 * )
26 *
27 * @return JsonResponse
28 */
29 public function getSearchAction(Request $request)
30 {
31 $this->validateAuthentication();
32
33 $term = $request->query->get('term');
34 $page = (int) $request->query->get('page', 1);
35 $perPage = (int) $request->query->get('perPage', 30);
36
37 $qb = $this->get('wallabag_core.entry_repository')
38 ->getBuilderForSearchByUser(
39 $this->getUser()->getId(),
40 $term,
41 null
42 );
43
44 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false);
45 $pager = new Pagerfanta($pagerAdapter);
46
47 $pager->setMaxPerPage($perPage);
48 $pager->setCurrentPage($page);
49
50 $pagerfantaFactory = new PagerfantaFactory('page', 'perPage');
51 $paginatedCollection = $pagerfantaFactory->createRepresentation(
52 $pager,
53 new Route(
54 'api_get_search',
55 [
56 'term' => $term,
57 'page' => $page,
58 'perPage' => $perPage,
59 ],
60 UrlGeneratorInterface::ABSOLUTE_URL
61 )
62 );
63
64 return $this->sendResponse($paginatedCollection);
65 }
b3205798 66}