Passed
Pull Request — master (#380)
by Wilmer
02:46
created

Connection::createTransaction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
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\Stubs;
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\Tests\Support\Mock;
16
use Yiisoft\Db\Transaction\TransactionInterface;
17
18
final class Connection extends ConnectionPDO implements ConnectionPDOInterface
19
{
20
    protected QueryBuilderInterface|null $queryBuilder = null;
21
    protected SchemaInterface|null $schema = null;
22
    private Mock|null $mock = null;
23
24
    public function __construct(string $dsn)
25
    {
26
        parent::__construct(new PDODriver($dsn), $this->getMock()->getQueryCache(), $this->getMock()->getSchemaCache());
27
    }
28
29
    public function createCommand(string $sql = null, array $params = []): CommandInterface
30
    {
31
        $command = new Command($this, $this->getMock()->getQueryCache());
32
33
        if ($sql !== null) {
34
            $command->setSql($sql);
35
        }
36
37
        if ($this->logger !== null) {
38
            $command->setLogger($this->logger);
39
        }
40
41
        if ($this->profiler !== null) {
42
            $command->setProfiler($this->profiler);
43
        }
44
45
        return $command->bindValues($params);
46
    }
47
48
    public function createTransaction(): TransactionInterface
49
    {
50
        return new Transaction($this);
51
    }
52
53
    public function getMock(): Mock
54
    {
55
        if ($this->mock === null) {
56
            $this->mock = new Mock();
57
        }
58
59
        return $this->mock;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->mock could return the type null which is incompatible with the type-hinted return Yiisoft\Db\Tests\Support\Mock. Consider adding an additional type-check to rule them out.
Loading history...
60
    }
61
62
    public function getQueryBuilder(): QueryBuilderInterface
63
    {
64
        if ($this->queryBuilder === null) {
65
            $this->queryBuilder = new QueryBuilder(
66
                $this->getQuoter(),
67
                $this->getSchema(),
68
            );
69
        }
70
71
        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...
72
    }
73
74
    public function getQuoter(): QuoterInterface
75
    {
76
        if ($this->quoter === null) {
77
            $this->quoter = new Quoter('`', '`', $this->getTablePrefix());
78
        }
79
80
        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...
81
    }
82
83
    public function getSchema(): SchemaInterface
84
    {
85
        if ($this->schema === null) {
86
            $this->schema = new Schema($this, $this->getMock()->getSchemaCache());
87
        }
88
89
        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...
90
    }
91
92
    protected function initConnection(): void
93
    {
94
        if ($this->getEmulatePrepare() !== null) {
95
            $this->driver->attributes([PDO::ATTR_EMULATE_PREPARES => $this->getEmulatePrepare()]);
96
        }
97
98
        $this->pdo = $this->driver->createConnection();
99
    }
100
}
101