Completed
Push — master ( 5ef80d...669d28 )
by Beñat
02:38
created

Pdo::update()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
cc 5
eloc 15
nc 8
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Shared Kernel library.
5
 *
6
 * Copyright (c) 2016-present LIN3S <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace LIN3S\SharedKernel\Infrastructure\Persistence\Sql;
15
16
/**
17
 * @author Beñat Espiña <[email protected]>
18
 */
19
final class Pdo
20
{
21
    public const DATE_FORMAT = 'Y-m-d H:i:s';
22
23
    private $pdo;
24
25
    public function __construct(\PDO $pdo)
26
    {
27
        $this->pdo = $pdo;
28
        $this->pdo->setAttribute($this->pdo::ATTR_ERRMODE, $this->pdo::ERRMODE_EXCEPTION);
29
    }
30
31
    public function query(string $sql, array $parameters = []) : array
32
    {
33
        return $this->execute($sql, $parameters)->fetchAll(\PDO::FETCH_ASSOC);
34
    }
35
36
    public function count(string $sql, array $parameters = []) : int
37
    {
38
        return (int)$this->execute($sql, $parameters)->fetchColumn();
39
    }
40
41
    public function insert(string $table, array $parameters) : void
42
    {
43
        if (!is_array($parameters[array_keys($parameters)[0]])) {
44
            $parameters = [$parameters];
45
        }
46
47
        $values = [];
48
        foreach ($parameters as $parameter) {
49
            $values = array_merge($values, array_values($parameter));
50
        }
51
        $numberOfInsertions = count($parameters);
52
        $columns = array_keys($parameters[array_keys($parameters)[0]]);
53
        $rowPlaces = '(' . implode(', ', array_fill(0, count($columns), '?')) . ')';
54
        $allPlaces = implode(', ', array_fill(0, $numberOfInsertions, $rowPlaces));
55
56
        $sql = 'INSERT INTO ' . $table . ' (' . implode(', ', $columns) . ') VALUES ' . $allPlaces;
57
58
        $this->execute($sql, $values);
59
    }
60
61
    public function update(string $table, array $parameters) : void
62
    {
63
        if (!is_array($parameters[array_keys($parameters)[0]])) {
64
            $parameters = [$parameters];
65
        }
66
67
        foreach ($parameters as $columns) {
68
            $sql = 'UPDATE ' . $table . ' SET ';
69
            $updates = 0;
70
71
            foreach ($columns as $column => $value) {
72
                if ($updates !== 0) {
73
                    $sql .= ',';
74
                }
75
                $sql .= $column . '= ?';
76
                $updates++;
77
            }
78
            $sql .= ' WHERE ' . array_keys($columns)[0] . '= ?';
79
            $values = array_values($columns);
80
            $values[] = $values[0];
81
82
            $this->execute($sql, $values);
83
        }
84
    }
85
86
    public function executeAtomically(callable $function)
87
    {
88
        $this->pdo->beginTransaction();
89
90
        try {
91
            $return = call_user_func($function, $this);
92
93
            $this->pdo->commit();
94
95
            return $return ?: true;
96
        } catch (\Exception | \Throwable $exception) {
97
            $this->pdo->rollback();
98
99
            throw $exception;
100
        }
101
    }
102
103
    public function execute(string $sql, array $parameters) : \PDOStatement
104
    {
105
        $statement = $this->pdo->prepare($sql);
106
        $statement->execute($parameters);
107
108
        return $statement;
109
    }
110
}
111