Completed
Push — master ( f5e5fb...7d0a28 )
by Rasmus
02:24
created

DatabaseContainer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 88.24%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 1
cbo 1
dl 0
loc 48
ccs 15
cts 17
cp 0.8824
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getType() 0 16 3
A hasType() 0 4 1
A createTable() 0 4 1
1
<?php
2
3
namespace mindplay\sql\model;
4
5
use mindplay\sql\model\schema\Type;
6
use mindplay\unbox\Container;
7
use UnexpectedValueException;
8
9
/**
10
 * This class implements a dedicated dependency injection container for the database domain.
11
 */
12
class DatabaseContainer extends Container implements TypeProvider, TableFactory
13
{
14 1
    public function __construct()
15
    {
16 1
        parent::__construct();
17
        
18
        // self-register:
19
20 1
        $this->set(TypeProvider::class, $this);
21 1
        $this->set(TableFactory::class, $this);
22 1
    }
23
24
    /**
25
     * @inheritdoc
26
     */
27 1
    public function getType($type_name)
28
    {
29 1
        if (! $this->has($type_name)) {
30 1
            $this->register($type_name); // auto-wiring (for Types with no special constructor dependencies)
31
        }
32
33 1
        $type = $this->get($type_name);
34
35 1
        if (! $type instanceof Type) {
36
            $class_name = get_class($type);
37
38
            throw new UnexpectedValueException("{$class_name} does not implement the Type interface");
39
        }
40
        
41 1
        return $type;
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47 1
    public function hasType($type_name)
48
    {
49 1
        return $this->has($type_name);
50
    }
51
    
52
    /**
53
     * @inheritdoc
54
     */
55 1
    public function createTable($class_name, $table_name, $alias)
56
    {
57 1
        return $this->create($class_name, ['name' => $table_name, 'alias' => $alias]);
58
    }
59
}
60