Connector::getTranslated()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace kalanis\kw_connect\eloquent;
4
5
6
use ArrayAccess;
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Support\Collection;
9
use kalanis\kw_connect\core\AConnector;
10
use kalanis\kw_connect\core\Interfaces\IFilterFactory;
11
use kalanis\kw_connect\core\Interfaces\IFilterSubs;
12
use kalanis\kw_connect\core\Interfaces\IIterableConnector;
13
use kalanis\kw_connect\core\Interfaces\IOrder;
14
use kalanis\kw_connect\core\Interfaces\IRow;
15
use kalanis\kw_connect\core\Rows\ArrayAccessRow;
16
17
18
/**
19
 * Class Connector
20
 * @package kalanis\kw_connect\eloquent
21
 * Data source is Laravel\Eloquent
22
 */
23
class Connector extends AConnector implements IIterableConnector
24
{
25
    protected Builder $queryBuilder;
26
    protected string $primaryKey;
27
    /** @var array<int, array<string>> */
28
    protected array $sorters = [];
29
    protected ?int $limit = null;
30
    protected ?int $offset = null;
31
    /** @var Collection */
32
    protected $rawData = null;
33
    protected bool $dataFetched = false;
34
35
    public function __construct(Builder $queryBuilder, string $primaryKey)
36
    {
37
        $this->queryBuilder = $queryBuilder;
38
        $this->primaryKey = $primaryKey;
39
    }
40
41
    public function setFiltering(string $colName, string $filterType, $value): void
42
    {
43
        $type = $this->getFilterFactory()->getFilter($filterType);
44
        if ($type instanceof IFilterSubs) {
45
            $type->addFilterFactory($this->getFilterFactory());
46
        }
47
        $type->setDataSource($this->queryBuilder);
48
        $type->setFiltering($colName, $value);
49
    }
50
51
    public function setOrdering(string $colName, string $direction): void
52
    {
53
        $this->sorters[] = [$colName, $direction];
54
    }
55
56
    public function setPagination(?int $offset, ?int $limit): void
57
    {
58
        $this->offset = $offset;
59
        $this->limit = $limit;
60
    }
61
62
    public function getTotalCount(): int
63
    {
64
        // count needs only filtered content
65
        $dataSource = clone $this->queryBuilder;
66
        return $dataSource->count();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $dataSource->count() could return the type Illuminate\Database\Eloquent\Builder which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
67
    }
68
69
    public function fetchData(): void
70
    {
71
        foreach (array_reverse($this->sorters) as list($colName, $direction)) {
72
            $dir = IOrder::ORDER_ASC == $direction ? 'asc' : 'desc' ;
73
            $this->queryBuilder->orderBy($colName, $dir);
74
        }
75
        if (!is_null($this->offset)) {
76
            $this->queryBuilder->offset($this->offset);
77
        }
78
        if (!is_null($this->limit)) {
79
            $this->queryBuilder->limit($this->limit);
80
        }
81
        $this->rawData = $this->queryBuilder->get();
82
        $this->parseData();
83
    }
84
85
    protected function parseData(): void
86
    {
87
        foreach ($this->rawData->getIterator() as $iterate) {
88
            $this->translatedData[$this->getPrimaryKey($iterate)] = $this->getTranslated($iterate);
89
        }
90
    }
91
92
    protected function getTranslated(ArrayAccess $data): IRow
93
    {
94
        return new ArrayAccessRow($data);
95
    }
96
97
    protected function getPrimaryKey(ArrayAccess $record): string
98
    {
99
        return strval($record->offsetGet($this->primaryKey));
100
    }
101
102
    public function getFilterFactory(): IFilterFactory
103
    {
104
        return Filters\Factory::getInstance();
105
    }
106
}
107