Passed
Push — master ( 4ac107...bec696 )
by Aleksandar
72:47
created

CallBuilder::getLastParts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
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\MySql\Builder\Builders;
19
20
use ArekX\PQL\Contracts\StructuredQuery;
21
use ArekX\PQL\Drivers\MySql\Builder\Builders\Traits\ValueColumnTrait;
22
use ArekX\PQL\Drivers\MySql\Builder\MySqlQueryBuilderState;
23
use ArekX\PQL\Sql\Statement\Call;
24
25
/**
26
 * Represents a query builder for building a stored procedure call.
27
 *
28
 * @see Call
29
 */
30
class CallBuilder extends QueryPartBuilder
31
{
32
    use ValueColumnTrait;
33
34
    /**
35
     * @inheritDoc
36
     */
37 3
    protected function getInitialParts(): array
38
    {
39 3
        return ['CALL'];
40
    }
41
42
    /**
43
     * @inheritDoc
44
     */
45 6
    protected function getRequiredParts(): array
46
    {
47 6
        return ['name'];
48
    }
49
50
    /**
51
     * @inheritDoc
52
     */
53 6
    protected function getPartBuilders(): array
54
    {
55
        return [
56 6
            'name' => fn($name, MySqlQueryBuilderState $state) => $this->buildNamePart($name, $state),
57 6
            'params' => fn($params, MySqlQueryBuilderState $state) => ['', $this->buildParamsPart($params, $state)],
58
        ];
59
    }
60
61
    /**
62
     * Build name of the procedure part.
63
     *
64
     * @param string $name Name of the procedure
65
     * @param MySqlQueryBuilderState $state Current query builder state.
66
     * @return string
67
     */
68 6
    protected function buildNamePart($name, MySqlQueryBuilderState $state)
69
    {
70 6
        if ($name instanceof StructuredQuery) {
0 ignored issues
show
introduced by
$name is never a sub-type of ArekX\PQL\Contracts\StructuredQuery.
Loading history...
71 2
            return $this->buildQuery($name, $state);
72
        }
73
74 6
        return $name;
75
    }
76
77
    /**
78
     * Build procedure parameters part.
79
     *
80
     * @param array $params Procedure parameters to be built.
81
     * @param MySqlQueryBuilderState $state Current query builder state.
82
     * @return string
83
     *
84
     * @throws \Exception
85
     */
86 4
    protected function buildParamsPart($params, MySqlQueryBuilderState $state)
87
    {
88 4
        $result = [];
89
90 4
        foreach ($params as $param) {
91 4
            $result[] = $this->buildValueColumn($param, $state);
92
        }
93
94 2
        return '(' . implode(', ', $result) . ')';
95
    }
96
97
    /**
98
     * @inheritDoc
99
     */
100 4
    protected function getLastParts(): array
101
    {
102 4
        return [];
103
    }
104
}