TEntryLookup   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 38
ccs 15
cts 15
cp 1
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A getEntry() 0 25 5
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