Completed
Push — master ( ad3a38...ce24f7 )
by Beniamin
02:38
created

QueryBuilderFactory::createInsert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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\SQLBuilder;
13
14
use Phuria\SQLBuilder\Connection\ConnectionInterface;
15
use Phuria\SQLBuilder\DependencyInjection\ContainerFactory;
16
use Phuria\SQLBuilder\QueryBuilder\DeleteBuilder;
17
use Phuria\SQLBuilder\QueryBuilder\InsertBuilder;
18
use Phuria\SQLBuilder\QueryBuilder\InsertSelectBuilder;
19
use Phuria\SQLBuilder\QueryBuilder\SelectBuilder;
20
use Phuria\SQLBuilder\QueryBuilder\UpdateBuilder;
21
use Phuria\SQLBuilder\QueryCompiler\QueryCompilerInterface;
22
use Pimple\Container;
23
24
/**
25
 * @author Beniamin Jonatan Šimko <[email protected]>
26
 */
27
class QueryBuilderFactory
28
{
29
    /**
30
     * @var Container $container
31
     */
32
    private $container;
33
34
    /**
35
     * @param Container|null $container
36
     */
37 36
    public function __construct(Container $container = null)
38 36
    {
39 36
        $this->container = $container ?: (new ContainerFactory())->create();
40 36
    }
41
42
    /**
43
     * @return Container
44
     */
45 36
    public function getContainer()
46
    {
47
        return $this->container;
48 36
    }
49
50
    /**
51
     * @param ConnectionInterface $connection
52
     * @param string              $name
53
     */
54 36
    public function registerConnection(ConnectionInterface $connection, $name = 'default')
55
    {
56 36
        $this->container['phuria.sql_builder.connection_manager']->registerConnection(
57 1
            $connection, $name
58 1
        );
59 1
    }
60
61
    /**
62
     * @param string $class
63
     *
64
     * @return QueryCompilerInterface
65
     */
66 36
    private function createQueryBuilder($class)
67
    {
68 36
        $parameterClass = $this->container['phuria.sql_builder.parameter_manager.class'];
69
70 36
        return new $class(
71 36
            $this->container['phuria.sql_builder.table_factory'],
72 36
            $this->container['phuria.sql_builder.query_compiler'],
73 36
            new $parameterClass,
74 36
            $this->container['phuria.sql_builder.query_factory']
75 36
        );
76
    }
77
78
    /**
79
     * @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...
80
     */
81 27
    public function createSelect()
82
    {
83 27
        return $this->createQueryBuilder(SelectBuilder::class);
84
    }
85
86
    /**
87
     * @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...
88
     */
89 4
    public function createUpdate()
90
    {
91 4
        return $this->createQueryBuilder(UpdateBuilder::class);
92
    }
93
94
    /**
95
     * @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...
96
     */
97 3
    public function createDelete()
98
    {
99 3
        return $this->createQueryBuilder(DeleteBuilder::class);
100
    }
101
102
    /**
103
     * @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...
104
     */
105 36
    public function createInsert()
106
    {
107 2
        return $this->createQueryBuilder(InsertBuilder::class);
108 36
    }
109
110
    /**
111
     * @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...
112
     */
113 1
    public function createInsertSelect()
114
    {
115 1
        return $this->createQueryBuilder(InsertSelectBuilder::class);
116
    }
117
}