ConfigStrategy   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getRecordIdentifier() 0 18 4
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DataFlow\Writer\Dbal\UpdateStrategy;
5
6
use SlayerBirden\DataFlow\DataBagInterface;
7
use SlayerBirden\DataFlow\Writer\Dbal\UpdateStrategyInterface;
8
9
class ConfigStrategy implements UpdateStrategyInterface
10
{
11
    /**
12
     * @var array
13
     */
14
    private $fields;
15
16 2
    public function __construct(array $fields)
17
    {
18 2
        $this->fields = $fields;
19 2
    }
20
21
    /**
22
     * Use predefined config.
23
     *
24
     * @throws InvalidConfigException
25
     * {@inheritdoc}
26
     */
27 2
    public function getRecordIdentifier(DataBagInterface $dataBag): array
28
    {
29 2
        $ids = [];
30 2
        foreach ($this->fields as $key => $value) {
31 2
            if ($value === null) {
32 2
                if (!isset($dataBag[$key])) {
33 1
                    throw new InvalidConfigException(
34 1
                        sprintf('Identifier field %s is missing in the bag: %s.', $key, json_encode($dataBag))
35
                    );
36
                }
37 1
                $ids[$key] = $dataBag[$key];
38
            } else {
39 1
                $ids[$key] = $value;
40
            }
41
        }
42
43 1
        return $ids;
44
    }
45
}
46