Passed
Push — master ( b979f8...3829ec )
by Aleksandar
07:20 queued 11s
created

QueryPartBuilder::buildQueryParts()   A

Complexity

Conditions 6
Paths 9

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 13
nc 9
nop 2
dl 0
loc 27
ccs 14
cts 14
cp 1
crap 6
rs 9.2222
c 0
b 0
f 0
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\Drivers\Pdo\MySql\Builders;
19
20
use ArekX\PQL\Contracts\QueryBuilder;
21
use ArekX\PQL\Contracts\QueryBuilderState;
22
use ArekX\PQL\Contracts\RawQuery;
23
use ArekX\PQL\Contracts\StructuredQuery;
24
use ArekX\PQL\Drivers\Pdo\MySql\MySqlQueryBuilderState;
25
use ArekX\PQL\RawQueryResult;
26
27
/**
28
 * Represents a base builder for query parts.
29
 */
30
abstract class QueryPartBuilder implements QueryBuilder
31
{
32
    /**
33
     * @inheritDoc
34
     */
35 74
    public function build(StructuredQuery $query, QueryBuilderState $state = null): RawQuery
36
    {
37
        /** @var MySqlQueryBuilderState $state */
38
39 74
        if ($state === null) {
40 1
            throw new \Exception('Passed state cannot be null.');
41
        }
42
43 73
        $input = $query->toArray();
44
45
46 73
        return RawQueryResult::create(
47 73
            $this->joinParts($state->getQueryPartGlue(), $this->buildQueryParts($input, $state)),
48 68
            $state->getParamsBuilder()->build(),
49 68
            $query->get('config') ?? null
50
        );
51
    }
52
53
    /**
54
     * Join built parts into one complete string.
55
     *
56
     * Part can be a string, in which case it will be joined with other by
57
     * using a string in $stringGlue
58
     *
59
     * If a part is an array it is used as a tuple [glue, partString] and
60
     * it will use its own glue string to join.
61
     *
62
     * @param string $stringGlue Default string glue to use if part is string
63
     * @param array $builtParts List of parts to join.
64
     *
65
     * @return string
66
     */
67 68
    protected function joinParts($stringGlue, array $builtParts): string
68
    {
69 68
        $result = '';
70
71 68
        foreach ($builtParts as $index => $part) {
72 68
            if (is_string($part)) {
73 68
                $result .= $index > 0 ? $stringGlue . $part : $part;
74 68
                continue;
75
            }
76
77 2
            [$partGlue, $partString] = $part;
78 2
            $result .= $partGlue . $partString;
79
        }
80
81 68
        return $result;
82
    }
83
84
    /**
85
     * Build each part from the structured query
86
     *
87
     * @param array $parts Parts from the structured query
88
     * @param MySqlQueryBuilderState $state Builder state
89
     * @return array Resulting array for each built part.
90
     * @throws \Exception
91
     */
92 73
    protected function buildQueryParts(array $parts, MySqlQueryBuilderState $state): array
93
    {
94 73
        $results = $this->getInitialParts();
95 73
        $requiredParts = $this->getRequiredParts();
96
97 73
        foreach ($this->getPartBuilders() as $partName => $buildPart) {
98 72
            if (empty($parts[$partName])) {
99
100 65
                if (in_array($partName, $requiredParts)) {
101 7
                    throw new \Exception("Part '${partName}' is required.");
102
                }
103
104 63
                continue;
105
            }
106
107 71
            $result = $buildPart($parts[$partName], $state);
108
109 71
            if ($result !== null) {
110 71
                $results[] = $result;
111
            }
112
        }
113
114 68
        foreach ($this->getLastParts() as $part) {
115 2
            $results[] = $part;
116
        }
117
118 68
        return $results;
119
    }
120
121
    /**
122
     * Return initial parts for the query
123
     *
124
     * @return array
125
     */
126
    protected abstract function getInitialParts(): array;
127
128
    /**
129
     * Return parts which must be set in the query in order to be built.
130
     * @return array
131
     */
132
    protected abstract function getRequiredParts(): array;
133
134
    /**
135
     * Return part builders for resolving the query parts to string.
136
     *
137
     * @return array
138
     */
139
    protected abstract function getPartBuilders(): array;
140
141
    /**
142
     * Return last parts to be applied at the end of the query.
143
     *
144
     * @return array
145
     */
146
    protected abstract function getLastParts(): array;
147
}