Test Setup Failed
Branch master (10fdc5)
by Beniamin
35:44
created

UnderQuery::createInsertSelect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
c 1
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of Phuria SQL Builder 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\DependencyInjection\ContainerFactory;
17
use Phuria\UnderQuery\QueryBuilder\DeleteBuilder;
18
use Phuria\UnderQuery\QueryBuilder\InsertBuilder;
19
use Phuria\UnderQuery\QueryBuilder\InsertSelectBuilder;
20
use Phuria\UnderQuery\QueryBuilder\SelectBuilder;
21
use Phuria\UnderQuery\QueryBuilder\UpdateBuilder;
22
use Phuria\UnderQuery\QueryCompiler\QueryCompilerInterface;
23
24
/**
25
 * @author Beniamin Jonatan Šimko <[email protected]>
26
 */
27
class UnderQuery
28
{
29
    /**
30
     * @var ContainerInterface $container
31
     */
32
    private $container;
33
34
    /**
35
     * @param ContainerInterface|null $container
36
     */
37
    public function __construct(ContainerInterface $container = null)
38
    {
39
        $this->container = $container ?: (new ContainerFactory())->create();
40
    }
41
42
    /**
43
     * @return ContainerInterface
44
     */
45
    public function getContainer()
46
    {
47
        return $this->container;
48
    }
49
50
    /**
51
     * @param ConnectionInterface $connection
52
     * @param string              $name
53
     */
54
    public function registerConnection(ConnectionInterface $connection, $name = 'default')
55
    {
56
        $this->container->get('phuria.sql_builder.connection_manager')->registerConnection(
57
            $connection, $name
58
        );
59
    }
60
61
    /**
62
     * @param string $class
63
     *
64
     * @return QueryCompilerInterface
65
     */
66
    private function createQueryBuilder($class)
67
    {
68
        $options = [
69
            'parameter_collection_class' => $this->container->get('phuria.sql_builder.parameter_collection.class'),
70
            'reference_collection_class' => $this->container->get('phuria.sql_builder.reference_collection.class')
71
        ];
72
73
        return new $class(
74
            $this->container->get('phuria.sql_builder.table_factory'),
75
            $this->container->get('phuria.sql_builder.query_compiler'),
76
            $this->container->get('phuria.sql_builder.query_factory'),
77
            $options
78
        );
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
    public function createSelect()
85
    {
86
        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
    public function createUpdate()
93
    {
94
        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
    public function createDelete()
101
    {
102
        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
    public function createInsert()
109
    {
110
        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
    public function createInsertSelect()
117
    {
118
        return $this->createQueryBuilder(InsertSelectBuilder::class);
119
    }
120
}