Passed
Pull Request — master (#380)
by Wilmer
04:10 queued 01:20
created

anonymous//tests/Support/Stubs/Connection.php$0   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 9
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 10
wmc 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Tests\Support\Stubs;
6
7
use Yiisoft\Db\Driver\PDO\CommandPDO;
8
use Yiisoft\Db\Driver\PDO\CommandPDOInterface;
9
use Yiisoft\Db\Driver\PDO\ConnectionPDO;
10
use Yiisoft\Db\Driver\PDO\ConnectionPDOInterface;
11
use Yiisoft\Db\Driver\PDO\PDODriver;
12
use Yiisoft\Db\Driver\PDO\PDODriverInterface;
13
use Yiisoft\Db\Driver\PDO\TransactionPDO;
14
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
15
use Yiisoft\Db\Schema\Quoter;
16
use Yiisoft\Db\Schema\QuoterInterface;
17
use Yiisoft\Db\Schema\SchemaInterface;
18
use Yiisoft\Db\Tests\Support\Mock;
19
use Yiisoft\Db\Transaction\TransactionInterface;
20
21
final class Connection extends ConnectionPDO implements ConnectionPDOInterface
22
{
23
    protected QueryBuilderInterface|null $queryBuilder = null;
24
    protected SchemaInterface|null $schema = null;
25
26
    public function __construct()
27
    {
28
        $this->mock = new Mock();
29
30
        parent::__construct($this->pdoDriver(), $this->mock->getQueryCache(), $this->mock->getSchemaCache());
0 ignored issues
show
Bug Best Practice introduced by
The property mock does not exist on Yiisoft\Db\Tests\Support\Stubs\Connection. Did you maybe forget to declare it?
Loading history...
31
    }
32
33
    protected function initConnection(): void
34
    {
35
    }
36
37
    public function createCommand(string $sql = null, array $params = []): CommandPDOInterface
38
    {
39
        $command = new CommandPDO($this, $this->mock->getQueryCache());
0 ignored issues
show
Bug Best Practice introduced by
The property mock does not exist on Yiisoft\Db\Tests\Support\Stubs\Connection. Did you maybe forget to declare it?
Loading history...
40
41
        if ($sql !== null) {
42
            $command->setSql($sql);
43
        }
44
45
        if ($this->logger !== null) {
46
            $command->setLogger($this->logger);
47
        }
48
49
        if ($this->profiler !== null) {
50
            $command->setProfiler($this->profiler);
51
        }
52
53
        return $command->bindValues($params);
54
    }
55
56
    public function createTransaction(): TransactionInterface
57
    {
58
        return new TransactionPDO($this);
59
    }
60
61
    public function getQueryBuilder(): QueryBuilderInterface
62
    {
63
        if ($this->queryBuilder === null) {
64
            $this->queryBuilder = new QueryBuilder(
65
                $this->getQuoter(),
66
                $this->getSchema(),
67
            );
68
        }
69
70
        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...
71
    }
72
73
    public function getQuoter(): QuoterInterface
74
    {
75
        if ($this->quoter === null) {
76
            $this->quoter = new Quoter('`', '`', $this->getTablePrefix());
77
        }
78
79
        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...
80
    }
81
82
    public function getSchema(): SchemaInterface
83
    {
84
        if ($this->schema === null) {
85
            $this->schema = new Schema($this, $this->mock->getSchemaCache());
0 ignored issues
show
Bug Best Practice introduced by
The property mock does not exist on Yiisoft\Db\Tests\Support\Stubs\Connection. Did you maybe forget to declare it?
Loading history...
86
        }
87
88
        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...
89
    }
90
91
    private function pdoDriver(): PDODriverInterface
92
    {
93
        return new class ('sqlite::memory:') extends PDODriver implements PDODriverInterface {
94
            public function __construct(string $dsn)
95
            {
96
                parent::__construct($dsn);
97
            }
98
99
            public function getDriverName(): string
100
            {
101
                return 'sqlite';
102
            }
103
        };
104
    }
105
}
106