Completed
Push — master ( c5c126...29bff4 )
by Edson
01:37
created

ActiveRecord::config()   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
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
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
    private static $config;
0 ignored issues
show
introduced by
The private property $config is not used, and could be removed.
Loading history...
21
22
    public static function connection(string $dsn, string $user, string $pass): void
23
    {
24
        ActiveRecord::$dbh = new PDO($dsn, $user, $pass);
25
        ActiveRecord::$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
26
    }
27
28
    private static function getConn(): PDO
29
    {
30
        return ActiveRecord::$dbh;
31
    }
32
33
    private static function prepare(string $query): PDOStatement
34
    {
35
        return ActiveRecord::$sth = ActiveRecord::getConn()->prepare(trim($query));
36
    }
37
38
    protected static function execute(string $query, array $parameters = []): ?PDOStatement
39
    {
40
        if (ActiveRecord::prepare($query)->execute($parameters)) {
41
            return ActiveRecord::$sth;
42
        }
43
        return null;
44
    }
45
46
    protected static function exec(string $sql): int
47
    {
48
        return ActiveRecord::$dbh->exec($sql);
49
    }
50
}
51