-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDataObjectFetcher.php
More file actions
143 lines (109 loc) · 3.59 KB
/
DataObjectFetcher.php
File metadata and controls
143 lines (109 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
namespace SilverStripe\Forager\DataObject;
use InvalidArgumentException;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Forager\Interfaces\DocumentFetcherInterface;
use SilverStripe\Forager\Interfaces\DocumentInterface;
use SilverStripe\Forager\Service\IndexConfiguration;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject;
class DataObjectFetcher implements DocumentFetcherInterface
{
use Configurable;
use Injectable;
private ?string $dataObjectClass = null;
private int $batchSize;
private int $offset = 0;
private static string $fetch_sort = 'ID';
private static string $fetch_sort_direction = 'ASC';
public function __construct(string $class)
{
if (!is_subclass_of($class, DataObject::class)) {
throw new InvalidArgumentException(sprintf(
'%s is not a subclass of %s',
$class,
DataObject::class
));
}
$this->dataObjectClass = $class;
// Default batch size is whatever has been configured for this class (or the default index batch size)
$this->batchSize = IndexConfiguration::singleton()->getLowestBatchSizeForClass($class);
}
public function getBatchSize(): int
{
return $this->batchSize;
}
public function setBatchSize(int $batchSize): void
{
$this->batchSize = $batchSize;
}
public function getOffset(): int
{
return $this->offset;
}
public function setOffset(int $offset): void
{
$this->offset = $offset;
}
public function incrementOffsetUp(): void
{
$this->offset += $this->batchSize;
}
public function incrementOffsetDown(): void
{
// Never go below 0
$this->offset = max(0, $this->offset - $this->batchSize);
}
/**
* @return DocumentInterface[]
*/
public function fetch(): array
{
// get configurable sort options
$sortBy = static::config()->get('fetch_sort') ?? 'ID';
$sortDirection = static::config()->get('fetch_sort_direction') ?? 'ASC';
// sort (default by ID) to ensure consistent ordering across batches
$list = $this->createDataList($this->getBatchSize(), $this->getOffset())
->sort($sortBy, $sortDirection);
$docs = [];
foreach ($list as $record) {
$docs[] = DataObjectDocument::create($record);
}
return $docs;
}
public function getTotalDocuments(): int
{
return $this->createDataList()->count();
}
public function getTotalBatches(): int
{
$total = $this->getTotalDocuments();
if ($total === 0) {
return 0;
}
return max(1, (int) ceil($total / $this->getBatchSize()));
}
public function createDocument(array $data): ?DocumentInterface
{
$idField = DataObjectDocument::config()->get('record_id_field');
$ID = $data[$idField] ?? null;
if (!$ID) {
throw new InvalidArgumentException(sprintf(
'No %s field found: %s',
$idField,
print_r($data, true)
));
}
$dataObject = DataObject::get_by_id($this->dataObjectClass, $ID);
if (!$dataObject) {
return null;
}
return DataObjectDocument::create($dataObject);
}
private function createDataList(?int $limit = null, ?int $offset = 0): DataList
{
$list = DataList::create($this->dataObjectClass);
return $list->limit($limit, $offset);
}
}