Completed
Push — master ( 7aa5ea...c891e1 )
by Beniamin
08:55
created

UnderQuery::getConnectionManager()   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 0
crap 1
1
<?php
2
3
/**
4
 * This file is part of UnderQuery package.
5
 *
6
 * Copyright (c) 2016 Beniamin Jonatan Šimko
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Phuria\UnderQuery;
13
14
use Interop\Container\ContainerInterface;
15
use Phuria\UnderQuery\Connection\ConnectionInterface;
16
use Phuria\UnderQuery\Connection\ConnectionManagerInterface;
17
use Phuria\UnderQuery\DependencyInjection\ContainerFactory;
18
use Phuria\UnderQuery\QueryBuilder\DeleteBuilder;
19
use Phuria\UnderQuery\QueryBuilder\InsertBuilder;
20
use Phuria\UnderQuery\QueryBuilder\InsertSelectBuilder;
21
use Phuria\UnderQuery\QueryBuilder\SelectBuilder;
22
use Phuria\UnderQuery\QueryBuilder\UpdateBuilder;
23
use Phuria\UnderQuery\QueryCompiler\QueryCompilerInterface;
24
25
/**
26
 * @author Beniamin Jonatan Šimko <[email protected]>
27
 */
28
class UnderQuery
29
{
30
    /**
31
     * @var ContainerInterface $container
32
     */
33
    private $container;
34
35
    /**
36
     * @param ContainerInterface|null $container
37
     */
38 3
    public function __construct(ContainerInterface $container = null)
39
    {
40 3
        $this->container = $container ?: (new ContainerFactory())->create();
41 3
    }
42
43
    /**
44
     * @return ContainerInterface
45
     */
46 2
    public function getContainer()
47
    {
48 2
        return $this->container;
49
    }
50
51
    /**
52
     * @param ConnectionInterface $connection
53
     * @param string              $name
54
     */
55 1
    public function registerConnection(ConnectionInterface $connection, $name = 'default')
56
    {
57 1
        $this->getConnectionManager()->registerConnection($connection, $name);
58 1
    }
59
60
    /**
61
     * @return ConnectionManagerInterface
62
     */
63 1
    public function getConnectionManager()
64
    {
65 1
        return $this->container->get('phuria.sql_builder.connection_manager');
66
    }
67
68
    /**
69
     * @param string $class
70
     *
71
     * @return QueryCompilerInterface
72
     */
73 1
    private function createQueryBuilder($class)
74
    {
75 1
        return new $class(
76 1
            $this->container->get('phuria.sql_builder.table_factory'),
77 1
            $this->container->get('phuria.sql_builder.query_compiler')
78 1
        );
79
    }
80
81
    /**
82
     * @return SelectBuilder
0 ignored issues
show
Documentation introduced by
Should the return type not be QueryCompilerInterface?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
83
     */
84 1
    public function createSelect()
85
    {
86 1
        return $this->createQueryBuilder(SelectBuilder::class);
87
    }
88
89
    /**
90
     * @return UpdateBuilder
0 ignored issues
show
Documentation introduced by
Should the return type not be QueryCompilerInterface?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
91
     */
92 1
    public function createUpdate()
93
    {
94 1
        return $this->createQueryBuilder(UpdateBuilder::class);
95
    }
96
97
    /**
98
     * @return DeleteBuilder
0 ignored issues
show
Documentation introduced by
Should the return type not be QueryCompilerInterface?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
99
     */
100 1
    public function createDelete()
101
    {
102 1
        return $this->createQueryBuilder(DeleteBuilder::class);
103
    }
104
105
    /**
106
     * @return InsertBuilder
0 ignored issues
show
Documentation introduced by
Should the return type not be QueryCompilerInterface?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
107
     */
108 1
    public function createInsert()
109
    {
110 1
        return $this->createQueryBuilder(InsertBuilder::class);
111
    }
112
113
    /**
114
     * @return InsertSelectBuilder
0 ignored issues
show
Documentation introduced by
Should the return type not be QueryCompilerInterface?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
115
     */
116 1
    public function createInsertSelect()
117
    {
118 1
        return $this->createQueryBuilder(InsertSelectBuilder::class);
119
    }
120
}