Passed
Push — master ( c7f95e...faf2cd )
by Dmitriy
03:07
created

ActiveRecordSchemaProvider::getTable()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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