Passed
Push — master ( e8198e...156eb8 )
by Sebastian
02:23
created

ExtendedPDO::getLastOperationStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * Linna Framework.
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2017, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types=1);
11
12
namespace Linna\Storage;
13
14
use PDO;
15
use PDOStatement;
16
use InvalidArgumentException;
17
18
/**
19
 * Extended PDO
20
 */
21
class ExtendedPDO extends PDO
22
{
23
    /**
24
     * @var bool Status for last operation.
25
     */
26
    private $lastOperationStatus;
27
    
28
    /**
29
     * Executes an SQL statement with parameters,
30
     * returning a result set as a PDOStatement object
31
     *
32
     * @param string $query SQL statement
33
     * @param array $param Parameter as array as PDOStatement::bindParam
34
     *
35
     * @return PDOStatement
36
     */
37 15
    public function queryWithParam(string $query, array $param) : PDOStatement
38
    {
39 15
        $statment = $this->prepare($query);
40
41 15
        foreach ($param as $value) {
42 14
            $this->checkValue($value);
43
44
            //reassign as reference
45
            //because bindParam need it as reference
46 12
            $ref = $value;
47 12
            $ref[1] = &$value[1];
48
49 12
            call_user_func_array([$statment, "bindParam"], $ref);
50
        }
51
52 13
        $this->lastOperationStatus = $statment->execute();
53
54 12
        return $statment;
55
    }
56
57
    /**
58
     * Return the last opration status.
59
     *
60
     * @return bool
61
     */
62 9
    public function getLastOperationStatus() : bool
63
    {
64 9
        return $this->lastOperationStatus;
65
    }
66
    
67
    /**
68
     * Check values passed to queryWithParam.
69
     *
70
     * @param array $value
71
     * @throws InvalidArgumentException
72
     */
73 14
    private function checkValue(array &$value)
74
    {
75 14
        if (count($value) < 2) {
76 1
            throw new InvalidArgumentException(__METHOD__.': Parameters array must contain at least two elements with this form: [\':name\', \'value\']');
77
        }
78
79 13
        if (strpos($value[0], ':') !== 0) {
80 1
            throw new InvalidArgumentException(__METHOD__.': Parameter name will be in the form :name');
81
        }
82 12
    }
83
}
84