DatabaseContainer::getSchema()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 15
ccs 6
cts 8
cp 0.75
crap 3.1406
rs 10
1
<?php
2
3
namespace mindplay\sql\model;
4
5
use mindplay\sql\model\schema\Schema;
6
use mindplay\sql\model\schema\Type;
7
use mindplay\sql\model\schema\Table;
8
use mindplay\unbox\Container;
9
use UnexpectedValueException;
10
11
/**
12
 * This class implements a dedicated dependency injection container for the database domain.
13
 */
14
class DatabaseContainer extends Container implements TypeProvider, TableFactory
15
{
16
    /**
17
     * @inheritdoc
18
     */
19 1
    public function getType(string $type_name): Type
20
    {
21 1
        if (! $this->has($type_name)) {
22 1
            $this->inject($type_name, $this->create($type_name)); // auto-wiring
23
        }
24
25 1
        $type = $this->get($type_name);
26
27 1
        if (! $type instanceof Type) {
28
            $class_name = get_class($type);
29
30
            throw new UnexpectedValueException("{$class_name} does not implement the Type interface");
31
        }
32
33 1
        return $type;
34
    }
35
36
    /**
37
     * @param $schema_type Schema class-name
38
     *
39
     * @return Schema
40
     */
41 1
    public function getSchema(string $schema_type): Schema
42
    {
43 1
        if (! $this->has($schema_type)) {
44 1
            $this->inject($schema_type, $this->create($schema_type)); // auto-wiring
45
        }
46
47 1
        $schema_type = $this->get($schema_type);
48
49 1
        if (! $schema_type instanceof Schema) {
50
            $class_name = get_class($schema_type);
51
52
            throw new UnexpectedValueException("{$class_name} does not extend the Schema class");
53
        }
54
55 1
        return $schema_type;
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61 1
    public function hasType(string $type_name): bool
62
    {
63 1
        return $this->has($type_name);
64
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69 1
    public function createTable(Schema $schema, string $class_name, string $table_name, string|null $alias): Table
70
    {
71 1
        return $this->create($class_name, [
72 1
            Schema::class => $schema,
73 1
            'name'        => $table_name,
74 1
            'alias'       => $alias,
75 1
        ]);
76
    }
77
}
78