UniqueIndexStrategy::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
ccs 4
cts 4
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\WriterUtilityInterface;
8
use SlayerBirden\DataFlow\Writer\Dbal\UpdateStrategyInterface;
9
10
class UniqueIndexStrategy implements UpdateStrategyInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    private $table;
16
    /**
17
     * @var WriterUtilityInterface
18
     */
19
    private $utility;
20
21 4
    public function __construct(string $table, WriterUtilityInterface $utility)
22
    {
23 4
        $this->table = $table;
24 4
        $this->utility = $utility;
25 4
    }
26
27
    /**
28
     * Use table unique keys.
29
     *
30
     * {@inheritdoc}
31
     */
32 3
    public function getRecordIdentifier(DataBagInterface $dataBag): array
33
    {
34 3
        $identifiers = [];
35 3
        $indices = $this->utility->getUniqueKeys($this->table);
36 3
        foreach ($indices as $index) {
37 3
            foreach ($index->getColumns() as $column) {
38 3
                if (isset($dataBag[$column])) {
39 3
                    $identifiers[$column] = $dataBag[$column];
40
                } else {
41
                    // if one of the columns is empty, this index can not be used
42 1
                    $identifiers = [];
43 3
                    break;
44
                }
45
            }
46
            // If we have already found something to identify the record with, skip other keys
47 3
            if (!empty($identifiers)) {
48 3
                break;
49
            }
50
        }
51 3
        return $identifiers;
52
    }
53
}
54