Skip to content

Commit 1fd77b1

Browse files
authored
Add a reader decorator that allows adding array metadata to decorated read data (#30)
1 parent b4d335b commit 1fd77b1

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yokai\Batch\Job\Item\Reader;
6+
7+
use Generator;
8+
use Yokai\Batch\Exception\UnexpectedValueException;
9+
use Yokai\Batch\Job\Item\ElementConfiguratorTrait;
10+
use Yokai\Batch\Job\Item\FlushableInterface;
11+
use Yokai\Batch\Job\Item\InitializableInterface;
12+
use Yokai\Batch\Job\Item\ItemReaderInterface;
13+
use Yokai\Batch\Job\JobExecutionAwareInterface;
14+
use Yokai\Batch\Job\JobExecutionAwareTrait;
15+
16+
final class AddMetadataReader implements
17+
ItemReaderInterface,
18+
InitializableInterface,
19+
FlushableInterface,
20+
JobExecutionAwareInterface
21+
{
22+
use ElementConfiguratorTrait;
23+
use JobExecutionAwareTrait;
24+
25+
private ItemReaderInterface $reader;
26+
27+
/**
28+
* @phpstan-var array<string, mixed>
29+
*/
30+
private array $metadata;
31+
32+
/**
33+
* @phpstan-param array<string, mixed> $metadata
34+
*/
35+
public function __construct(ItemReaderInterface $reader, array $metadata)
36+
{
37+
$this->reader = $reader;
38+
$this->metadata = $metadata;
39+
}
40+
41+
/**
42+
* @inheritDoc
43+
*/
44+
public function initialize(): void
45+
{
46+
$this->configureElementJobContext($this->reader, $this->jobExecution);
47+
$this->initializeElement($this->reader);
48+
}
49+
50+
/**
51+
* @inheritdoc
52+
* @phpstan-return Generator<array<mixed>>
53+
*/
54+
public function read(): Generator
55+
{
56+
foreach ($this->reader->read() as $item) {
57+
if (!\is_array($item)) {
58+
throw UnexpectedValueException::type('array', $item);
59+
}
60+
61+
yield $this->metadata + $item;
62+
}
63+
}
64+
65+
/**
66+
* @inheritDoc
67+
*/
68+
public function flush(): void
69+
{
70+
$this->flushElement($this->reader);
71+
}
72+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yokai\Batch\Tests\Job\Item\Reader;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Yokai\Batch\Exception\UnexpectedValueException;
9+
use Yokai\Batch\Job\Item\Reader\AddMetadataReader;
10+
use Yokai\Batch\Job\Item\Reader\StaticIterableReader;
11+
use Yokai\Batch\JobExecution;
12+
13+
class AddMetadataReaderTest extends TestCase
14+
{
15+
public function testRead(): void
16+
{
17+
$reader = new AddMetadataReader(
18+
new StaticIterableReader([['name' => 'John'], ['name' => 'Jane']]),
19+
['_type' => 'user']
20+
);
21+
22+
$reader->setJobExecution(JobExecution::createRoot('123456', 'test'));
23+
$reader->initialize();
24+
$read = \iterator_to_array($reader->read());
25+
$reader->flush();
26+
27+
self::assertSame(
28+
[['_type' => 'user', 'name' => 'John'], ['_type' => 'user', 'name' => 'Jane']],
29+
$read
30+
);
31+
}
32+
33+
public function testReadDecoratedNotReturningAnArray(): void
34+
{
35+
$this->expectException(UnexpectedValueException::class);
36+
37+
$reader = new AddMetadataReader(
38+
new StaticIterableReader(['string']),
39+
['_type' => 'user']
40+
);
41+
42+
$reader->setJobExecution(JobExecution::createRoot('123456', 'test'));
43+
$reader->initialize();
44+
\iterator_to_array($reader->read());
45+
}
46+
}

0 commit comments

Comments
 (0)