Connector   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 34
c 2
b 0
f 1
dl 0
loc 82
rs 10
wmc 16

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getTranslated() 0 3 1
A getFilterFactory() 0 3 1
A fetchData() 0 14 5
A getPrimaryKey() 0 3 1
A setPagination() 0 4 1
A __construct() 0 4 1
A setOrdering() 0 3 1
A parseData() 0 4 2
A getTotalCount() 0 5 1
A setFiltering() 0 8 2
1
<?php
2
3
namespace kalanis\kw_connect\yii3;
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 kalanis\kw_connect\core\Rows\SimpleArrayRow;
13
use Yiisoft\Db\Query\Query;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Query\Query was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
16
/**
17
 * Class Connector
18
 * @package kalanis\kw_connect\yii3
19
 * Data source is Dibi\Fluent
20
 */
21
class Connector extends AConnector implements IIterableConnector
22
{
23
    protected Query $yiiFluent;
24
    protected string $primaryKey;
25
    /** @var array<int, array<string>> */
26
    protected array $sorters = [];
27
    protected ?int $limit = null;
28
    protected ?int $offset = null;
29
    /** @var array[][] */
30
    protected array $rawData = [];
31
    protected bool $dataFetched = false;
32
33
    public function __construct(Query $dataSource, string $primaryKey)
34
    {
35
        $this->yiiFluent = $dataSource;
36
        $this->primaryKey = $primaryKey;
37
    }
38
39
    public function setFiltering(string $colName, string $filterType, $value): void
40
    {
41
        $type = $this->getFilterFactory()->getFilter($filterType);
42
        if ($type instanceof IFilterSubs) {
43
            $type->addFilterFactory($this->getFilterFactory());
44
        }
45
        $type->setDataSource($this->yiiFluent);
46
        $type->setFiltering($colName, $value);
47
    }
48
49
    public function setOrdering(string $colName, string $direction): void
50
    {
51
        $this->sorters[] = [$colName, $direction];
52
    }
53
54
    public function setPagination(?int $offset, ?int $limit): void
55
    {
56
        $this->offset = $offset;
57
        $this->limit = $limit;
58
    }
59
60
    public function getTotalCount(): int
61
    {
62
        // count needs only filtered content
63
        $dataSource = clone $this->yiiFluent;
64
        return intval($dataSource->count($this->primaryKey));
65
    }
66
67
    public function fetchData(): void
68
    {
69
        foreach (array_reverse($this->sorters) as list($colName, $direction)) {
70
            $dir = IOrder::ORDER_ASC == $direction ? SORT_ASC : SORT_DESC ;
71
            $this->yiiFluent->addOrderBy([$colName, $dir]);
72
        }
73
        if (!is_null($this->offset)) {
74
            $this->yiiFluent->offset($this->offset);
75
        }
76
        if (!is_null($this->limit)) {
77
            $this->yiiFluent->limit($this->limit);
78
        }
79
        $this->rawData = $this->yiiFluent->select('*')->all();
80
        $this->parseData();
81
    }
82
83
    protected function parseData(): void
84
    {
85
        foreach ($this->rawData as $mapper) {
86
            $this->translatedData[$this->getPrimaryKey($mapper)] = $this->getTranslated($mapper);
87
        }
88
    }
89
90
    protected function getTranslated(array $data): IRow
91
    {
92
        return new SimpleArrayRow($data);
93
    }
94
95
    protected function getPrimaryKey(array $record): string
96
    {
97
        return strval($record[$this->primaryKey]);
98
    }
99
100
    public function getFilterFactory(): IFilterFactory
101
    {
102
        return Filters\Factory::getInstance();
103
    }
104
}
105