Passed
Push — master ( ee6b03...f5a05a )
by Aleksandar
02:24
created

QueryRunner::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
c 0
b 0
f 0
nc 4
nop 2
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 3
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;
19
20
use ArekX\PQL\Contracts\Driver;
21
use ArekX\PQL\Contracts\QueryBuilder;
22
use ArekX\PQL\Contracts\ResultReader;
23
use ArekX\PQL\Contracts\StructuredQueryRunner;
24
use ArekX\PQL\Contracts\ResultBuilder;
25
use ArekX\PQL\Contracts\StructuredQuery;
26
27
/**
28
 * Runner for queries for allowing easy use of the
29
 * driver and builder.
30
 */
31
class QueryRunner implements StructuredQueryRunner
32
{
33
    /**
34
     * Current driver in use.
35
     * @var Driver
36
     */
37
    public Driver $driver;
38
39
    /**
40
     * Current query builder in use.
41
     *
42
     * @var QueryBuilder
43
     */
44
    public QueryBuilder $builder;
45
46
    /**
47
     * Creates a new instance of this runner
48
     *
49
     * @param Driver|null $driver Driver to be set to be used
50
     * @param QueryBuilder|null $builder Builder to be used
51
     * @return static
52
     */
53 21
    public static function create(?Driver $driver = null, ?QueryBuilder $builder = null): static
54
    {
55 21
        return new static($driver, $builder);
56
    }
57
58
    /**
59
     * Creates new instance of this runner
60
     *
61
     * @param Driver|null $driver Driver to be set to be used
62
     * @param QueryBuilder|null $builder Builder to be used
63
     */
64 21
    public function __construct(?Driver $driver = null, ?QueryBuilder $builder = null)
65
    {
66 21
        if ($driver) {
67 21
            $this->useDriver($driver);
68
        }
69
70 21
        if ($builder) {
71 21
            $this->useBuilder($builder);
72
        }
73
    }
74
75
    /**
76
     * @inheritDoc
77
     */
78 21
    public function useDriver(Driver $driver): static
79
    {
80 21
        $this->driver = $driver;
81 21
        return $this;
82
    }
83
84
    /**
85
     * @inheritDoc
86
     */
87 21
    public function useBuilder(QueryBuilder $builder): static
88
    {
89 21
        $this->builder = $builder;
90 21
        return $this;
91
    }
92
93
    /**
94
     * @inheritDoc
95
     */
96 6
    public function run(StructuredQuery $query): mixed
97
    {
98 6
        return $this->driver->run($this->builder->build($query));
99
    }
100
101
    /**
102
     * @inheritDoc
103
     */
104 5
    public function fetchFirst(StructuredQuery $query): mixed
105
    {
106 5
        return $this->driver->fetchFirst($this->builder->build($query));
107
    }
108
109
    /**
110
     * @inheritDoc
111
     */
112 2
    public function fetchAll(StructuredQuery $query): array
113
    {
114 2
        return $this->driver->fetchAll($this->builder->build($query));
115
    }
116
117
    /**
118
     * @inheritDoc
119
     */
120 9
    public function fetch(StructuredQuery $query): ResultBuilder
121
    {
122 9
        return $this->driver->fetch($this->builder->build($query));
123
    }
124
125
    /**
126
     * @inheritDoc
127
     */
128 4
    public function fetchReader(StructuredQuery $query): ResultReader
129
    {
130 4
        return $this->driver->fetchReader($this->builder->build($query));
131
    }
132
}
133