DatabaseMetadata::getDatabaseName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php declare(strict_types=1);
2
3
/**
4
 * @copyright   (c) 2017-present brian ridley
5
 * @author      brian ridley <[email protected]>
6
 * @license     http://opensource.org/licenses/MIT MIT
7
 */
8
9
namespace ptlis\GrepDb\Metadata\MySQL;
10
11
/**
12
 * DTO storing database metadata.
13
 */
14
final class DatabaseMetadata
15
{
16
    /** @var string */
17
    private $databaseName;
18
19
    /** @var TableMetadata[] */
20
    private $tableMetadataList = [];
21
22
23
    /**
24
     * @param string $databaseName
25
     * @param TableMetadata[] $tableMetadataList
26
     */
27 6
    public function __construct(
28
        string $databaseName,
29
        array $tableMetadataList
30
    ) {
31 6
        $this->databaseName = $databaseName;
32
33 6
        foreach ($tableMetadataList as $tableMetadata) {
34 6
            $this->tableMetadataList[$tableMetadata->getTableName()] = $tableMetadata;
35
        }
36 6
    }
37
38 4
    public function getDatabaseName(): string
39
    {
40 4
        return $this->databaseName;
41
    }
42
43
    /**
44
     * Get the metadata for a single table.
45
     */
46 2
    public function getTableMetadata(string $tableName): TableMetadata
47
    {
48 2
        if (!array_key_exists($tableName, $this->tableMetadataList)) {
49 1
            throw new \RuntimeException('Database "' . $this->databaseName . '" doesn\'t contain table "' . $tableName . '"');
50
        }
51
52 1
        return $this->tableMetadataList[$tableName];
53
    }
54
55
    /**
56
     * Get the metadata for all tables.
57
     *
58
     * @return TableMetadata[]
59
     */
60 1
    public function getAllTableMetadata(): array
61
    {
62 1
        return $this->tableMetadataList;
63
    }
64
}