Passed
Push — master ( a39d50...8807b4 )
by Wilmer
03:04
created

Connection::initConnection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
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\Driver\PDO\ConnectionPDOInterface;
11
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
12
use Yiisoft\Db\Schema\Quoter;
13
use Yiisoft\Db\Schema\QuoterInterface;
14
use Yiisoft\Db\Schema\SchemaInterface;
15
use Yiisoft\Db\Transaction\TransactionInterface;
16
17
final class Connection extends ConnectionPDO implements ConnectionPDOInterface
18
{
19
    protected QueryBuilderInterface|null $queryBuilder = null;
20
    protected SchemaInterface|null $schema = null;
21
22
    public function createCommand(string $sql = null, array $params = []): CommandInterface
23
    {
24
        $command = new Command($this, $this->queryCache);
25
26
        if ($sql !== null) {
27
            $command->setSql($sql);
28
        }
29
30
        if ($this->logger !== null) {
31
            $command->setLogger($this->logger);
32
        }
33
34
        if ($this->profiler !== null) {
35
            $command->setProfiler($this->profiler);
36
        }
37
38
        return $command->bindValues($params);
39
    }
40
41
    public function createTransaction(): TransactionInterface
42
    {
43
        return new Transaction($this);
44
    }
45
46
    public function getQueryBuilder(): QueryBuilderInterface
47
    {
48
        if ($this->queryBuilder === null) {
49
            $this->queryBuilder = new QueryBuilder(
50
                $this->getQuoter(),
51
                $this->getSchema(),
52
            );
53
        }
54
55
        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...
56
    }
57
58
    public function getQuoter(): QuoterInterface
59
    {
60
        if ($this->quoter === null) {
61
            $this->quoter = new Quoter('', '', $this->getTablePrefix());
62
        }
63
64
        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...
65
    }
66
67
    public function getSchema(): SchemaInterface
68
    {
69
        if ($this->schema === null) {
70
            $this->schema = new Schema($this, $this->schemaCache);
71
        }
72
73
        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...
74
    }
75
76
    protected function initConnection(): void
77
    {
78
        if ($this->getEmulatePrepare() !== null) {
79
            $this->driver->attributes([PDO::ATTR_EMULATE_PREPARES => $this->getEmulatePrepare()]);
80
        }
81
82
        $this->pdo = $this->driver->createConnection();
83
    }
84
}
85