Completed
Push — master ( 29bff4...b22283 )
by Edson
10:30
created

ActiveRecord::conn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * activerecord.php é uma biblioteca PHP que permite ao desenvolvedor
7
 * trabalhar com banco de dados usando o padrão de projeto Active Record
8
 * (https://en.wikipedia.org/wiki/Active_record_pattern)
9
 */
10
11
namespace Bonfim\ActiveRecord;
12
13
use PDO;
14
use PDOStatement;
15
16
class ActiveRecord
17
{
18
    private static $dbh;
19
    private static $sth;
20
21
    public static function conn(string $dsn, string $user, string $pass): void
22
    {
23
        ActiveRecord::$dbh = new PDO($dsn, $user, $pass);
24
        ActiveRecord::$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
25
    }
26
27
    private static function getConn(): PDO
28
    {
29
        return ActiveRecord::$dbh;
30
    }
31
32
    private static function prepare(string $query): PDOStatement
33
    {
34
        return ActiveRecord::$sth = ActiveRecord::getConn()->prepare(trim($query));
35
    }
36
37
    protected static function execute(string $query, array $parameters = []): ?PDOStatement
38
    {
39
        if (ActiveRecord::prepare($query)->execute($parameters)) {
40
            return ActiveRecord::$sth;
41
        }
42
        return null;
43
    }
44
45
    protected static function exec(string $sql): int
46
    {
47
        return ActiveRecord::$dbh->exec($sql);
48
    }
49
}
50