Connector::fetchData()   A
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 8
c 1
b 0
f 0
nc 12
nop 0
dl 0
loc 13
rs 9.6111
1
<?php
2
3
namespace kalanis\kw_connect\doctrine_dbal;
4
5
6
use Doctrine\DBAL\Connection;
7
use Doctrine\DBAL\Query\QueryBuilder;
8
use kalanis\kw_connect\core\AConnector;
9
use kalanis\kw_connect\core\Interfaces\IFilterFactory;
10
use kalanis\kw_connect\core\Interfaces\IFilterSubs;
11
use kalanis\kw_connect\core\Interfaces\IIterableConnector;
12
use kalanis\kw_connect\core\Interfaces\IOrder;
13
use kalanis\kw_connect\core\Interfaces\IRow;
14
use kalanis\kw_connect\core\Rows\SimpleArrayRow;
15
16
17
/**
18
 * Class Connector
19
 * @package kalanis\kw_connect\doctrine_dbal
20
 * Data source is Doctrine DBAL
21
 */
22
class Connector extends AConnector implements IIterableConnector
23
{
24
    protected Connection $connection;
25
    protected QueryBuilder $queryBuilder;
26
    protected string $primaryKey = '';
27
    /** @var array<int, array<string>> */
28
    protected array $ordering = [];
29
    protected ?int $limit = null;
30
    protected ?int $offset = null;
31
    protected bool $dataFetched = false;
32
33
    public function __construct(Connection $connection, string $primaryKey)
34
    {
35
        $this->connection = $connection;
36
        $this->queryBuilder = $connection->createQueryBuilder();
37
        $this->primaryKey = $primaryKey;
38
    }
39
40
    public function setFiltering(string $colName, string $filterType, $value): void
41
    {
42
        $type = $this->getFilterFactory()->getFilter($filterType);
43
        if ($type instanceof IFilterSubs) {
44
            $type->addFilterFactory($this->getFilterFactory());
45
        }
46
        $type->setDataSource($this->queryBuilder);
47
        $type->setFiltering($colName, $value);
48
    }
49
50
    public function setOrdering(string $colName, string $direction): void
51
    {
52
        $this->ordering[] = [$colName, $direction];
53
    }
54
55
    public function setPagination(?int $offset, ?int $limit): void
56
    {
57
        $this->offset = $offset;
58
        $this->limit = $limit;
59
    }
60
61
    public function getTotalCount(): int
62
    {
63
        $this->queryBuilder->select('count(' . $this->primaryKey. ')');
64
        return intval($this->queryBuilder->fetchOne());
65
    }
66
67
    public function fetchData(): void
68
    {
69
        foreach ($this->ordering as list($colName, $direction)) {
70
            $dir = IOrder::ORDER_ASC == $direction ? 'ASC' : 'DESC' ;
71
            $this->queryBuilder->orderBy(strval($colName), strval($dir));
72
        }
73
        if (!is_null($this->offset)) {
74
            $this->queryBuilder->setFirstResult($this->offset);
75
        }
76
        if (!is_null($this->limit)) {
77
            $this->queryBuilder->setMaxResults($this->limit);
78
        }
79
        $this->parseData();
80
    }
81
82
    protected function parseData(): void
83
    {
84
        foreach (
85
            $this->connection->iterateNumeric(
86
                $this->queryBuilder->getSQL(),
87
                $this->queryBuilder->getParameters(),
0 ignored issues
show
Bug introduced by
$this->queryBuilder->getParameters() of type Doctrine\DBAL\Query\list is incompatible with the type array expected by parameter $params of Doctrine\DBAL\Connection::iterateNumeric(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
                /** @scrutinizer ignore-type */ $this->queryBuilder->getParameters(),
Loading history...
88
                $this->queryBuilder->getParameterTypes()
89
            ) as $value
90
        ) {
91
            $this->translatedData[$this->getPrimaryKey($value)] = $this->getTranslated($value);
0 ignored issues
show
Bug introduced by
$value of type Doctrine\DBAL\list is incompatible with the type array<mixed,mixed> expected by parameter $data of kalanis\kw_connect\doctr...nector::getPrimaryKey(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

91
            $this->translatedData[$this->getPrimaryKey(/** @scrutinizer ignore-type */ $value)] = $this->getTranslated($value);
Loading history...
92
        }
93
    }
94
95
    /**
96
     * @param array<int|string, bool|float|int|string|null> $data
97
     * @return IRow
98
     */
99
    protected function getTranslated($data): IRow
100
    {
101
        return new SimpleArrayRow($data);
102
    }
103
104
    /**
105
     * @param array<mixed> $data
106
     * @return string
107
     */
108
    protected function getPrimaryKey($data): string
109
    {
110
        return strval($data[$this->primaryKey]);
111
    }
112
113
    public function getFilterFactory(): IFilterFactory
114
    {
115
        return Filters\Factory::getInstance();
116
    }
117
}
118