Completed
Push — master ( e7358a...a8d41d )
by Alex
03:58 queued 01:47
created

PdoCrudStatement   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 43
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A prepare() 0 9 2
A bindParameter() 0 3 1
1
<?php
2
namespace Mezon\PdoCrud;
3
4
/**
5
 * Class PdoCrud
6
 *
7
 * @package Mezon
8
 * @subpackage PdoCrud
9
 * @author Dodonov A.A.
10
 * @version v.1.0 (2019/08/16)
11
 * @copyright Copyright (c) 2019, aeon.org
12
 */
13
14
/**
15
 * Class provides simple CRUD operations
16
 */
17
trait PdoCrudStatement
18
{
19
    
20
    /**
21
     * PDO statement
22
     *
23
     * @var \PDOStatement
24
     */
25
    private $pdoStatement = null;
26
27
    /**
28
     * Method sets safe query
29
     *
30
     * @param string $query
31
     *            safe query
32
     * @codeCoverageIgnore
33
     */
34
    public function prepare(string $query): void
35
    {
36
        $this->pdoStatement = $this->pdo->prepare($query, [
37
            \PDO::ATTR_CURSOR => \PDO::CURSOR_FWDONLY
38
        ]);
39
40
        if ($this->pdoStatement === false) {
41
            $errorInfo = $this->pdo->errorInfo();
42
            throw (new \Exception('Query "' . $query . '" was not prepared. ' . $errorInfo[2], - 1));
43
        }
44
    }
45
46
    /**
47
     * Method binds parameters to query
48
     *
49
     * @param string $parameter
50
     *            name of the parameter
51
     * @param mixed $variable
52
     *            value
53
     * @param int $type
54
     *            parameter type
55
     * @codeCoverageIgnore
56
     */
57
    public function bindParameter(string $parameter, $variable, int $type = \PDO::PARAM_STR): void
58
    {
59
        $this->pdoStatement->bindParam($parameter, $variable, $type);
60
    }
61
}
62