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

DatabaseContainer::hasType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 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