Passed
Push — master ( ded3fd...8acf3f )
by Aleksandar
03:45
created

UnionBuilder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 46
ccs 13
cts 13
cp 1
rs 10
wmc 6

4 Methods

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