-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from Setono/batch-iterator
Create batch iterator
- Loading branch information
Showing
9 changed files
with
164 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusCalloutPlugin\BatchIterator; | ||
|
||
use Doctrine\ORM\QueryBuilder; | ||
use Doctrine\Persistence\ManagerRegistry; | ||
use DoctrineBatchUtils\BatchProcessing\SimpleBatchIteratorAggregate; | ||
use Psr\EventDispatcher\EventDispatcherInterface; | ||
use Setono\DoctrineObjectManagerTrait\ORM\ORMManagerTrait; | ||
use Setono\SyliusCalloutPlugin\Event\BatchIteratorEvent; | ||
|
||
/** | ||
* @template T | ||
* | ||
* @implements BatchIteratorInterface<T> | ||
*/ | ||
final class BatchIterator implements BatchIteratorInterface | ||
{ | ||
use ORMManagerTrait; | ||
|
||
/** @var list<callable> */ | ||
private array $modifications = []; | ||
|
||
/** | ||
* @param class-string<T> $class | ||
*/ | ||
public function __construct( | ||
private readonly EventDispatcherInterface $eventDispatcher, | ||
ManagerRegistry $managerRegistry, | ||
private readonly string $class, | ||
) { | ||
$this->managerRegistry = $managerRegistry; | ||
} | ||
|
||
/** | ||
* @param callable(QueryBuilder): void $callable | ||
*/ | ||
public function modify(callable $callable): static | ||
{ | ||
$this->modifications[] = $callable; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return SimpleBatchIteratorAggregate<array-key, T> | ||
*/ | ||
public function getIterator(): SimpleBatchIteratorAggregate | ||
{ | ||
$manager = $this->getManager($this->class); | ||
$qb = $manager | ||
->createQueryBuilder() | ||
->select('o') | ||
->from($this->class, 'o'); | ||
|
||
foreach ($this->modifications as $modification) { | ||
$modification($qb); | ||
} | ||
|
||
$this->eventDispatcher->dispatch(new BatchIteratorEvent($qb, $this->class)); | ||
|
||
/** @var SimpleBatchIteratorAggregate<array-key, T> $iterator */ | ||
$iterator = SimpleBatchIteratorAggregate::fromQuery($qb->getQuery(), 100); | ||
|
||
return $iterator; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusCalloutPlugin\BatchIterator; | ||
|
||
use Doctrine\Persistence\ManagerRegistry; | ||
use Psr\EventDispatcher\EventDispatcherInterface; | ||
use Setono\DoctrineObjectManagerTrait\ORM\ORMManagerTrait; | ||
|
||
final class BatchIteratorFactory implements BatchIteratorFactoryInterface | ||
{ | ||
use ORMManagerTrait; | ||
|
||
public function __construct( | ||
private readonly EventDispatcherInterface $eventDispatcher, | ||
ManagerRegistry $managerRegistry, | ||
) { | ||
$this->managerRegistry = $managerRegistry; | ||
} | ||
|
||
public function create(string $class): BatchIteratorInterface | ||
{ | ||
return new BatchIterator($this->eventDispatcher, $this->managerRegistry, $class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusCalloutPlugin\BatchIterator; | ||
|
||
interface BatchIteratorFactoryInterface | ||
{ | ||
/** | ||
* @template T | ||
* | ||
* @param class-string<T> $class | ||
* | ||
* @return BatchIteratorInterface<T> | ||
*/ | ||
public function create(string $class): BatchIteratorInterface; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusCalloutPlugin\BatchIterator; | ||
|
||
use Doctrine\ORM\QueryBuilder; | ||
|
||
/** | ||
* @template T | ||
* | ||
* @extends \IteratorAggregate<array-key, T> | ||
*/ | ||
interface BatchIteratorInterface extends \IteratorAggregate | ||
{ | ||
/** | ||
* @param callable(QueryBuilder): void $callable | ||
*/ | ||
public function modify(callable $callable): static; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
<services> | ||
<service id="setono_sylius_callout.batch_iterator.factory" | ||
class="Setono\SyliusCalloutPlugin\BatchIterator\BatchIteratorFactory"> | ||
<argument type="service" id="event_dispatcher"/> | ||
<argument type="service" id="doctrine"/> | ||
</service> | ||
</services> | ||
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters