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

AbstractBuilder::setParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
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\QueryBuilder;
13
14
use Phuria\UnderQuery\Parameter\ParameterCollectionInterface;
15
use Phuria\UnderQuery\Query\Query;
16
use Phuria\UnderQuery\Query\QueryFactoryInterface;
17
use Phuria\UnderQuery\QueryCompiler\QueryCompilerInterface;
18
use Phuria\UnderQuery\Reference\ReferenceCollectionInterface;
19
use Phuria\UnderQuery\TableFactory\TableFactoryInterface;
20
21
/**
22
 * @author Beniamin Jonatan Šimko <[email protected]>
23
 */
24
abstract class AbstractBuilder implements BuilderInterface
25
{
26
    /**
27
     * @var TableFactoryInterface
28
     */
29
    private $tableFactory;
30
31
    /**
32
     * @var QueryCompilerInterface
33
     */
34
    private $queryCompiler;
35
36
    /**
37
     * @var ParameterCollectionInterface
38
     */
39
    private $parameterCollection;
40
41
    /**
42
     * @var ReferenceCollectionInterface
43
     */
44
    private $referenceCollection;
45
46
    /**
47
     * @var QueryFactoryInterface
48
     */
49
    private $queryFactory;
50
51
    /**
52
     * @param TableFactoryInterface     $tableFactory
53
     * @param QueryCompilerInterface    $queryCompiler
54
     * @param QueryFactoryInterface     $queryFactory
55
     * @param array                     $options
56
     */
57
    public function __construct(
58
        TableFactoryInterface $tableFactory,
59
        QueryCompilerInterface $queryCompiler,
60
        QueryFactoryInterface $queryFactory,
61
        array $options
62
    ) {
63
        $this->tableFactory = $tableFactory;
64
        $this->queryCompiler = $queryCompiler;
65
        $this->queryFactory = $queryFactory;
66
67
        $this->parameterCollection = new $options['parameter_collection_class'];
68
        $this->referenceCollection = new $options['reference_collection_class'];
69
    }
70
71
    /**
72
     * @return BuilderInterface
73
     */
74
    public function getQueryBuilder()
75
    {
76
        return $this;
77
    }
78
79
    /**
80
     * @return TableFactoryInterface
81
     */
82
    public function getTableFactory()
83
    {
84
        return $this->tableFactory;
85
    }
86
87
    /**
88
     * @return QueryCompilerInterface
89
     */
90
    public function getQueryCompiler()
91
    {
92
        return $this->queryCompiler;
93
    }
94
95
    /**
96
     * @return ParameterCollectionInterface
97
     */
98
    public function getParameters()
99
    {
100
        return $this->parameterCollection;
101
    }
102
103
    /**
104
     * @return ReferenceCollectionInterface
105
     */
106
    public function getReferences()
107
    {
108
        return $this->referenceCollection;
109
    }
110
111
    /**
112
     * @inheritdoc
113
     */
114
    public function buildSQL()
115
    {
116
        return $this->getQueryCompiler()->compile($this->getQueryBuilder());
117
    }
118
119
    /**
120
     * @inheritdoc
121
     */
122
    public function objectToString($object)
123
    {
124
        return $this->getReferences()->createReference($object);
125
    }
126
127
    /**
128
     * @inheritdoc
129
     */
130
    public function setParameter($name, $value)
131
    {
132
        $parameter = $this->getParameters()->getParameter($name);
133
        $parameter->setValue($value);
134
135
        return $this;
136
    }
137
138
    /**
139
     * @param mixed $connectionHint
140
     *
141
     * @return Query
142
     */
143
    public function buildQuery($connectionHint = 'default')
144
    {
145
        return $this->queryFactory->buildQuery(
146
            $this->buildSQL(),
147
            $this->getParameters()->toArray(),
148
            $connectionHint
149
        );
150
    }
151
}