Connector::fetchData()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 2
b 0
f 0
nc 3
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
namespace kalanis\kw_connect\nette;
4
5
6
use kalanis\kw_connect\core\AConnector;
7
use kalanis\kw_connect\core\Interfaces\IFilterFactory;
8
use kalanis\kw_connect\core\Interfaces\IFilterSubs;
9
use kalanis\kw_connect\core\Interfaces\IIterableConnector;
10
use kalanis\kw_connect\core\Interfaces\IOrder;
11
use kalanis\kw_connect\core\Interfaces\IRow;
12
use Nette\Database\IRow as NetteRow;
13
use Nette\Database\Table\Selection;
14
15
16
/**
17
 * Class Connector
18
 * @package kalanis\kw_connect\nette
19
 * Data source is Nette\Database
20
 */
21
class Connector extends AConnector implements IIterableConnector
22
{
23
    protected Selection $netteTable;
24
    protected string $primaryKey = '';
25
    /** @var array<int, array<string>> */
26
    protected array $ordering;
27
    protected ?int $limit = null;
28
    protected ?int $offset = null;
29
    protected bool $dataFetched = false;
30
31
    public function __construct(Selection $netteTable, string $primaryKey)
32
    {
33
        $this->netteTable = $netteTable;
34
        $this->primaryKey = $primaryKey;
35
    }
36
37
    public function setFiltering(string $colName, string $filterType, $value): void
38
    {
39
        $type = $this->getFilterFactory()->getFilter($filterType);
40
        if ($type instanceof IFilterSubs) {
41
            $type->addFilterFactory($this->getFilterFactory());
42
        }
43
        $type->setDataSource($this->netteTable);
44
        $type->setFiltering($colName, $value);
45
    }
46
47
    public function setOrdering(string $colName, string $direction): void
48
    {
49
        $this->ordering[] = [$colName, $direction];
50
    }
51
52
    public function setPagination(?int $offset, ?int $limit): void
53
    {
54
        $this->offset = $offset;
55
        $this->limit = $limit;
56
    }
57
58
    public function getTotalCount(): int
59
    {
60
        return $this->netteTable->count('*');
61
    }
62
63
    public function fetchData(): void
64
    {
65
        foreach ($this->ordering as list($colName, $direction)) {
66
            $dir = IOrder::ORDER_ASC == $direction ? 'ASC' : 'DESC' ;
67
            $this->netteTable->order(strval($colName), $dir);
68
        }
69
        $this->netteTable->limit($this->limit, $this->offset);
70
        $this->parseData();
71
    }
72
73
    protected function parseData(): void
74
    {
75
        foreach ($this->netteTable->fetchAll() as $mapper) {
76
            $this->translatedData[$this->getPrimaryKey($mapper)] = $this->getTranslated($mapper);
77
        }
78
    }
79
80
    /**
81
     * @param NetteRow<string|int, string|int|float|bool|null> $data
82
     * @return IRow
83
     */
84
    protected function getTranslated(NetteRow $data): IRow
85
    {
86
        return new Row($data);
87
    }
88
89
    /**
90
     * @param NetteRow<string|int, string|int|float|bool|null> $record
91
     * @return string
92
     */
93
    protected function getPrimaryKey(NetteRow $record): string
94
    {
95
        return strval($record->offsetGet($this->primaryKey));
96
    }
97
98
    public function getFilterFactory(): IFilterFactory
99
    {
100
        return Filters\Factory::getInstance();
101
    }
102
}
103