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

QueryRunner::create()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
nc 4
nop 2
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 3
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;
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 null $builder Builder to be used
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $builder is correct as it would always require null to be passed?
Loading history...
51
     * @return static
52
     */
53 5
    public static function create($driver = null, $builder = null)
54
    {
55 5
        $instance = new static();
56
57 5
        if ($driver) {
58 5
            $instance->useDriver($driver);
59
        }
60
61 5
        if ($builder) {
0 ignored issues
show
introduced by
$builder is of type null, thus it always evaluated to false.
Loading history...
62 5
            $instance->useBuilder($builder);
63
        }
64
65 5
        return $instance;
66
    }
67
68
    /**
69
     * @inheritDoc
70
     */
71 5
    public function useDriver(Driver $driver): StructuredQueryRunner
72
    {
73 5
        $this->driver = $driver;
74 5
        return $this;
75
    }
76
77
    /**
78
     * @inheritDoc
79
     */
80 5
    public function useBuilder(QueryBuilder $builder): StructuredQueryRunner
81
    {
82 5
        $this->builder = $builder;
83 5
        return $this;
84
    }
85
86
    /**
87
     * @inheritDoc
88
     */
89 1
    public function run(StructuredQuery $query)
90
    {
91 1
        return $this->driver->run($this->builder->build($query));
92
    }
93
94
    /**
95
     * @inheritDoc
96
     */
97 1
    public function fetchFirst(StructuredQuery $query)
98
    {
99 1
        return $this->driver->fetchFirst($this->builder->build($query));
100
    }
101
102
    /**
103
     * @inheritDoc
104
     */
105 1
    public function fetchAll(StructuredQuery $query): array
106
    {
107 1
        return $this->driver->fetchAll($this->builder->build($query));
108
    }
109
110
    /**
111
     * @inheritDoc
112
     */
113 1
    public function fetch(StructuredQuery $query): ResultBuilder
114
    {
115 1
        return $this->driver->fetch($this->builder->build($query));
116
    }
117
118
    /**
119
     * @inheritDoc
120
     */
121 1
    public function fetchReader(StructuredQuery $query): ResultReader
122
    {
123 1
        return $this->driver->fetchReader($this->builder->build($query));
124
    }
125
}