SqlDatabase::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 4
1
<?php
2
3
namespace Arrilot\DataAnonymization\Database;
4
5
use PDO;
6
7
class SqlDatabase implements DatabaseInterface
8
{
9
    /**
10
     * PDO instance.
11
     *
12
     * @var PDO
13
     */
14
    protected $pdo;
15
16
    /**
17
     * Constructor.
18
     *
19
     * @param string     $dsn
20
     * @param string     $user
21
     * @param string     $password
22
     * @param null|array $options
23
     */
24
    public function __construct($dsn, $user, $password, $options = null)
25
    {
26
        if (is_null($options)) {
27
            $options = [
28
                PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
29
                PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
30
            ];
31
        }
32
33
        $this->pdo = new PDO($dsn, $user, $password, $options);
34
    }
35
36
    /**
37
     * Get all rows from a $table specified by where. Only $columns are selected.
38
     *
39
     * @param string      $table
40
     * @param string      $columns
41
     * @param string|null $where
42
     *
43
     * @return array
44
     */
45
    public function getRows($table, $columns, $where)
46
    {
47
        $columns = implode(',', $columns);
48
        $sql = "SELECT {$columns} FROM {$table}";
49
50
        if ($where) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $where of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
51
            $sql .= " WHERE {$where}";
52
        }
53
54
        $stmt = $this->pdo->query($sql);
55
        while ($row = $stmt->fetch()) {
56
            yield $row;
57
        }
58
    }
59
60
    /**
61
     * Update $column value with a $newValue.
62
     * Update is performed on a row specified by $primaryValue of $table.
63
     *
64
     * @param string $table
65
     * @param array  $primaryKeyValue
66
     * @param string $column
67
     * @param mixed $value
68
     *
69
     * @return void
70
     */
71
    public function updateByPrimary($table, $primaryKeyValue, $column, $value)
72
    {
73
        $where = $this->buildWhereForArray($primaryKeyValue);
74
        $quotedValue = $value === null ? 'null' : $this->pdo->quote($value);
75
76
        $sql = "UPDATE
77
                    {$table}
78
                SET
79
                    {$column} = {$quotedValue}
80
                WHERE
81
                    {$where}";
82
83
        $this->pdo->query($sql);
84
    }
85
86
    /**
87
     * Build SQL where for key-value array.
88
     *
89
     * @param array $primaryKeyValue
90
     *
91
     * @return string
92
     */
93
    protected function buildWhereForArray($primaryKeyValue)
94
    {
95
        $where = [];
96
        foreach ($primaryKeyValue as $key => $value) {
97
            $where[] = "{$key}='{$value}'";
98
        }
99
100
        return implode(' AND ', $where);
101
    }
102
}
103