|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pgs\RestfonyBundle\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\ORMException; |
|
6
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
7
|
|
|
use FOS\RestBundle\Request\ParamFetcherInterface; |
|
8
|
|
|
use Knp\Component\Pager\Pagination\AbstractPagination; |
|
9
|
|
|
use Knp\Component\Pager\PaginatorInterface; |
|
10
|
|
|
use Pgs\RestfonyBundle\Model\RestPaginator; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
12
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @author Michał Sikora |
|
16
|
|
|
*/ |
|
17
|
|
|
class RestPaginatorFactory |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var PaginatorInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
private $paginator; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var RelationPaginatorFactory |
|
26
|
|
|
*/ |
|
27
|
|
|
private $relationPaginatorFactory; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param PaginatorInterface $paginator |
|
31
|
|
|
* @param RelationPaginatorFactory $relationPaginatorFactory |
|
32
|
|
|
*/ |
|
33
|
2 |
|
public function __construct(PaginatorInterface $paginator, RelationPaginatorFactory $relationPaginatorFactory) |
|
34
|
|
|
{ |
|
35
|
2 |
|
$this->paginator = $paginator; |
|
36
|
2 |
|
$this->relationPaginatorFactory = $relationPaginatorFactory; |
|
37
|
2 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param QueryBuilder $query |
|
41
|
|
|
* @param Request $request |
|
42
|
|
|
* @param ParamFetcherInterface $paramFetcher |
|
43
|
|
|
* @param string $moduleName |
|
44
|
|
|
* |
|
45
|
|
|
* @return RestPaginator |
|
46
|
|
|
*/ |
|
47
|
2 |
|
public function create(QueryBuilder $query, Request $request, ParamFetcherInterface $paramFetcher, $moduleName) |
|
48
|
|
|
{ |
|
49
|
|
|
try { |
|
50
|
2 |
|
$page = $paramFetcher->get('page'); |
|
51
|
2 |
|
$limit = $paramFetcher->get('limit'); |
|
52
|
|
|
/** @var AbstractPagination $paginationView */ |
|
53
|
2 |
|
$paginationView = $this->paginator->paginate($query, $page, $limit); |
|
54
|
|
|
|
|
55
|
1 |
|
return new RestPaginator( |
|
56
|
|
|
$page, |
|
57
|
|
|
$paginationView, |
|
58
|
1 |
|
$this->relationPaginatorFactory->create($request, $paginationView, $moduleName) |
|
59
|
|
|
); |
|
60
|
1 |
|
} catch (ORMException $ex) { |
|
61
|
1 |
|
throw new BadRequestHttpException(); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|