PedigreeMapper::getLike()   A
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 15
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 24
ccs 16
cts 16
cp 1
crap 5
rs 9.4555
1
<?php
2
3
namespace kalanis\kw_pedigree\Storage\File;
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_mapper\Search\Search;
10
use kalanis\kw_mapper\Storage\Shared;
11
use kalanis\kw_pedigree\Interfaces\ILike;
12
use kalanis\kw_pedigree\Interfaces\ISex;
13
use kalanis\kw_pedigree\Storage\APedigreeRecord;
14
15
16
/**
17
 * Class PedigreeMapper
18
 * @package kalanis\kw_pedigree\Storage\File
19
 */
20
class PedigreeMapper extends Mappers\Storage\ATable implements ILike
21
{
22 2
    protected function setMap(): void
23
    {
24 2
        $this->setSource('pedigree.txt');
25 2
        $this->setStorage();
26 2
        $this->setFormat(Shared\FormatFiles\SeparatedElements::class);
27 2
        $this->setRelation('id', 0);
28 2
        $this->setRelation('short', 1);
29 2
        $this->setRelation('name', 2);
30 2
        $this->setRelation('family', 3);
31 2
        $this->setRelation('birth', 4);
32 2
        $this->setRelation('death', 5);
33 2
        $this->setRelation('fatherId', 6);
34 2
        $this->setRelation('motherId', 7);
35 2
        $this->setRelation('successes', 8);
36 2
        $this->setRelation('sex', 9);
37 2
        $this->setRelation('text', 10);
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
        $short = strval($record->__get('short'));
49 2
        $id = intval($record->__get('id'));
50 2
        if (!empty($id) && empty($short)) {
51
            // probably update
52 1
            return true;
53
        }
54 2
        if (empty($short)) {
55 1
            return false;
56
        }
57
58 2
        $clear = get_class($record);
59 2
        $search = new Search(new $clear());
60 2
        $search->exact('short', $short);
61 2
        $results = $search->getResults();
62 2
        if (empty($results)) {
63 1
            return true;
64
        }
65 2
        $result = reset($results);
66
67 2
        return (intval($record->__get('id')) == intval($result->__get('id')));
68
    }
69
70
    /**
71
     * @param APedigreeRecord $record
72
     * @param string $what
73
     * @param string|null $sex
74
     * @throws MapperException
75
     * @return APedigreeRecord[]
76
     */
77 2
    public function getLike(APedigreeRecord $record, string $what, ?string $sex = null): array
78
    {
79
        // must be this way because files does not support OR
80 2
        $search1 = new Search($record);
81 2
        $search1->like('name', $what);
82 2
        $records1 = $search1->getResults();
83
84 2
        $search2 = new Search($record);
85 2
        $search2->like('family', $what);
86 2
        $records2 = $search2->getResults();
87
88 2
        $records = (array) array_combine(array_map([$this, 'getResultId'], $records1), $records1)
89 2
            + array_combine(array_map([$this, 'getResultId'], $records2), $records2);
90
91 2
        if ($sex && in_array($sex, [ISex::MALE, ISex::FEMALE])) {
92 1
            $limited = [];
93 1
            foreach ($records as $record) {
94 1
                if ($record->__get('sex') == $sex) {
95 1
                    $limited[] = $record;
96
                }
97
            }
98 1
            $records = $limited;
99
        }
100 2
        return $records;
101
    }
102
103 2
    public function getResultId(APedigreeRecord $record): int
104
    {
105 2
        return intval($record->id);
106
    }
107
}
108