Skip to content

Commit

Permalink
refactor: batch
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Jul 16, 2024
1 parent 2e2d412 commit eb75c22
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 13 deletions.
22 changes: 12 additions & 10 deletions packages/rekapager-core/src/Batch/BatchProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,35 +60,37 @@ final public function process(
?string $resume = null,
?int $pageSize = null
): void {
// determine start page identifier

if ($resume !== null) {
$startPageIdentifier = $this->pageableIdentifierResolver
->decode($this->pageable, $resume);
} else {
$startPageIdentifier = null;
}

// prepare pages

$itemsPerPage = $pageSize ?? $this->batchProcessor->getItemsPerPage();
$pageable = $this->pageable->withItemsPerPage($itemsPerPage);
$pages = $pageable->getPages($startPageIdentifier);

// emit event

$beforeProcessEvent = new BeforeProcessEvent(
pageable: $pageable,
startPageIdentifier: $resume,
);

$this->batchProcessor->beforeProcess($beforeProcessEvent);

$pages = $pageable->getPages($startPageIdentifier);

$numOfPages = 0;
$numOfItems = 0;

foreach ($pages as $page) {
$numOfPages++;

$pageIdentifier = $page->getPageIdentifier();
$pageIdentifierString = $this->pageableIdentifierResolver->encode($pageIdentifier);

if ($this->stopFlag) {
$interruptEvent = new InterruptEvent(
pageable: $pageable,
nextPageIdentifier: $pageIdentifierString,
);

Expand All @@ -105,8 +107,6 @@ final public function process(
$this->batchProcessor->beforePage($beforePageEvent);

foreach ($page as $key => $item) {
$numOfItems++;

$itemEvent = new ItemEvent(
key: $key,
item: $item,
Expand All @@ -122,7 +122,9 @@ final public function process(
$this->batchProcessor->afterPage($afterPageEvent);
}

$afterProcessEvent = new AfterProcessEvent();
$afterProcessEvent = new AfterProcessEvent(
pageable: $pageable,
);

$this->batchProcessor->afterProcess($afterProcessEvent);
}
Expand Down
10 changes: 10 additions & 0 deletions packages/rekapager-core/src/Batch/BatchProcessorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ public function processItem(ItemEvent $itemEvent): void;
*/
public function getItemsPerPage(): int;

/**
* @param BeforeProcessEvent<TKey,T> $event
*/
public function beforeProcess(BeforeProcessEvent $event): void;

/**
* @param AfterProcessEvent<TKey,T> $event
*/
public function afterProcess(AfterProcessEvent $event): void;

/**
Expand All @@ -49,5 +56,8 @@ public function beforePage(BeforePageEvent $event): void;
*/
public function afterPage(AfterPageEvent $event): void;

/**
* @param InterruptEvent<TKey,T> $event
*/
public function onInterrupt(InterruptEvent $event): void;
}
20 changes: 19 additions & 1 deletion packages/rekapager-core/src/Batch/Event/AfterProcessEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,27 @@

namespace Rekalogika\Rekapager\Batch\Event;

use Rekalogika\Contracts\Rekapager\PageableInterface;

/**
* @template TKey of array-key
* @template T
*/
final class AfterProcessEvent
{
public function __construct()
/**
* @param PageableInterface<TKey,T> $pageable
*/
public function __construct(
private readonly PageableInterface $pageable,
) {
}

/**
* @return PageableInterface<TKey,T>
*/
public function getPageable(): PageableInterface
{
return $this->pageable;
}
}
18 changes: 18 additions & 0 deletions packages/rekapager-core/src/Batch/Event/BeforeProcessEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,19 @@

namespace Rekalogika\Rekapager\Batch\Event;

use Rekalogika\Contracts\Rekapager\PageableInterface;

/**
* @template TKey of array-key
* @template T
*/
final class BeforeProcessEvent
{
/**
* @param PageableInterface<TKey,T> $pageable
*/
public function __construct(
private readonly PageableInterface $pageable,
private readonly ?string $startPageIdentifier,
) {
}
Expand All @@ -24,4 +34,12 @@ public function getStartPageIdentifier(): ?string
{
return $this->startPageIdentifier;
}

/**
* @return PageableInterface<TKey,T>
*/
public function getPageable(): PageableInterface
{
return $this->pageable;
}
}
18 changes: 18 additions & 0 deletions packages/rekapager-core/src/Batch/Event/InterruptEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,31 @@

namespace Rekalogika\Rekapager\Batch\Event;

use Rekalogika\Contracts\Rekapager\PageableInterface;

/**
* @template TKey of array-key
* @template T
*/
final class InterruptEvent
{
/**
* @param PageableInterface<TKey,T> $pageable
*/
public function __construct(
private readonly PageableInterface $pageable,
private readonly ?string $nextPageIdentifier,
) {
}

/**
* @return PageableInterface<TKey,T>
*/
public function getPageable(): PageableInterface
{
return $this->pageable;
}

public function getNextPageIdentifier(): ?string
{
return $this->nextPageIdentifier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function beforeProcess(BeforeProcessEvent $event): void
$this->io->definitionList(
['Start page' => $event->getStartPageIdentifier() ?? '(first page)'],
['Progress file' => $this->progressFile ?? '(not used)'],
['Items per page' => $this->getItemsPerPage()],
['Items per page' => $event->getPageable()->getItemsPerPage()],
// ['Total pages' => $event->getTotalPages() ?? '(unknown)'],
// ['Total items' => $event->getTotalItems() ?? '(unknown)'],
);
Expand Down Expand Up @@ -131,7 +131,7 @@ public function onInterrupt(InterruptEvent $event): void
}

/**
* @param AfterPageEvent<TKey,T>|AfterProcessEvent|InterruptEvent $event
* @param AfterPageEvent<TKey,T>|AfterProcessEvent<TKey,T>|InterruptEvent<TKey,T> $event
* @return void
*/
private function showStats(AfterPageEvent|AfterProcessEvent|InterruptEvent $event): void
Expand Down

0 comments on commit eb75c22

Please sign in to comment.