1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Query\Pagination\WalkStrategy; |
4
|
|
|
|
5
|
|
|
use Bdf\Prime\Collection\CollectionInterface; |
6
|
|
|
use Bdf\Prime\Query\Contract\Limitable; |
7
|
|
|
use Bdf\Prime\Query\Contract\Orderable; |
8
|
|
|
use Bdf\Prime\Query\Contract\ReadOperation; |
9
|
|
|
use Bdf\Prime\Query\Contract\Whereable; |
10
|
|
|
use Bdf\Prime\Query\ReadCommandInterface; |
11
|
|
|
use InvalidArgumentException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Walk strategy using a primary key (or any unique key) as cursor |
15
|
|
|
* This strategy supports deleting entities during the walk, but the entity must contains a single primary key, and the query must be ordered by this key |
16
|
|
|
* Any sort on other attribute are not supported |
|
|
|
|
17
|
|
|
*/ |
18
|
|
|
final class KeyWalkStrategy implements WalkStrategyInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var KeyInterface |
22
|
|
|
*/ |
23
|
|
|
private $key; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* PrimaryKeyWalkStrategy constructor. |
27
|
|
|
* @param KeyInterface $key |
|
|
|
|
28
|
|
|
*/ |
29
|
16 |
|
public function __construct(KeyInterface $key) |
30
|
|
|
{ |
31
|
16 |
|
$this->key = $key; |
32
|
16 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
|
|
|
|
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
|
|
|
|
37
|
13 |
|
public function initialize(ReadCommandInterface $query, int $chunkSize, int $startPage): WalkCursor |
38
|
|
|
{ |
39
|
13 |
|
if (!self::supports($query, $startPage, $this->key->name())) { |
40
|
3 |
|
throw new InvalidArgumentException('KeyWalkStrategy is not supported by this query'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** @var Limitable&Orderable&ReadCommandInterface $query */ |
|
|
|
|
44
|
10 |
|
$query = clone $query; |
45
|
|
|
|
46
|
10 |
|
if (!isset($query->getOrders()[$this->key->name()])) { |
47
|
10 |
|
$query->order($this->key->name(), Orderable::ORDER_ASC); |
48
|
|
|
} |
49
|
|
|
|
50
|
10 |
|
$query->limit($chunkSize); |
51
|
|
|
|
52
|
10 |
|
return new WalkCursor($query); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
#[ReadOperation] |
|
|
|
|
59
|
10 |
|
public function next(WalkCursor $cursor): WalkCursor |
|
|
|
|
60
|
|
|
{ |
61
|
10 |
|
$cursor = clone $cursor; |
62
|
|
|
|
63
|
10 |
|
if ($cursor->entities) { |
64
|
8 |
|
$cursor->cursor = $this->key->get(end($cursor->entities)); |
65
|
|
|
} |
66
|
|
|
|
67
|
10 |
|
if ($cursor->cursor !== null) { |
68
|
8 |
|
$operator = $cursor->query->getOrders()[$this->key->name()] === Orderable::ORDER_ASC ? '>' : '<'; |
|
|
|
|
69
|
8 |
|
$cursor->query->where($this->key->name(), $operator, $cursor->cursor); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
10 |
|
$cursor->entities = $cursor->query->all(); |
73
|
|
|
|
74
|
10 |
|
if ($cursor->entities instanceof CollectionInterface) { |
|
|
|
|
75
|
|
|
$cursor->entities = $cursor->entities->all(); |
76
|
|
|
} |
77
|
|
|
|
78
|
10 |
|
return $cursor; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Check if the strategy supports the given parameters |
83
|
|
|
* |
84
|
|
|
* @param ReadCommandInterface $query The query |
85
|
|
|
* @param int|null $startPage The start page |
|
|
|
|
86
|
|
|
* @param string $key The cursor key |
87
|
|
|
* |
88
|
|
|
* @return bool |
|
|
|
|
89
|
|
|
* |
90
|
|
|
* @psalm-assert-if-true Orderable&Limitable&Whereable $query |
91
|
|
|
*/ |
92
|
16 |
|
public static function supports(ReadCommandInterface $query, ?int $startPage, string $key): bool |
93
|
|
|
{ |
94
|
16 |
|
if ($startPage !== null && $startPage !== 1) { |
95
|
3 |
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
14 |
|
if (!($query instanceof Orderable && $query instanceof Limitable && $query instanceof Whereable)) { |
99
|
2 |
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
13 |
|
$orders = $query->getOrders(); |
103
|
|
|
|
104
|
13 |
|
return empty($orders) || (count($orders) === 1 && isset($orders[$key])); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|