1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overblog\GraphQLBundle\Relay\Connection; |
4
|
|
|
|
5
|
|
|
use Overblog\GraphQLBundle\Definition\Argument; |
6
|
|
|
use Overblog\GraphQLBundle\Relay\Connection\Output\Connection; |
7
|
|
|
use Overblog\GraphQLBundle\Relay\Connection\Output\ConnectionBuilder; |
8
|
|
|
|
9
|
|
|
class Paginator |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var callable |
13
|
|
|
*/ |
14
|
|
|
private $fetcher; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param callable $fetcher |
18
|
|
|
*/ |
19
|
5 |
|
public function __construct(callable $fetcher) |
20
|
|
|
{ |
21
|
5 |
|
$this->fetcher = $fetcher; |
22
|
5 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param Argument|array $args |
26
|
|
|
* @param int|callable $total |
27
|
|
|
* |
28
|
|
|
* @return Connection |
29
|
|
|
*/ |
30
|
3 |
View Code Duplication |
public function backward($args, $total) |
|
|
|
|
31
|
|
|
{ |
32
|
3 |
|
$args = $this->protectArgs($args); |
33
|
3 |
|
$limit = $args['last']; |
34
|
3 |
|
$offset = max(0, ConnectionBuilder::getOffsetWithDefault($args['before'], $total) - $limit); |
|
|
|
|
35
|
|
|
|
36
|
3 |
|
$entities = call_user_func($this->fetcher, $offset, $limit); |
37
|
|
|
|
38
|
3 |
|
return ConnectionBuilder::connectionFromArraySlice($entities, $args, [ |
39
|
3 |
|
'sliceStart' => $offset, |
40
|
3 |
|
'arrayLength' => $total, |
41
|
3 |
|
]); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param Argument|array $args |
46
|
|
|
* |
47
|
|
|
* @return Connection |
48
|
|
|
*/ |
49
|
3 |
View Code Duplication |
public function forward($args) |
|
|
|
|
50
|
|
|
{ |
51
|
3 |
|
$args = $this->protectArgs($args); |
52
|
3 |
|
$limit = $args['first']; |
53
|
3 |
|
$offset = ConnectionBuilder::getOffsetWithDefault($args['after'], 0); |
54
|
|
|
|
55
|
|
|
// The extra fetched element is here to determine if there is a next page. |
56
|
3 |
|
$entities = call_user_func($this->fetcher, $offset, $limit + 1); |
57
|
|
|
|
58
|
3 |
|
return ConnectionBuilder::connectionFromArraySlice($entities, $args, [ |
59
|
3 |
|
'sliceStart' => $offset, |
60
|
3 |
|
'arrayLength' => $offset + count($entities), |
61
|
3 |
|
]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param Argument|array $args |
66
|
|
|
* @param int|callable $total |
67
|
|
|
* |
68
|
|
|
* @return Connection |
69
|
|
|
*/ |
70
|
1 |
|
public function auto($args, $total) |
71
|
|
|
{ |
72
|
1 |
|
$args = $this->protectArgs($args); |
73
|
|
|
|
74
|
1 |
|
if ($args['last']) { |
75
|
1 |
|
return $this->backward($args, is_callable($total) ? call_user_func($total) : $total); |
76
|
|
|
} else { |
77
|
1 |
|
return $this->forward($args); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param Argument|array $args |
83
|
|
|
* |
84
|
|
|
* @return Argument |
85
|
|
|
*/ |
86
|
5 |
|
private function protectArgs($args) |
87
|
|
|
{ |
88
|
5 |
|
return $args instanceof Argument ? $args : new Argument($args); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.