Completed
Push — master ( 408932...ce7eb5 )
by Dawid
06:58
created

Connection   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 70%

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 99
ccs 28
cts 40
cp 0.7
rs 10
c 0
b 0
f 0
wmc 16

12 Methods

Rating   Name   Duplication   Size   Complexity  
A quote() 0 3 1
A beginTransaction() 0 3 1
A __construct() 0 4 1
A getBaseConnection() 0 4 1
A commit() 0 3 1
A open() 0 11 2
A inTransaction() 0 3 1
A execute() 0 13 4
A log() 0 3 1
A rollBack() 0 3 1
A close() 0 3 1
A isOpen() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Igni\Storage\Driver\Pdo;
4
5
use Igni\Storage\Driver\Connection as ConnectionInterface;
6
use Igni\Storage\Exception\ConnectionException;
7
use PDO;
8
9
final class Connection implements ConnectionInterface
10
{
11
    /** @var PDO|null */
12
    private $handler;
13
14
    /** @var string */
15
    private $dsn;
16
17
    /** @var ConnectionOptions */
18
    private $options;
19
20
    /** @var array */
21
    private $queryLog = [];
22
23 49
    public function __construct(string $dsn, ConnectionOptions $options = null)
24
    {
25 49
        $this->dsn = $dsn;
26 49
        $this->options = $options ?? new ConnectionOptions();
27 49
    }
28
29 48
    public function close(): void
30
    {
31 48
        $this->handler = null;
32 48
    }
33
34 25
    public function open(): void
35
    {
36 25
        if ($this->isOpen()) {
37 22
            return;
38
        }
39
40 25
        $this->handler = new PDO(
41 25
            $this->dsn,
42 25
            $this->options->getUsername(),
43 25
            $this->options->getPassword(),
44 25
            $this->options->getAttributes()
45
        );
46 25
    }
47
48 25
    public function isOpen(): bool
49
    {
50 25
        return $this->handler !== null;
51
    }
52
53
    /**
54
     * @param string $query
55
     * @param array $parameters
56
     * @return Cursor
57
     * @throws ConnectionException
58
     */
59 24
    public function execute(...$parameters): Cursor
60
    {
61 24
        if (!$this->isOpen()) {
62 21
            $this->open();
63
        }
64
65 24
        $query = $parameters[0];
66
67 24
        if (isset($parameters[1]) && is_array($parameters[1])) {
68 13
            return new Cursor($this, $query, $parameters[1]);
69
        }
70
71 11
        return new Cursor($this, $query);
72
    }
73
74
    public function beginTransaction(): bool
75
    {
76
        return $this->handler->beginTransaction();
0 ignored issues
show
Bug introduced by
The method beginTransaction() does not exist on null. ( Ignorable by Annotation )

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

76
        return $this->handler->/** @scrutinizer ignore-call */ beginTransaction();

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...
77
    }
78
79
    public function inTransaction(): bool
80
    {
81
        return $this->handler->inTransaction();
82
    }
83
84
    public function commit(): bool
85
    {
86
        return $this->handler->commit();
87
    }
88
89
    public function rollBack(): bool
90
    {
91
        return $this->handler->rollBack();
92
    }
93
94
    public function quote($string): string
95
    {
96
        return $this->handler->quote((string) $string);
97
    }
98
99 23
    public function getBaseConnection(): PDO
100
    {
101 23
        $this->open();
102 23
        return $this->handler;
103
    }
104
105
    public function log(string $query)
106
    {
107
        $this->queryLog[] = $query;
108
    }
109
}
110