Passed
Push — master ( f18857...c4e378 )
by Dmitriy
09:04 queued 05:09
created

DbSchemaProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Api\Inspector\Database\Db;
6
7
use Yiisoft\Db\Connection\ConnectionInterface;
8
use Yiisoft\Db\Query\Query;
9
use Yiisoft\Db\Schema\ColumnSchemaInterface;
10
use Yiisoft\Db\Schema\TableSchemaInterface;
11
use Yiisoft\Yii\Debug\Api\Inspector\Database\SchemaProviderInterface;
12
13
class DbSchemaProvider implements SchemaProviderInterface
14
{
15 2
    public function __construct(private ConnectionInterface $db)
16
    {
17 2
    }
18
19 1
    public function getTables(): array
20
    {
21 1
        $quoter = $this->db->getQuoter();
22
        /** @var TableSchemaInterface[] $tableSchemas */
23 1
        $tableSchemas = $this->db->getSchema()->getTableSchemas();
24 1
        $tables = [];
25
26 1
        foreach ($tableSchemas as $schema) {
27 1
            $tables[] = [
28 1
                'table' => $quoter->unquoteSimpleTableName($schema->getName()),
29 1
                'primaryKeys' => $schema->getPrimaryKey(),
30 1
                'columns' => $this->serializeARColumnsSchemas($schema->getColumns()),
31 1
                'records' => (new Query($this->db))->from($schema->getName())->count(),
32 1
            ];
33
        }
34 1
        return $tables;
35
    }
36
37 1
    public function getTable(string $tableName): array
38
    {
39
        /** @var TableSchemaInterface[] $tableSchemas */
40 1
        $schema = $this->db->getSchema()->getTableSchema($tableName);
41 1
        $records = (new Query($this->db))->from($schema->getName())->all();
42 1
        $data = [];
43
44
        // TODO: add pagination
45 1
        foreach ($records as $r => $record) {
46 1
            foreach ($record as $n => $attribute) {
47 1
                $data[$r][$n] = $attribute;
48
            }
49
        }
50
51 1
        return [
52 1
            'table' => $schema->getName(),
53 1
            'primaryKeys' => $schema->getPrimaryKey(),
54 1
            'columns' => $this->serializeARColumnsSchemas($schema->getColumns()),
55 1
            'records' => $data,
56 1
        ];
57
    }
58
59
    /**
60
     * @param ColumnSchemaInterface[] $columns
61
     */
62 2
    private function serializeARColumnsSchemas(array $columns): array
63
    {
64 2
        $result = [];
65 2
        foreach ($columns as $columnSchema) {
66 2
            $result[] = [
67 2
                'name' => $columnSchema->getName(),
68 2
                'size' => $columnSchema->getSize(),
69 2
                'type' => $columnSchema->getType(),
70 2
                'dbType' => $columnSchema->getDbType(),
71 2
                'defaultValue' => $columnSchema->getDefaultValue(),
72 2
                'comment' => $columnSchema->getComment(),
73 2
                'allowNull' => $columnSchema->isAllowNull(),
74 2
            ];
75
        }
76 2
        return $result;
77
    }
78
}
79