ConfigStrategy::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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