1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the daikon-cqrs/async-job project. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Daikon\AsyncJob\Metadata; |
10
|
|
|
|
11
|
|
|
use Daikon\AsyncJob\Job\JobDefinitionInterface; |
12
|
|
|
use Daikon\AsyncJob\Job\JobDefinitionMap; |
13
|
|
|
use Daikon\Interop\Assertion; |
14
|
|
|
use Daikon\Metadata\MetadataInterface; |
15
|
|
|
use Daikon\Metadata\MetadataEnricherInterface; |
16
|
|
|
|
17
|
|
|
final class JobMetadataEnricher implements MetadataEnricherInterface |
18
|
|
|
{ |
19
|
|
|
public const JOB = 'job'; |
20
|
|
|
|
21
|
|
|
private JobDefinitionMap $jobDefinitionMap; |
22
|
|
|
|
23
|
|
|
private array $settings; |
24
|
|
|
|
25
|
|
|
public function __construct(JobDefinitionMap $jobDefinitionMap, array $settings = []) |
26
|
|
|
{ |
27
|
|
|
$this->jobDefinitionMap = $jobDefinitionMap; |
28
|
|
|
$this->settings = $settings; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function enrich(MetadataInterface $metadata): MetadataInterface |
32
|
|
|
{ |
33
|
|
|
Assertion::keyIsset($this->settings, 'job', 'Enricher requires a job definition to enrich metadata with.'); |
34
|
|
|
|
35
|
|
|
/** @var JobDefinitionInterface $jobDefinition */ |
36
|
|
|
$jobDefinition = $this->jobDefinitionMap->get($this->settings['job']); |
37
|
|
|
$metadata = $metadata->with(self::JOB, $this->settings['job']); |
38
|
|
|
foreach ($jobDefinition->getSettings() as $setting => $value) { |
39
|
|
|
$metadata = $metadata->with($setting, $value); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return $jobDefinition->getStrategy()->enrich($metadata); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|