Passed
Pull Request — master (#425)
by Wilmer
11:59 queued 07:53
created

Connection::getMigrationBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Tests\Support\Stub;
6
7
use PDO;
8
use Yiisoft\Db\Command\CommandInterface;
9
use Yiisoft\Db\Driver\PDO\ConnectionPDO;
10
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
11
use Yiisoft\Db\Schema\Quoter;
12
use Yiisoft\Db\Schema\QuoterInterface;
13
use Yiisoft\Db\Schema\SchemaInterface;
14
use Yiisoft\Db\Transaction\TransactionInterface;
15
16
final class Connection extends ConnectionPDO
17
{
18
    protected QueryBuilderInterface|null $queryBuilder = null;
19
    protected SchemaInterface|null $schema = null;
20
21
    public function createCommand(string $sql = null, array $params = []): CommandInterface
22
    {
23
        $command = new Command($this, $this->queryCache);
24
25
        if ($sql !== null) {
26
            $command->setSql($sql);
27
        }
28
29
        if ($this->logger !== null) {
30
            $command->setLogger($this->logger);
31
        }
32
33
        if ($this->profiler !== null) {
34
            $command->setProfiler($this->profiler);
35
        }
36
37
        return $command->bindValues($params);
38
    }
39
40
    public function createTransaction(): TransactionInterface
41
    {
42
        return new Transaction($this);
43
    }
44
45
    public function getQueryBuilder(): QueryBuilderInterface
46
    {
47
        if ($this->queryBuilder === null) {
48
            $this->queryBuilder = new QueryBuilder(
49
                $this->getQuoter(),
50
                $this->getSchema(),
51
            );
52
        }
53
54
        return $this->queryBuilder;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->queryBuilder could return the type null which is incompatible with the type-hinted return Yiisoft\Db\QueryBuilder\QueryBuilderInterface. Consider adding an additional type-check to rule them out.
Loading history...
55
    }
56
57
    public function getQuoter(): QuoterInterface
58
    {
59
        if ($this->quoter === null) {
60
            $this->quoter = new Quoter(['[', ']'], ['[', ']'], $this->getTablePrefix());
61
        }
62
63
        return $this->quoter;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->quoter could return the type null which is incompatible with the type-hinted return Yiisoft\Db\Schema\QuoterInterface. Consider adding an additional type-check to rule them out.
Loading history...
64
    }
65
66
    public function getMigrationBuilder(): MigrationBuilder
67
    {
68
        return new MigrationBuilder($this->getSchema());
69
    }
70
71
    public function getSchema(): SchemaInterface
72
    {
73
        if ($this->schema === null) {
74
            $this->schema = new Schema($this, $this->schemaCache);
75
        }
76
77
        return $this->schema;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->schema could return the type null which is incompatible with the type-hinted return Yiisoft\Db\Schema\SchemaInterface. Consider adding an additional type-check to rule them out.
Loading history...
78
    }
79
80
    protected function initConnection(): void
81
    {
82
        if ($this->getEmulatePrepare() !== null) {
83
            $this->driver->attributes([PDO::ATTR_EMULATE_PREPARES => $this->getEmulatePrepare()]);
84
        }
85
86
        $this->pdo = $this->driver->createConnection();
87
    }
88
}
89