PedigreeMapper   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 98.04%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 50
c 1
b 0
f 0
dl 0
loc 77
ccs 50
cts 51
cp 0.9804
rs 10
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setMap() 0 18 1
A getLike() 0 31 4
A beforeSave() 0 17 5
1
<?php
2
3
namespace kalanis\kw_pedigree\Storage\SingleTable;
4
5
6
use kalanis\kw_mapper\MapperException;
7
use kalanis\kw_mapper\Mappers;
8
use kalanis\kw_mapper\Records\ARecord;
9
use kalanis\kw_pedigree\Interfaces\ILike;
10
use kalanis\kw_pedigree\PedigreeException;
11
use kalanis\kw_pedigree\Storage\APedigreeRecord;
12
13
14
/**
15
 * Class PedigreeMapper
16
 * @package kalanis\kw_pedigree\Storage\SingleTable
17
 * Need to be used over MySQL / MariaDB
18
 */
19
class PedigreeMapper extends Mappers\Database\ADatabase implements ILike
20
{
21 2
    protected function setMap(): void
22
    {
23 2
        $this->setSource('pedigree');
24 2
        $this->setTable('kw_pedigree');
25 2
        $this->setRelation('id', 'pedigree_id');
26 2
        $this->setRelation('short', 'pedigree_short');
27 2
        $this->setRelation('name', 'pedigree_name');
28 2
        $this->setRelation('family', 'pedigree_family');
29 2
        $this->setRelation('birth', 'pedigree_birth');
30 2
        $this->setRelation('death', 'pedigree_death');
31 2
        $this->setRelation('fatherId', 'pedigree_father_id');
32 2
        $this->setRelation('motherId', 'pedigree_mother_id');
33 2
        $this->setRelation('successes', 'pedigree_successes');
34 2
        $this->setRelation('sex', 'pedigree_sex');
35 2
        $this->setRelation('text', 'pedigree_text');
36 2
        $this->addForeignKey('father', PedigreeRecord::class, 'fatherId', 'id');
37 2
        $this->addForeignKey('mother', PedigreeRecord::class, 'motherId', 'id');
38 2
        $this->addPrimaryKey('id');
39 2
    }
40
41
    /**
42
     * @param ARecord $record
43
     * @throws MapperException
44
     * @return bool
45
     */
46 2
    protected function beforeSave(ARecord $record): bool
47
    {
48 2
        $text = strval($record->__get('text'));
49 2
        if (empty($text)) {
50 2
            $record->__set('text', '');
51
        }
52
53 2
        $short = strval($record->__get('short'));
54 2
        $id = intval($record->__get('id'));
55 2
        if (!empty($id) && empty($short)) {
56
            // probably update
57 1
            return true;
58
        }
59 2
        if (empty($short)) {
60 1
            return false;
61
        }
62 2
        return true;
63
    }
64
65 2
    public function getLike(APedigreeRecord $record, string $what, ?string $sex = null): array
66
    {
67 2
        $query = 'SELECT `%1$s` AS `id`, `%2$s` AS `name`, `%3$s` AS `family` FROM `%4$s` WHERE (`%2$s` LIKE :named1 OR `%3$s` LIKE :named1)';
68 2
        $params = [':named1' => sprintf('%%%s%%', $what)];
69 2
        if (!is_null($sex)) {
70 1
            $query .= ' AND `%5$s` = :sx1';
71 1
            $params[':sx1'] = $sex;
72
        }
73 2
        $query .= ' ORDER BY `%3$s` ASC, `%2$s` ASC LIMIT 0, 30;';
74
75 2
        $read = $this->getReadDatabase();
76 2
        if (method_exists($read, 'query')) {
77 2
            $result = $read->query(sprintf($query,
78 2
                $this->relations['id'],
79 2
                $this->relations['name'],
80 2
                $this->relations['family'],
81 2
                $this->getTable(),
82 2
                $this->relations['sex']
83
            ), $params);
84
85 2
            $items = [];
86 2
            foreach ($result as $line) {
87 2
                $item = clone $record;
88 2
                $item->loadWithData($line);
89 2
                $items[] = $item;
90
            }
91 2
            return $items;
92
        } else {
93
            // @codeCoverageIgnoreStart
94
            // when you set the DB which does not have a query method (LDAP)
95
            throw new PedigreeException('This mapper does not have query!');
96
        }
97
        // @codeCoverageIgnoreEnd
98
    }
99
}
100