Passed
Pull Request — master (#136)
by Wilmer
12:57
created

ConnectionPDO::createTransaction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mysql;
6
7
use PDO;
8
use Yiisoft\Db\Driver\PDO\CommandPDOInterface;
9
use Yiisoft\Db\Driver\PDO\ConnectionPDO as AbstractConnectionPDO;
10
use Yiisoft\Db\Exception\Exception;
11
use Yiisoft\Db\Exception\InvalidConfigException;
12
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
13
use Yiisoft\Db\Schema\QuoterInterface;
14
use Yiisoft\Db\Schema\SchemaInterface;
15
use Yiisoft\Db\Transaction\TransactionInterface;
16
17
/**
18
 * Database connection class prefilled for MySQL Server.
19
 * The class Connection represents a connection to a database via [PDO](https://secure.php.net/manual/en/book.pdo.php).
20
 */
21
final class ConnectionPDO extends AbstractConnectionPDO
22
{
23 186
    public function createCommand(?string $sql = null, array $params = []): CommandPDOInterface
24
    {
25 186
        $command = new CommandPDO($this, $this->queryCache);
26
27 186
        if ($sql !== null) {
28 184
            $command->setSql($sql);
29
        }
30
31 186
        if ($this->logger !== null) {
32 186
            $command->setLogger($this->logger);
33
        }
34
35 186
        if ($this->profiler !== null) {
36 186
            $command->setProfiler($this->profiler);
37
        }
38
39 186
        return $command->bindValues($params);
40
    }
41
42 11
    public function createTransaction(): TransactionInterface
43
    {
44 11
        return new TransactionPDO($this);
45
    }
46
47
    /**
48
     * @throws Exception|InvalidConfigException
49
     */
50 357
    public function getQueryBuilder(): QueryBuilderInterface
51
    {
52 357
        if ($this->queryBuilder === null) {
53 357
            $this->queryBuilder = new QueryBuilder(
54 357
                $this->getQuoter(),
55 357
                $this->getSchema(),
56
            );
57
        }
58
59 357
        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...
60
    }
61
62 374
    public function getQuoter(): QuoterInterface
63
    {
64 374
        if ($this->quoter === null) {
65 374
            $this->quoter = new Quoter('`', '`', $this->getTablePrefix());
66
        }
67
68 374
        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...
69
    }
70
71
    public function getName(): string
72
    {
73
        return $this->getDriver()->getName();
0 ignored issues
show
Bug introduced by
The method getName() does not exist on Yiisoft\Db\Driver\PDO\PDODriverInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
        return $this->getDriver()->/** @scrutinizer ignore-call */ getName();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
74
    }
75
76 384
    public function getSchema(): SchemaInterface
77
    {
78 384
        if ($this->schema === null) {
79 384
            $this->schema = new Schema($this, $this->schemaCache);
80
        }
81
82 384
        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...
83
    }
84
85
    /**
86
     * Initializes the DB connection.
87
     *
88
     * This method is invoked right after the DB connection is established.
89
     *
90
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
91
     *
92
     * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty.
93
     *
94
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
95
     */
96 199
    protected function initConnection(): void
97
    {
98 199
        if ($this->getEmulatePrepare() !== null) {
99 2
            $this->driver->attributes([PDO::ATTR_EMULATE_PREPARES => $this->getEmulatePrepare()]);
100
        }
101
102 199
        $this->pdo = $this->driver->createConnection();
103
    }
104
}
105