Completed
Push — master ( d8dd7c...fad061 )
by Rasmus
03:55 queued 01:13
created

Database::getSchema()   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 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
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\query\SQLQuery;
6
use mindplay\sql\model\schema\Schema;
7
use UnexpectedValueException;
8
9
/**
10
 * This class implements the primary public API of the database model.
11
 */
12
abstract class Database
13
{
14
    /**
15
     * @var DatabaseContainer
16
     */
17
    protected $container;
18
19
    /**
20
     * The typical use-case is to omit the `$factory` argument - it exists primarily for
21
     * mocking and dependency-injection under test.
22
     *
23
     * @param DatabaseContainerFactory|null $factory custom factory instance (typically omitted)
24
     */
25 1
    public function __construct(DatabaseContainerFactory $factory = null)
26
    {
27 1
        if ($factory === null) {
28 1
            $factory = new DatabaseContainerFactory();
29
        }
30
31 1
        $this->bootstrap($factory);
32
33 1
        $this->container = $factory->createContainer();
34 1
    }
35
36
    /**
37
     * @return DatabaseContainer
38
     */
39
    abstract protected function bootstrap(DatabaseContainerFactory $factory);
40
41
    /**
42
     * @param string Schema class-name
43
     *
44
     * @return Schema
45
     */
46 1
    public function getSchema($schema)
47
    {
48 1
        return $this->container->getSchema($schema);
49
    }
50
    
51
    /**
52
     * @param string $sql
53
     * 
54
     * @return SQLQuery
55
     */
56 1
    public function sql($sql)
57
    {
58 1
        return $this->container->create(SQLQuery::class, ['sql' => $sql]);
59
    }
60
}
61