Passed
Push — master ( b5bf25...b5ca6c )
by Aleksandar
02:13
created

SqlQueryBuilderFactory::build()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
1
<?php
2
/**
3
 * Copyright 2021 Aleksandar Panic
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *   http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
namespace ArekX\PQL\Sql;
19
20
use ArekX\PQL\Contracts\QueryBuilder;
21
use ArekX\PQL\Contracts\QueryBuilderFactory;
22
use ArekX\PQL\Contracts\QueryBuilderState;
23
use ArekX\PQL\Contracts\RawQuery;
24
use ArekX\PQL\Contracts\StructuredQuery;
25
26
/**
27
 * Represents a sql query builder factory
28
 * for mapping queries to builders.
29
 */
30
abstract class SqlQueryBuilderFactory implements QueryBuilderFactory, QueryBuilder
31
{
32
    /**
33
     * Created builder instances.
34
     * @var QueryBuilder[]
35
     */
36
    protected $createdBuilders = [];
37
38
    /**
39
     * @inheritDoc
40
     */
41 5
    public function build(StructuredQuery $query, QueryBuilderState $state = null): RawQuery
42
    {
43 5
        return $this->getBuilder($query)->build($query, $state ?: $this->createState());
44
    }
45
46
    /**
47
     * @inheritDoc
48
     */
49 7
    public function getBuilder(StructuredQuery $query): QueryBuilder
50
    {
51 7
        $class = get_class($query);
52
53 7
        if (empty($this->createdBuilders[$class])) {
54 7
            $this->createdBuilders[$class] = $this->createBuilder($class);
55
        }
56
57 6
        return $this->createdBuilders[$class];
58
    }
59
60
    /**
61
     * Create a builder from a query class.
62
     *
63
     * @param string $queryClass Class of the query to be created.
64
     * @return QueryBuilder
65
     */
66
    protected abstract function createBuilder(string $queryClass): QueryBuilder;
67
68
    /**
69
     * Create a new state for this query builder.
70
     * @return QueryBuilderState
71
     */
72
    protected abstract function createState(): QueryBuilderState;
73
}