UniqueIndexStrategy   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 44
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getRecordIdentifier() 0 21 5
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