TRecordsInJoins   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 31
c 0
b 0
f 0
dl 0
loc 65
ccs 33
cts 33
cp 1
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getRecordsInJoin() 0 3 1
B recordLookup() 0 30 6
A initRecordLookup() 0 10 1
1
<?php
2
3
namespace kalanis\kw_mapper\Search\Connector\Database;
4
5
6
use kalanis\kw_mapper\MapperException;
7
use kalanis\kw_mapper\Records\ARecord;
8
use ReflectionClass;
9
use ReflectionException;
10
11
12
/**
13
 * Trait TRecordsInJoins
14
 * @package kalanis\kw_mapper\Search\Connector\Database
15
 * Which records are in selection
16
 */
17
trait TRecordsInJoins
18
{
19
    /** @var RecordsInJoin[] */
20
    protected array $recordsInJoin = [];
21
22
    /**
23
     * @param ARecord $record
24
     * @throws MapperException
25
     */
26 82
    public function initRecordLookup(ARecord $record): void
27
    {
28 82
        $rec = new RecordsInJoin();
29 82
        $rec->setData(
30 82
            $record,
31 82
            $record->getMapper()->getAlias(),
32 82
            null,
33 82
            ''
34 82
        );
35 82
        $this->recordsInJoin[$record->getMapper()->getAlias()] = $rec;
36
    }
37
38
    /**
39
     * @param string $storeKey
40
     * @param string $knownAs
41
     * @throws MapperException
42
     * @return RecordsInJoin|null
43
     */
44 15
    public function recordLookup(string $storeKey, string $knownAs = ''): ?RecordsInJoin
45
    {
46 15
        if (isset($this->recordsInJoin[$storeKey])) {
47 10
            return $this->recordsInJoin[$storeKey];
48
        }
49 10
        foreach ($this->recordsInJoin as $record) {
50 10
            $foreignKeys = $record->getRecord()->getMapper()->getForeignKeys();
51 10
            $fk = empty($knownAs) ? $storeKey : $knownAs ;
52 10
            if (isset($foreignKeys[$fk])) {
53 9
                $recordClassName = $foreignKeys[$fk]->getRemoteRecord();
54
                try {
55
                    /** @var class-string $recordClassName */
56 9
                    $reflect = new ReflectionClass($recordClassName);
57 8
                    $thatRecord = $reflect->newInstance();
58 1
                } catch (ReflectionException $ex) {
59 1
                    throw new MapperException($ex->getMessage(), $ex->getCode(), $ex);
60
                }
61
                /** @var ARecord $thatRecord */
62 8
                $rec = new RecordsInJoin();
63 8
                $rec->setData(
64 8
                    $thatRecord,
65 8
                    $storeKey,
66 8
                    $record->getRecord()->getMapper()->getAlias(),
67 8
                    $knownAs
68 8
                );
69 8
                $this->recordsInJoin[$storeKey] = $rec;
70 8
                return $this->recordsInJoin[$storeKey];
71
            }
72
        }
73 2
        return null;
74
    }
75
76
    /**
77
     * @return RecordsInJoin[]
78
     */
79 1
    public function getRecordsInJoin(): array
80
    {
81 1
        return $this->recordsInJoin;
82
    }
83
}
84