JobMetadataEnricher::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
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