UnionBuilder   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 54
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getInitialParts() 0 3 1
A buildUnions() 0 8 3
A getRequiredParts() 0 3 1
A getLastParts() 0 3 1
A getPartBuilders() 0 5 1
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\Drivers\Pdo\MySql\Builders\Traits\SubQueryTrait;
21
use ArekX\PQL\Drivers\Pdo\MySql\MySqlQueryBuilderState;
22
use ArekX\PQL\Sql\Query\Union;
23
24
/**
25
 * Represents a query builder for building an UNION query
26
 *
27
 * @see Union
28
 */
29
class UnionBuilder extends QueryPartBuilder
30
{
31
    use SubQueryTrait;
32
33
    /**
34
     * @inheritDoc
35
     */
36 4
    protected function getInitialParts(): array
37
    {
38 4
        return [];
39
    }
40
41
    /**
42
     * @inheritDoc
43
     */
44 4
    protected function getRequiredParts(): array
45
    {
46 4
        return ['from'];
47
    }
48
49
    /**
50
     * @inheritDoc
51
     */
52 4
    protected function getPartBuilders(): array
53
    {
54 4
        return [
55 4
            'from' => fn($part, $state) => $this->buildQuery($part, $state),
56 4
            'union' => fn($part, $state) => $this->buildUnions($part, $state)
57 4
        ];
58
    }
59
60
    /**
61
     * Build other unions added.
62
     *
63
     * @param array $unions Unions to be built
64
     * @param MySqlQueryBuilderState $state Query builder state
65
     * @return string
66
     */
67 2
    protected function buildUnions(array $unions, MySqlQueryBuilderState $state): string
68
    {
69 2
        $result = [];
70 2
        foreach ($unions as [$query, $type]) {
71 2
            $result[] = ($type ? strtoupper($type) . ' ' : '') . $this->buildQuery($query, $state);
72
        }
73
74 2
        return 'UNION ' . implode(' UNION ', $result);
75
    }
76
77
    /**
78
     * @inheritDoc
79
     */
80 3
    protected function getLastParts(): array
81
    {
82 3
        return [];
83
    }
84
}
85