TEntryLookup   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 95%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 48
ccs 19
cts 20
cp 0.95
rs 10
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A getEntry() 0 35 6
1
<?php
2
3
namespace kalanis\kw_files_mapper\Processing\Mapper;
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\Mapper
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 64
    protected function getEntry(array $path, ?ARecord $parentNode = null): ?ARecord
28
    {
29 64
        if (empty($path)) {
30 18
            $search = new Search($this->getLookupRecord());
31 17
            $search->null($this->getTranslation()->getParentKey());
32 17
            $all = $search->getResults();
33
34 17
            if (empty($all)) {
35
                // no root node?!?!
36
                // @codeCoverageIgnoreStart
37
                return null;
38
            }
39
            // @codeCoverageIgnoreEnd
40
41 17
            return reset($all);
42
        }
43
44 61
        foreach ($path as $levelKey) {
45 61
            $search = new Search($this->getLookupRecord());
46 43
            $search->exact($this->getTranslation()->getCurrentKey(), strval($levelKey));
47 43
            if ($parentNode) {
48 20
                $search->exact(
49 20
                    $this->getTranslation()->getParentKey(),
50 20
                    strval($parentNode->__get($this->getTranslation()->getPrimaryKey()))
51
                );
52
            }
53 43
            $all = $search->getResults();
54 43
            if (empty($all)) {
55 27
                return null;
56
            }
57
58 33
            $parentNode = reset($all);
59
        }
60
61 31
        return $parentNode;
62
    }
63
64
    abstract protected function getLookupRecord(): ARecord;
65
}
66