Completed
Push — master ( 7aaa3d...861874 )
by Jared
01:36
created

Executable::beginTransaction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * @author Jared King <[email protected]>
5
 *
6
 * @see http://jaredtking.com
7
 *
8
 * @copyright 2015 Jared King
9
 * @license MIT
10
 */
11
12
namespace JAQB\Operations;
13
14
use PDO;
15
16
trait Executable
17
{
18
    /**
19
     * @var PDO
20
     */
21
    protected $pdo;
22
23
    /**
24
     * @var int
25
     */
26
    protected $rowCount;
27
28
    /**
29
     * Sets the PDO instance used by this query.
30
     *
31
     * @param PDO $pdo
32
     *
33
     * @return self
34
     */
35
    public function setPDO(PDO $pdo)
36
    {
37
        $this->pdo = $pdo;
38
39
        return $this;
40
    }
41
42
    /**
43
     * Gets the PDO instance used by this query.
44
     *
45
     * @return PDO
46
     */
47
    public function getPDO()
48
    {
49
        return $this->pdo;
50
    }
51
52
    /**
53
     * Executes a query.
54
     *
55
     * @return \PDOStatement|bool result
56
     */
57
    public function execute()
58
    {
59
        $stmt = $this->pdo->prepare($this->build());
0 ignored issues
show
Bug introduced by
It seems like build() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
60
61
        if ($stmt->execute($this->getValues())) {
0 ignored issues
show
Bug introduced by
It seems like getValues() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
62
            $this->rowCount = $stmt->rowCount();
63
64
            return $stmt;
65
        } else {
66
            return false;
67
        }
68
    }
69
70
    /**
71
     * Returns the number of rows affected by the last executed statement.
72
     *
73
     * @return int
74
     */
75
    public function rowCount()
76
    {
77
        return $this->rowCount;
78
    }
79
80
    /**
81
     * Starts a transaction.
82
     *
83
     * @return bool
84
     */
85
    public function beginTransaction()
86
    {
87
        return $this->pdo->beginTransaction();
88
    }
89
90
    /**
91
     * Commits the transaction.
92
     *
93
     * @return bool
94
     */
95
    public function commit()
96
    {
97
        return $this->pdo->commit();
98
    }
99
100
    /**
101
     * Rolls back the transaction.
102
     *
103
     * @return bool
104
     */
105
    public function rollBack()
106
    {
107
        return $this->pdo->rollBack();
108
    }
109
110
    /**
111
     * Checks if the query is in a transaction.
112
     *
113
     * @return bool
114
     */
115
    public function inTransaction()
116
    {
117
        return $this->pdo->inTransaction();
118
    }
119
}
120