CycleSchemaProvider::getTable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 13
ccs 0
cts 9
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Api\Inspector\Database\Cycle;
6
7
use Cycle\Database\ColumnInterface;
8
use Cycle\Database\DatabaseProviderInterface;
9
use Yiisoft\Yii\Debug\Api\Inspector\Database\SchemaProviderInterface;
10
11
class CycleSchemaProvider implements SchemaProviderInterface
12
{
13
    public function __construct(private DatabaseProviderInterface $databaseProvider)
14
    {
15
    }
16
17
    public function getTables(): array
18
    {
19
        $database = $this->databaseProvider->database();
20
        $tableSchemas = $database->getTables();
21
22
        $tables = [];
23
        foreach ($tableSchemas as $schema) {
24
            $records = $database->select()->from($schema->getName())->count();
25
            $tables[] = [
26
                'table' => $schema->getName(),
27
                'primaryKeys' => $schema->getPrimaryKeys(),
28
                'columns' => $this->serializeCycleColumnsSchemas($schema->getColumns()),
29
                'records' => $records,
30
            ];
31
        }
32
33
        return $tables;
34
    }
35
36
    public function getTable(string $tableName): array
37
    {
38
        $database = $this->databaseProvider->database();
39
        $schema = $database->table($tableName);
40
41
        // TODO: add pagination
42
        $records = $database->select()->from($tableName)->fetchAll();
43
44
        return [
45
            'table' => $schema->getName(),
46
            'primaryKeys' => $schema->getPrimaryKeys(),
47
            'columns' => $this->serializeCycleColumnsSchemas($schema->getColumns()),
48
            'records' => $records,
49
        ];
50
    }
51
52
    /**
53
     * @param ColumnInterface[] $columns
54
     */
55
    private function serializeCycleColumnsSchemas(array $columns): array
56
    {
57
        $result = [];
58
        foreach ($columns as $columnSchema) {
59
            $result[] = [
60
                'name' => $columnSchema->getName(),
61
                'size' => $columnSchema->getSize(),
62
                'type' => $columnSchema->getInternalType(),
63
                'dbType' => $columnSchema->getType(),
64
                'defaultValue' => $columnSchema->getDefaultValue(),
65
                'comment' => null, // unsupported for now
66
                'allowNull' => $columnSchema->isNullable(),
67
            ];
68
        }
69
        return $result;
70
    }
71
}
72