TEntryLookup::getEntry()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 14
c 1
b 0
f 0
nc 6
nop 2
dl 0
loc 25
ccs 15
cts 15
cp 1
crap 5
rs 9.4888
1
<?php
2
3
namespace kalanis\kw_files_mapper\Processing\MapperNoRoot;
4
5
6
use kalanis\kw_files_mapper\Support\TTranslate;
7
use kalanis\kw_mapper\MapperException;
8
use kalanis\kw_mapper\Records\ARecord;
9
use kalanis\kw_mapper\Search\Search;
10
11
12
/**
13
 * Trait TEntryLookup
14
 * @package kalanis\kw_files_mapper\Processing\MapperNoRoot
15
 * Get entry
16
 */
17
trait TEntryLookup
18
{
19
    use TTranslate;
20
21
    /**
22
     * @param array<string> $path
23
     * @param ARecord|null $parentNode
24
     * @throws MapperException
25
     * @return ARecord|null
26
     */
27 65
    protected function getEntry(array $path, ?ARecord $parentNode = null): ?ARecord
28
    {
29 65
        if (empty($path)) {
30
            // no root node
31 15
            return null;
32
        }
33
34 61
        foreach ($path as $levelKey) {
35 61
            $search = new Search($this->getLookupRecord());
36 43
            $search->exact($this->getTranslation()->getCurrentKey(), strval($levelKey));
37 43
            if ($parentNode) {
38 18
                $search->exact(
39 18
                    $this->getTranslation()->getParentKey(),
40 18
                    strval($parentNode->__get($this->getTranslation()->getPrimaryKey()))
41
                );
42
            }
43 43
            $all = $search->getResults();
44 43
            if (empty($all)) {
45 27
                return null;
46
            }
47
48 33
            $parentNode = reset($all);
49
        }
50
51 31
        return $parentNode;
52
    }
53
54
    abstract protected function getLookupRecord(): ARecord;
55
}
56