Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/batch/docs/domain/item-job/item-writer.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ It can be any class implementing [ItemWriterInterface](../../../src/Job/Item/Ite
write items as a json string each on a line of a file.
- [ChainWriter](../../../src/Job/Item/Writer/ChainWriter.php):
write items on multiple item writers.
- [ConditionalWriter](../../../src/Job/Item/Writer/ConditionalWriter.php):
will only write items that are matching your conditions.
- [NullWriter](../../../src/Job/Item/Writer/NullWriter.php):
do not write items.
- [RoutingWriter](../../../src/Job/Item/Writer/RoutingWriter.php):
Expand Down
70 changes: 70 additions & 0 deletions src/batch/src/Job/Item/Writer/ConditionalWriter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace Yokai\Batch\Job\Item\Writer;

use Yokai\Batch\Job\Item\ElementConfiguratorTrait;
use Yokai\Batch\Job\Item\FlushableInterface;
use Yokai\Batch\Job\Item\InitializableInterface;
use Yokai\Batch\Job\Item\ItemWriterInterface;
use Closure;
use Yokai\Batch\Job\JobExecutionAwareInterface;
use Yokai\Batch\Job\JobExecutionAwareTrait;

/**
* This {@see ItemWriterInterface} will transfer writing
* to another {@see ItemWriterInterface},
* if the closure you provided tells to.
*/
final class ConditionalWriter implements
ItemWriterInterface,
JobExecutionAwareInterface,
InitializableInterface,
FlushableInterface
{
use JobExecutionAwareTrait;
use ElementConfiguratorTrait;

private bool $initialized = false;

public function __construct(
private Closure $shouldWrite,
private ItemWriterInterface $writer,
) {
}

public function write(iterable $items): void
{
$keptItems = [];
foreach ($items as $item) {
if (($this->shouldWrite)($item, $this->jobExecution)) {
$keptItems[] = $item;
}
}

if ($keptItems === []) {
return;
}

if (!$this->initialized) {
$this->configureElementJobContext($this->writer, $this->jobExecution);
$this->initializeElement($this->writer);
$this->initialized = true;
}

$this->writer->write($keptItems);
}

public function initialize(): void
{
$this->initialized = false;
}

public function flush(): void
{
if ($this->initialized) {
$this->flushElement($this->writer);
}
}
}
50 changes: 50 additions & 0 deletions src/batch/tests/Job/Item/Writer/ConditionalWriterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Yokai\Batch\Tests\Job\Item\Writer;

use Yokai\Batch\Job\Item\Writer\ConditionalWriter;
use PHPUnit\Framework\TestCase;
use Yokai\Batch\JobExecution;
use Yokai\Batch\Test\Job\Item\Writer\InMemoryWriter;
use Yokai\Batch\Test\Job\Item\Writer\TestDebugWriter;

class ConditionalWriterTest extends TestCase
{
public function testWriteSomething(): void
{
$writer = new ConditionalWriter(
fn (int $number) => ($number % 2) === 0,
$debugWriter = new TestDebugWriter($memoryWriter = new InMemoryWriter())
);

$writer->setJobExecution(JobExecution::createRoot('123', 'test.conditional_writer'));
$writer->initialize();
$writer->write([1, 2, 3, 4, 5]);
$writer->write([6, 7, 8]);
$writer->flush();

$debugWriter->assertWasConfigured();
$debugWriter->assertWasUsed();
self::assertSame([[2, 4], [6, 8]], $memoryWriter->getBatchItems());
}

public function testWriteNothing(): void
{
$writer = new ConditionalWriter(
fn () => false,
$debugWriter = new TestDebugWriter($memoryWriter = new InMemoryWriter())
);

$writer->setJobExecution(JobExecution::createRoot('123', 'test.conditional_writer'));
$writer->initialize();
$writer->write([1, 2, 3, 4, 5]);
$writer->write([6, 7, 8]);
$writer->flush();

$debugWriter->assertWasNotConfigured();
$debugWriter->assertWasNotUsed();
self::assertSame([], $memoryWriter->getBatchItems());
}
}