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\unbox\Container; |
8
|
|
|
use UnexpectedValueException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This class implements a dedicated dependency injection container for the database domain. |
12
|
|
|
*/ |
13
|
|
|
class DatabaseContainer extends Container implements TypeProvider, TableFactory |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @inheritdoc |
17
|
|
|
*/ |
18
|
1 |
View Code Duplication |
public function getType($type_name) |
|
|
|
|
19
|
|
|
{ |
20
|
1 |
|
if (! $this->has($type_name)) { |
21
|
1 |
|
$this->inject($type_name, $this->create($type_name)); // auto-wiring |
22
|
|
|
} |
23
|
|
|
|
24
|
1 |
|
$type = $this->get($type_name); |
25
|
|
|
|
26
|
1 |
|
if (! $type instanceof Type) { |
27
|
|
|
$class_name = get_class($type); |
28
|
|
|
|
29
|
|
|
throw new UnexpectedValueException("{$class_name} does not implement the Type interface"); |
30
|
|
|
} |
31
|
|
|
|
32
|
1 |
|
return $type; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string Schema class-name |
37
|
|
|
* |
38
|
|
|
* @return Schema |
39
|
|
|
*/ |
40
|
1 |
View Code Duplication |
public function getSchema($schema_type) |
|
|
|
|
41
|
|
|
{ |
42
|
1 |
|
if (! $this->has($schema_type)) { |
43
|
1 |
|
$this->inject($schema_type, $this->create($schema_type)); // auto-wiring |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
$schema_type = $this->get($schema_type); |
47
|
|
|
|
48
|
1 |
|
if (! $schema_type instanceof Schema) { |
49
|
|
|
$class_name = get_class($schema_type); |
50
|
|
|
|
51
|
|
|
throw new UnexpectedValueException("{$class_name} does not extend the Schema class"); |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
return $schema_type; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritdoc |
59
|
|
|
*/ |
60
|
1 |
|
public function hasType($type_name) |
61
|
|
|
{ |
62
|
1 |
|
return $this->has($type_name); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritdoc |
67
|
|
|
*/ |
68
|
1 |
|
public function createTable($class_name, $table_name, $alias) |
69
|
|
|
{ |
70
|
1 |
|
return $this->create($class_name, ['name' => $table_name, 'alias' => $alias]); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.