Completed
Push — master ( 6adfd5...8dd9a1 )
by Rasmus
02:23
created

Database::select()   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 2
Bugs 0 Features 0
Metric Value
c 2
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\framework;
4
5
use mindplay\sql\model\DeleteQuery;
6
use mindplay\sql\model\InsertQuery;
7
use mindplay\sql\model\Schema;
8
use mindplay\sql\model\SelectQuery;
9
use mindplay\sql\model\SQLQuery;
10
use mindplay\sql\model\Table;
11
use mindplay\sql\model\UpdateQuery;
12
use UnexpectedValueException;
13
14
/**
15
 * This class implements the primary public API of the database model.
16
 */
17
abstract class Database
18
{
19
    /**
20
     * @var DatabaseContainer
21
     */
22
    protected $container;
23
    
24 1
    public function __construct()
25
    {
26 1
        $this->container = new DatabaseContainer();
27
28 1
        $this->container->set(Driver::class, $this->createDriver());
29 1
    }
30
    
31
    /**
32
     * @return Driver
33
     */
34
    abstract protected function createDriver();
35
36
    /**
37
     * @param string Schema class-name
38
     *
39
     * @return Schema
40
     */
41 1 View Code Duplication
    public function getSchema($schema)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
42
    {
43 1
        if (! $this->container->has($schema)) {
44 1
            $this->container->register($schema); // auto-wiring (for Schema with no special constructor dependencies)
45
        }
46
47 1
        $schema = $this->container->get($schema);
48
49 1
        if (! $schema instanceof Schema) {
50
            $class_name = get_class($schema);
51
52
            throw new UnexpectedValueException("{$class_name} does not extend the Schema class");
53
        }
54
55 1
        return $schema;
56
    }
57
    
58
    /**
59
     * @param string $sql
60
     * 
61
     * @return SQLQuery
62
     */
63 1
    public function sql($sql)
64
    {
65 1
        return $this->container->create(SQLQuery::class, ['sql' => $sql]);
66
    }
67
}
68