UsersMapper   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
dl 0
loc 52
ccs 0
cts 26
cp 0
rs 10
c 1
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A afterLoad() 0 5 1
A setMap() 0 14 1
A beforeInsert() 0 5 1
A beforeUpdate() 0 5 1
1
<?php
2
3
namespace kalanis\kw_auth_sources\Sources\Mapper\Database;
4
5
6
use kalanis\kw_mapper\MapperException;
7
use kalanis\kw_mapper\Mappers\Database\ADatabase;
8
use kalanis\kw_mapper\Records\ARecord;
9
10
11
/**
12
 * Class UsersMapper
13
 * @package kalanis\kw_auth_sources\Data\Mapper\Database
14
 * @codeCoverageIgnore remote source
15
 */
16
class UsersMapper extends ADatabase
17
{
18
    protected function setMap(): void
19
    {
20
        $this->setSource('default_database');
21
        $this->setTable('users');
22
        $this->setRelation('id', 'u_id');
23
        $this->setRelation('login', 'u_login');
24
        $this->setRelation('pass', 'u_pass');
25
        $this->setRelation('groupId', 'gr_id');
26
        $this->setRelation('display', 'u_display');
27
        $this->setRelation('cert', 'u_cert');
28
        $this->setRelation('salt', 'u_salt');
29
        $this->setRelation('extra', 'u_extra');
30
        $this->addPrimaryKey('id');
31
        $this->addForeignKey('groups', GroupsRecord::class, 'groupId', 'id');
32
    }
33
34
    /**
35
     * @param ARecord $record
36
     * @throws MapperException
37
     * @return bool
38
     */
39
    protected function afterLoad(ARecord $record): bool
40
    {
41
        $entry = $record->getEntry('extra');
42
        $entry->setData(json_decode(strval($entry->getData()), true), true);
43
        return parent::afterLoad($record);
44
    }
45
46
    /**
47
     * @param ARecord $record
48
     * @throws MapperException
49
     * @return bool
50
     */
51
    protected function beforeInsert(ARecord $record): bool
52
    {
53
        $entry = $record->getEntry('extra');
54
        $entry->setData(json_encode($entry->getData()));
55
        return parent::beforeInsert($record);
56
    }
57
58
    /**
59
     * @param ARecord $record
60
     * @throws MapperException
61
     * @return bool
62
     */
63
    protected function beforeUpdate(ARecord $record): bool
64
    {
65
        $entry = $record->getEntry('extra');
66
        $entry->setData(json_encode($entry->getData()));
67
        return parent::beforeUpdate($record);
68
    }
69
}
70