Skip to content

Commit 3c66c6c

Browse files
authored
Introduce JobParameterAccessor to help job components access the required execution parameters (#26)
* Introduce JobParameterAccessor to help job components access the required execution parameters * Refactored FlatFileReader & FlatFileWriter to use a JobParameterAccessorInterface instead of fetching file path from JobParameters * Introduce more job parameter accessors, mostly decorators able to move in JobExecution graph * Added more tests to cover FlatFileReader & FlatFileWriter * Add missing tests cases for job parameters accessors * Allow extra variables from constructor in ReplaceWithVariablesParameterAccessor * Fixed PHPStan issue with array type
1 parent a53932b commit 3c66c6c

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

src/ContainerParameterAccessor.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yokai\Batch\Bridge\Symfony\Framework;
6+
7+
use Symfony\Component\DependencyInjection\ContainerInterface;
8+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
9+
use Yokai\Batch\Exception\CannotAccessParameterException;
10+
use Yokai\Batch\Job\Parameters\JobParameterAccessorInterface;
11+
use Yokai\Batch\JobExecution;
12+
13+
/**
14+
* This job parameter accessor implementation returns container parameter value.
15+
*/
16+
final class ContainerParameterAccessor implements JobParameterAccessorInterface
17+
{
18+
private ContainerInterface $container;
19+
private string $name;
20+
21+
public function __construct(ContainerInterface $container, string $name)
22+
{
23+
$this->container = $container;
24+
$this->name = $name;
25+
}
26+
27+
/**
28+
* @inheritdoc
29+
*/
30+
public function get(JobExecution $execution)
31+
{
32+
try {
33+
return $this->container->getParameter($this->name);
34+
} catch (InvalidArgumentException $exception) {
35+
throw new CannotAccessParameterException(
36+
\sprintf('Cannot access "%s" parameter from container parameters', $this->name),
37+
$exception
38+
);
39+
}
40+
}
41+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yokai\Batch\Tests\Bridge\Symfony\Framework;
6+
7+
use Symfony\Component\DependencyInjection\Container;
8+
use Yokai\Batch\Bridge\Symfony\Framework\ContainerParameterAccessor;
9+
use PHPUnit\Framework\TestCase;
10+
use Yokai\Batch\Exception\CannotAccessParameterException;
11+
use Yokai\Batch\JobExecution;
12+
13+
class ContainerParameterAccessorTest extends TestCase
14+
{
15+
public function test(): void
16+
{
17+
$container = new Container();
18+
$container->setParameter('some.parameter', 'foo');
19+
$accessor = new ContainerParameterAccessor($container, 'some.parameter');
20+
21+
self::assertSame('foo', $accessor->get(JobExecution::createRoot('123', 'testing')));
22+
}
23+
24+
public function testParameterNotFound(): void
25+
{
26+
$this->expectException(CannotAccessParameterException::class);
27+
$container = new Container();
28+
$container->setParameter('some.parameter', 'foo');
29+
$accessor = new ContainerParameterAccessor($container, 'undefined.parameter');
30+
$accessor->get(JobExecution::createRoot('123', 'testing'));
31+
}
32+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yokai\Batch\Tests\Bridge\Symfony\Framework\Job\Parameters;
6+
7+
use Yokai\Batch\Exception\CannotAccessParameterException;
8+
use Yokai\Batch\Job\Parameters\JobExecutionSummaryAccessor;
9+
use PHPUnit\Framework\TestCase;
10+
use Yokai\Batch\JobExecution;
11+
12+
class JobExecutionSummaryAccessorTest extends TestCase
13+
{
14+
public function test(): void
15+
{
16+
$accessor = new JobExecutionSummaryAccessor('report');
17+
18+
$execution = JobExecution::createRoot('123', 'testing');
19+
$execution->getSummary()->set('report', 42);
20+
self::assertSame(42, $accessor->get($execution));
21+
22+
$execution = JobExecution::createRoot('123', 'testing');
23+
$execution->getSummary()->set('anything.else', 0);
24+
$execution->getSummary()->set('report', 1042);
25+
self::assertSame(1042, $accessor->get($execution));
26+
}
27+
28+
public function testNotFound(): void
29+
{
30+
$this->expectException(CannotAccessParameterException::class);
31+
32+
$accessor = new JobExecutionSummaryAccessor('an.undefined.summary.var');
33+
34+
$accessor->get(JobExecution::createRoot('123', 'testing'));
35+
}
36+
}

0 commit comments

Comments
 (0)