PdoDatabaseObject::execute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
/**
3
 * Class encapsulating the DB connection.
4
 *
5
 * @author    Matthias Gisder <[email protected]>
6
 * @copyright 2014 inGenerator Ltd
7
 * @licence   BSD
8
 */
9
10
namespace Ingenerator\RunSingle;
11
12
use \PDO;
13
14
class PdoDatabaseObject
15
{
16
    /**
17
     * @var string
18
     */
19
    protected $db_table_name;
20
21
    /**
22
     * @var \PDO
23
     */
24
    protected $pdo;
25
26
    /**
27
     * @var bool
28
     */
29
    protected $is_initialised = FALSE;
30
31
    /**
32
     * @param \PDO      $pdo
33
     * @param string    $db_table_name
34
     */
35
    public function __construct(\PDO $pdo, $db_table_name)
36
    {
37
        $this->pdo           = $pdo;
38
        $this->db_table_name = $db_table_name;
39
    }
40
41
    protected function init()
42
    {
43
        if (! $this->is_initialised) {
44
            $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
45
            $this->is_initialised = TRUE;
46
        }
47
    }
48
49
    /**
50
     * @return string
51
     */
52
    public function get_db_table_name()
53
    {
54
        return $this->db_table_name;
55
    }
56
57
    /**
58
     * @param string  $sql
59
     * @param mixed[] $params
60
     *
61
     * @return \PDOStatement
62
     */
63
    public function execute($sql, $params)
64
    {
65
        $this->init();
66
        $q = $this->pdo->prepare($sql);
67
68
        foreach ($params as $key => $value) {
69
            $q->bindParam($key, $value);
70
        }
71
72
        $q->execute($params);
73
74
        return $q;
75
    }
76
77
    /**
78
     * @param string  $sql
79
     * @param mixed[] $params
80
     *
81
     * @return string[]|object|false
0 ignored issues
show
Documentation introduced by
Should the return type not be array? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
82
     */
83
    public function fetch_all($sql, $params)
84
    {
85
        $this->init();
86
        $q = $this->execute($sql, $params);
87
88
        return $q->fetchAll();
89
    }
90
91
}
92