Ldap::multiple()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 11
c 1
b 0
f 1
dl 0
loc 16
rs 9.6111
cc 5
nc 4
nop 0
1
<?php
2
3
namespace kalanis\kw_mapper\Search\Connector;
4
5
6
use kalanis\kw_mapper\MapperException;
7
use kalanis\kw_mapper\Records\ARecord;
8
use kalanis\kw_mapper\Records\TFill;
9
use kalanis\kw_mapper\Storage;
10
11
12
/**
13
 * Class Ldap
14
 * @package kalanis\kw_mapper\Search
15
 * Connect LDAP as datasource
16
 * Lightweight Directory Access Protocol
17
 * LDAP is in reality a tree. So it's similar to normal volume and its content. The key is path and value is content.
18
 * @codeCoverageIgnore for now - external source
19
 */
20
class Ldap extends AConnector
21
{
22
    use TFill;
23
24
    protected Storage\Database\Raw\Ldap $database;
25
    protected Storage\Database\Dialects\LdapQueries $dialect;
26
27
    /**
28
     * @param ARecord $record
29
     * @param Storage\Shared\QueryBuilder|null $builder
30
     * @throws MapperException
31
     */
32
    public function __construct(ARecord $record, ?Storage\Shared\QueryBuilder $builder = null)
33
    {
34
        $this->basicRecord = $record;
35
        $this->initRecordLookup($record);
36
        $config = Storage\Database\ConfigStorage::getInstance()->getConfig($record->getMapper()->getSource());
37
        $this->database = Storage\Database\DatabaseSingleton::getInstance()->getDatabase($config);
38
        $this->dialect = new Storage\Database\Dialects\LdapQueries();
39
        $this->queryBuilder = $builder ?: new Storage\Shared\QueryBuilder();
40
        $this->queryBuilder->setBaseTable($record->getMapper()->getAlias());
41
    }
42
43
    public function getCount(): int
44
    {
45
        $entries = $this->multiple();
46
        if (empty($entries) || empty($entries['count'])) {
47
            return 0;
48
        }
49
        return intval($entries['count']);
50
    }
51
52
    public function getResults(): array
53
    {
54
        $lines = $this->multiple();
55
        if (empty($lines)) {
56
            return [];
57
        }
58
59
        $result = [];
60
        $relationMap = array_flip($this->basicRecord->getMapper()->getRelations());
61
        foreach ($lines as $key => $line) {
62
            if (is_numeric($key) && is_iterable($line)) {
63
                $rec = clone $this->basicRecord;
64
                foreach ($line as $index => $item) {
65
                    $entry = $rec->getEntry(strval($relationMap[$index]));
66
                    $entry->setData($this->typedFillSelection($entry, $this->readItem($item)), true);
67
                }
68
                $result[] = $rec;
69
            }
70
        }
71
        return $result;
72
    }
73
74
    /**
75
     * @param mixed $item
76
     * @return string
77
     */
78
    protected function readItem($item)
79
    {
80
        return (empty($item) || empty($item[0]) || ('NULL' == $item[0])) ? '' : $item[0];
81
    }
82
83
    /**
84
     * @throws MapperException
85
     * @return array<string|int, string|int|array<string|int, string|int|float|null>>
86
     */
87
    protected function multiple(): array
88
    {
89
        $connect = $this->database->getConnection();
90
        if (!(is_resource($connect) || is_object($connect))) {
91
            return [];
92
        }
93
        $result = ldap_search(
94
            $connect,
0 ignored issues
show
Bug introduced by
It seems like $connect can also be of type object; however, parameter $ldap of ldap_search() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

94
            /** @scrutinizer ignore-type */ $connect,
Loading history...
95
            $this->dialect->domainDn($this->database->getDomain()),
96
            $this->dialect->filter($this->queryBuilder)
97
        );
98
        if (false === $result) {
99
            return [];
100
        }
101
        $items = ldap_get_entries($connect, $result);
0 ignored issues
show
Bug introduced by
It seems like $connect can also be of type object; however, parameter $ldap of ldap_get_entries() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

101
        $items = ldap_get_entries(/** @scrutinizer ignore-type */ $connect, $result);
Loading history...
102
        return (false !== $items) ? $items : [];
103
    }
104
}
105