Passed
Push — master ( c85748...2fd0ab )
by Petr
08:11
created

TForeignKey::initClassFks()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
1
<?php
2
3
namespace kalanis\kw_mapper\Mappers\Shared;
4
5
6
/**
7
 * Trait TForeignKey
8
 * @package kalanis\kw_mapper\Mappers\Shared
9
 * Accessing foreign keys
10
 *
11
 * @todo idea: fk from/to composite keys
12
 *     - shall be like array where the first entry is what will join and then what entry aliases will be used
13
 */
14
trait TForeignKey
15
{
16
    /** @var array<string, ForeignKey> */
17
    protected $foreignKeys = [];
18
    /** @var ForeignKey|null */
19
    private $foreignKeyClass = null;
20
21 7
    public function addForeignKey(string $localAlias, string $remoteRecord, string $localEntryKey, string $remoteEntryKey): void
22
    {
23 7
        $this->initClassFks();
24 7
        $foreignKeyClass = clone $this->foreignKeyClass;
25 7
        $this->foreignKeys[$localAlias] = $foreignKeyClass->setData($localAlias, $remoteRecord, $localEntryKey, $remoteEntryKey);
26
    }
27
28 7
    private function initClassFks(): void
29
    {
30 7
        if (empty($this->foreignKeyClass)) {
31 7
            $this->foreignKeyClass = new ForeignKey();
32
        }
33
    }
34
35
    /**
36
     * @return array<string, ForeignKey>
37
     */
38 11
    public function getForeignKeys(): array
39
    {
40 11
        return $this->foreignKeys;
41
    }
42
}
43