WinRegistry::insertRecord()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace kalanis\kw_mapper\Mappers\Database;
4
5
6
use kalanis\kw_mapper\Interfaces\IDriverSources;
7
use kalanis\kw_mapper\MapperException;
8
use kalanis\kw_mapper\Mappers\AMapper;
9
use kalanis\kw_mapper\Mappers\Shared\TContent;
10
use kalanis\kw_mapper\Records;
11
use kalanis\kw_mapper\Records\ARecord;
12
use kalanis\kw_mapper\Records\TFill;
13
use kalanis\kw_mapper\Storage;
14
15
16
/**
17
 * Class WinRegistry
18
 * @package kalanis\kw_mapper\Mappers\Database
19
 * We are not crazy enough - let's work with Windows Registry! In PHP.
20
 * - the path is tree and there is bunch of keys; it's composed primary key - part (HK*_*) and path
21
 * - the difference with registry and normal file is simple - there is also content type, not just path and content
22
 * -> it's similar to flags in *nix or app rights in OS9
23
 *
24
 * The registry is hybrid between usual database and filesystem - storing values in tree nodes like FS and with type
25
 * control like DB. That means there is specific query set.
26
 * This class uses external library added into php which allows most of operations over registry.
27
 *
28
 * @codeCoverageIgnore cannot check this on *nix
29
 */
30
class WinRegistry extends AMapper
31
{
32
    use TContent;
33
    use TFill;
34
35
    protected string $typeKey = '';
36
    protected Storage\Database\Raw\WinRegistry $database;
37
38
    /**
39
     * @throws MapperException
40
     */
41
    public function __construct()
42
    {
43
        parent::__construct();
44
        $config = Storage\Database\ConfigStorage::getInstance()->getConfig($this->getSource());
45
        $this->database = Storage\Database\DatabaseSingleton::getInstance()->getDatabase($config); // @phpstan-ignore-line
46
    }
47
48
    public function getAlias(): string
49
    {
50
        return 'win_registry';
51
    }
52
53
    protected function setMap(): void
54
    {
55
        $this->addPrimaryKey('part');
56
        $this->addPrimaryKey('path');
57
        $this->setTypeKey('type');
58
        $this->setContentKey('content');
59
    }
60
61
    public function setTypeKey(string $typeKey): self
62
    {
63
        $this->typeKey = $typeKey;
64
        return $this;
65
    }
66
67
    public function getTypeKey(): string
68
    {
69
        return $this->typeKey;
70
    }
71
72
    /**
73
     * @param Records\ARecord|Records\RegistryRecord $record
74
     * @throws MapperException
75
     * @return bool
76
     */
77
    protected function insertRecord(Records\ARecord $record): bool
78
    {
79
        $pks = $this->getPrimaryKeys();
80
        return $this->database->exec(
81
            IDriverSources::ACTION_INSERT,
82
            $record->offsetGet(reset($pks)),
83
            $record->offsetGet(next($pks)),
84
            $record->offsetGet($this->getTypeKey()),
85
            $record->offsetGet($this->getContentKey())
86
        );
87
    }
88
89
    protected function updateRecord(ARecord $record): bool
90
    {
91
        $pks = $this->getPrimaryKeys();
92
        return $this->database->exec(
93
            IDriverSources::ACTION_UPDATE,
94
            $record->offsetGet(reset($pks)),
95
            $record->offsetGet(next($pks)),
96
            $record->offsetGet($this->getTypeKey()),
97
            $record->offsetGet($this->getContentKey())
98
        );
99
    }
100
101
    protected function deleteRecord(ARecord $record): bool
102
    {
103
        $pks = $this->getPrimaryKeys();
104
        return $this->database->exec(
105
            IDriverSources::ACTION_DELETE,
106
            $record->offsetGet(reset($pks)),
107
            $record->offsetGet(next($pks)),
108
            $record->offsetGet($this->getTypeKey())
109
        );
110
    }
111
112
    protected function loadRecord(ARecord $record): bool
113
    {
114
        $pks = $this->getPrimaryKeys();
115
        $values = $this->database->values(
116
            $record->offsetGet(reset($pks)),
117
            $record->offsetGet(next($pks))
118
        );
119
120
        if (empty($values)) { // nothing found
121
            return false;
122
        }
123
124
        $value = reset($values);
125
126
        // fill entries in record
127
        $entry = $record->getEntry($this->getTypeKey());
128
        $entry->setData($this->typedFillSelection($entry, reset($value)), true);
129
        $entry = $record->getEntry($this->getContentKey());
130
        $entry->setData($this->typedFillSelection($entry, next($value)), true);
131
132
        return true;
133
    }
134
135
    public function countRecord(ARecord $record): int
136
    {
137
        $pks = $this->getPrimaryKeys();
138
        $values = $this->database->values(
139
            $record->offsetGet(reset($pks)),
140
            $record->offsetGet(next($pks))
141
        );
142
        return count($values);
143
    }
144
145
    public function loadMultiple(ARecord $record): array
146
    {
147
        $pks = $this->getPrimaryKeys();
148
        $values = $this->database->values(
149
            $record->offsetGet(reset($pks)),
150
            $record->offsetGet(next($pks))
151
        );
152
153
        if (empty($values)) { // nothing found
154
            return [];
155
        }
156
157
        $result = [];
158
159
        foreach ($values as $value) {
160
            $rec = clone $record;
161
162
            // fill entries in record
163
            $entry = $rec->getEntry($this->getTypeKey());
164
            $entry->setData($this->typedFillSelection($entry, reset($value)), true);
165
            $entry = $rec->getEntry($this->getContentKey());
166
            $entry->setData($this->typedFillSelection($entry, next($value)), true);
167
168
            $result[] = $rec;
169
        }
170
171
        return $result;
172
    }
173
}
174