Completed
Push — master ( 8eb315...5aaba6 )
by Changwan
04:29
created

MysqlConnection::setEventEmitter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
namespace Wandu\Database\Connection;
3
4
use Exception;
5
use PDO;
6
use PDOStatement;
7
use Throwable;
8
use Wandu\Database\Contracts\Connection;
9
use Wandu\Database\Events\ExecuteQuery;
10
use Wandu\Event\Contracts\EventEmitter;
11
12
class MysqlConnection implements Connection
13
{
14
    /** @var \PDO */
15
    protected $pdo;
16
17
    /** @var \Wandu\Event\Contracts\EventEmitter */
18
    protected $emitter;
19
    
20 15
    public function __construct(PDO $pdo)
21
    {
22 15
        $this->pdo = $pdo;
23 15
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 14
    public function setEventEmitter(EventEmitter $emitter)
29
    {
30 14
        $this->emitter = $emitter;
31 14
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 6
    public function fetch(string $query, array $bindings = [])
37
    {
38 6
        $statement = $this->execute($query, $bindings);
39 6
        while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
40 6
            yield $row;
41
        }
42 6
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 8
    public function first(string $query, array $bindings = [])
48
    {
49 8
        $attributes = $this->execute($query, $bindings)->fetch(PDO::FETCH_ASSOC);
50 8
        return $attributes ?: null;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 2
    public function query(string $query, array $bindings = [])
57
    {
58 2
        return $this->execute($query, $bindings)->rowCount();
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 1
    public function getLastInsertId()
65
    {
66 1
        return $this->pdo->lastInsertId();
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function transaction(callable $handler)
73
    {
74
        $this->pdo->beginTransaction();
75
        try {
76
            $result = call_user_func($handler, $this);
77
        } catch (Exception $e) {
78
            $this->pdo->rollBack();
79
            throw $e;
80
        } catch (Throwable $e) {
81
            $this->pdo->rollBack();
82
            throw $e;
83
        }
84
        $this->pdo->commit();
85
        return $result;
86
    }
87
88
    /**
89
     * @param string $query
90
     * @param array $bindings
91
     * @return \PDOStatement
92
     */
93 14
    protected function execute(string $query, array $bindings = [])
94
    {
95 14
        $statement = $this->pdo->prepare($query);
96 14
        $this->bindValues($statement, $bindings);
97 14
        $statement->execute();
98 14
        if ($this->emitter) {
99 14
            $this->emitter->trigger(new ExecuteQuery($statement->queryString, $bindings));
100
        }
101 14
        return $statement;
102
    }
103
    
104
    /**
105
     * @param \PDOStatement $statement
106
     * @param array $bindings
107
     */
108 14
    protected function bindValues(PDOStatement $statement, array $bindings = [])
109
    {
110 14
        foreach ($bindings as $key => $value) {
111 14
            if (is_int($value)) {
112 7
                $dataType = PDO::PARAM_INT;
113 12
            } elseif (is_bool($value)) {
114
                $dataType = PDO::PARAM_BOOL;
115 12
            } elseif (is_null($value)) {
116
                $dataType = PDO::PARAM_NULL;
117
            } else {
118 12
                $dataType = PDO::PARAM_STR;
119
            }
120 14
            $statement->bindValue(
121 14
                is_int($key) ? $key + 1 : $key,
122
                $value,
123 14
                $dataType
124
            );
125
        }
126 14
    }
127
}
128