-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathCustomerDataSource.php
More file actions
82 lines (70 loc) · 2.55 KB
/
Copy pathCustomerDataSource.php
File metadata and controls
82 lines (70 loc) · 2.55 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
<?php
declare(strict_types=1);
namespace Setono\SyliusFeedPlugin\DataSource;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;
use Setono\Doctrine\ORMTrait;
use Setono\SyliusFeedPlugin\Context\FeedContext;
use Setono\SyliusFeedPlugin\Doctrine\BatchIterator;
use Setono\SyliusFeedPlugin\Filter\FilterSet;
use Setono\SyliusFeedPlugin\Generator\ChunkRange;
/**
* Streams every customer — one row per customer (§8.3). Unlike a product, a customer has no
* enabled/state concept to guard on, and the feed type declares no scope dimensions, so this data
* source needs neither a query-level filter nor a context-dependent constraint: it is the
* thinnest possible data source, proving the engine does not require either (§6.3, §8.3).
* Iterates in bounded-memory batches via {@see BatchIterator} (clears the entity manager every
* batch).
*/
final class CustomerDataSource implements DataSourceInterface
{
use ORMTrait;
use IdRangePartitionTrait;
private const BATCH_SIZE = 1000;
/**
* @param class-string $resourceClass
*/
public function __construct(
ManagerRegistry $managerRegistry,
private readonly string $resourceClass,
) {
$this->managerRegistry = $managerRegistry;
}
public function getResourceClass(): string
{
return $this->resourceClass;
}
public function getItems(FeedContext $context, FilterSet $filters): iterable
{
return BatchIterator::iterate(
$this->createQueryBuilder()->getQuery(),
$this->getManager($this->resourceClass),
self::BATCH_SIZE,
);
}
public function count(FeedContext $context, FilterSet $filters): int
{
return (int) $this->createQueryBuilder()
->select('COUNT(c.id)')
->getQuery()
->getSingleScalarResult();
}
public function getIdRange(FeedContext $context, FilterSet $filters): ?ChunkRange
{
return $this->resolveIdRange($this->createQueryBuilder(), 'c');
}
public function getItemsInRange(FeedContext $context, FilterSet $filters, ChunkRange $range): iterable
{
return BatchIterator::iterate(
$this->constrainToRange($this->createQueryBuilder(), 'c', $range)->getQuery(),
$this->getManager($this->resourceClass),
self::BATCH_SIZE,
);
}
private function createQueryBuilder(): QueryBuilder
{
return $this->getManager($this->resourceClass)
->getRepository($this->resourceClass)
->createQueryBuilder('c');
}
}