1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Paysera\Bundle\ApiBundle\Normalizer\Pagination; |
6
|
|
|
|
7
|
|
|
use Paysera\Bundle\ApiBundle\Entity\PagedQuery; |
8
|
|
|
use Paysera\Component\Normalization\NormalizationContext; |
9
|
|
|
use Paysera\Component\Normalization\NormalizerInterface; |
10
|
|
|
use Paysera\Component\Normalization\TypeAwareInterface; |
11
|
|
|
use Paysera\Pagination\Entity\Result; |
12
|
|
|
use Paysera\Pagination\Service\Doctrine\ResultProvider; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @see ResultNormalizer for fetched result normalization |
16
|
|
|
*/ |
17
|
|
|
class PagedQueryNormalizer implements NormalizerInterface, TypeAwareInterface |
18
|
|
|
{ |
19
|
|
|
private $resultProvider; |
20
|
|
|
private $defaultTotalCountStrategy; |
21
|
|
|
private $maximumOffset; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param ResultProvider $resultProvider |
25
|
|
|
* @param string $defaultTotalCountStrategy |
26
|
|
|
* @param int|null $maximumOffset |
27
|
|
|
*/ |
28
|
109 |
|
public function __construct(ResultProvider $resultProvider, string $defaultTotalCountStrategy, $maximumOffset) |
29
|
|
|
{ |
30
|
109 |
|
$this->resultProvider = $resultProvider; |
31
|
109 |
|
$this->defaultTotalCountStrategy = $defaultTotalCountStrategy; |
32
|
109 |
|
$this->maximumOffset = $maximumOffset; |
33
|
109 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param PagedQuery $pagedQuery |
37
|
|
|
* @param NormalizationContext $normalizationContext |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
27 |
|
public function normalize($pagedQuery, NormalizationContext $normalizationContext) |
41
|
|
|
{ |
42
|
27 |
|
$result = $this->fetchResultByPagedQuery($pagedQuery, $normalizationContext); |
43
|
25 |
|
return $normalizationContext->normalize($result, ''); |
44
|
|
|
} |
45
|
|
|
|
46
|
27 |
|
private function fetchResultByPagedQuery(PagedQuery $pagedQuery, NormalizationContext $normalizationContext) |
47
|
|
|
{ |
48
|
27 |
|
$configuredQuery = clone $pagedQuery->getConfiguredQuery(); |
49
|
|
|
|
50
|
|
|
if ( |
51
|
27 |
|
!$normalizationContext->isFieldIncluded('items') |
52
|
27 |
|
&& !$normalizationContext->isFieldIncluded('_metadata.has_next') |
53
|
27 |
|
&& !$normalizationContext->isFieldIncluded('_metadata.has_previous') |
54
|
27 |
|
&& !$normalizationContext->isFieldIncluded('cursors') |
55
|
|
|
) { |
56
|
1 |
|
$totalCount = $this->resultProvider->getTotalCountForQuery($configuredQuery); |
57
|
1 |
|
return (new Result()) |
58
|
1 |
|
->setTotalCount($totalCount) |
59
|
|
|
; |
60
|
|
|
} |
61
|
|
|
|
62
|
26 |
|
$strategy = $pagedQuery->getTotalCountStrategy(); |
63
|
26 |
|
if ($strategy === PagedQuery::TOTAL_COUNT_STRATEGY_DEFAULT) { |
64
|
24 |
|
$strategy = $this->defaultTotalCountStrategy; |
65
|
|
|
} |
66
|
|
|
$totalIncluded = ( |
67
|
26 |
|
$strategy === PagedQuery::TOTAL_COUNT_STRATEGY_ALWAYS |
68
|
7 |
|
&& $normalizationContext->isFieldIncluded('_metadata.total') |
69
|
|
|
) || ( |
70
|
21 |
|
$strategy === PagedQuery::TOTAL_COUNT_STRATEGY_OPTIONAL |
71
|
26 |
|
&& $normalizationContext->isFieldExplicitlyIncluded('_metadata.total') |
72
|
|
|
); |
73
|
|
|
|
74
|
26 |
|
$configuredQuery->setTotalCountNeeded($totalIncluded); |
75
|
|
|
|
76
|
|
|
if ( |
77
|
26 |
|
!$configuredQuery->hasMaximumOffset() |
78
|
26 |
|
&& $this->maximumOffset !== null |
79
|
26 |
|
&& $pagedQuery->getTotalCountStrategy() !== PagedQuery::TOTAL_COUNT_STRATEGY_ALWAYS |
80
|
|
|
) { |
81
|
16 |
|
$configuredQuery->setMaximumOffset($this->maximumOffset); |
82
|
|
|
} |
83
|
|
|
|
84
|
26 |
|
return $this->resultProvider->getResultForQuery($configuredQuery, $pagedQuery->getPager()); |
85
|
|
|
} |
86
|
|
|
|
87
|
96 |
|
public function getType(): string |
88
|
|
|
{ |
89
|
96 |
|
return PagedQuery::class; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|