Completed
Push — master ( df2561...efdf8d )
by Michael
03:00
created

AbstractActiveRecord::getPdo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * This file is part of the miBadger package.
5
 *
6
 * @author Michael Webbers <[email protected]>
7
 * @license http://opensource.org/licenses/Apache-2.0 Apache v2 License
8
 * @version 1.0.0
9
 */
10
11
namespace miBadger\ActiveRecord;
12
13
/**
14
 * The abstract active record class.
15
 *
16
 * @since 1.0.0
17
 */
18
abstract class AbstractActiveRecord implements ActiveRecordInterface
19
{
20
	/** @var \PDO The PDO object. */
21
	private $pdo;
22
23
	/** @var null|int The ID. */
24
	private $id;
25
26
	/**
27
	 * Construct an abstract pdo active record with the given pdo.
28
	 *
29
	 * @param \PDO $pdo
30
	 */
31
	public function __construct(\PDO $pdo)
32
	{
33
		$this->pdo = $pdo;
34
		$this->id = null;
35
	}
36
37
	/**
38
	 * {@inheritdoc}
39
	 */
40
	public function create()
41
	{
42
		$pdoStatement = $this->pdo->prepare($this->getCreateQuery());
43
		$pdoStatement->execute($this->getActiveRecordData());
44
45
		$this->id = intval($this->pdo->lastInsertId());
46
		return $this;
47
	}
48
49
	/**
50
	 * Returns the create query.
51
	 *
52
	 * @return string the create query.
53
	 */
54 View Code Duplication
	private function getCreateQuery()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
	{
56
		$columns = array_keys($this->getActiveRecordData());
57
		$values = [];
58
59
		foreach ($columns as $key => $value) {
60
			$values[] = ':' . $value;
61
		}
62
63
		return sprintf('INSERT INTO %s (%s) VALUES (%s)', $this->getActiveRecordName(), implode(', ', $columns), implode(', ', $values));
64
	}
65
66
	/**
67
	 * {@inheritdoc}
68
	 */
69
	public function read($id)
70
	{
71
		$data = $this->getActiveRecordData();
72
73
		$pdoStatement = $this->pdo->prepare($this->getReadQuery());
74
		$pdoStatement->execute(['id' => $id]);
75
		$result = $pdoStatement->fetch(\PDO::FETCH_ASSOC);
76
77
		$this->id = $id;
78
79
		foreach ($data as $key => &$value) {
80
			$value = $result[$key];
81
		}
82
83
		return $this;
84
	}
85
86
	/**
87
	 * Returns the read query.
88
	 *
89
	 * @return string the read query.
90
	 */
91
	private function getReadQuery()
92
	{
93
		return sprintf('SELECT * FROM `%s` WHERE `id` = :id', $this->getActiveRecordName());
94
	}
95
96
	/**
97
	 * {@inheritdoc}
98
	 */
99
	public function update()
100
	{
101
		$pdoStatement = $this->pdo->prepare($this->getUpdateQuery());
102
		$pdoStatement->execute(['id' => $this->id] + $this->getActiveRecordData());
103
104
		return $this;
105
	}
106
107
	/**
108
	 * Returns the update query.
109
	 *
110
	 * @return string the update query.
111
	 */
112 View Code Duplication
	private function getUpdateQuery()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
	{
114
		$values = [];
115
116
		foreach (array_keys($this->getActiveRecordData()) as $key => $value) {
117
			$values[] = $value . ' = :' . $value;
118
		}
119
120
		return sprintf('UPDATE %s SET %s WHERE `id` = :id', $this->getActiveRecordName(), implode(', ', $values));
121
	}
122
123
	/**
124
	 * {@inheritdoc}
125
	 */
126
	public function delete()
127
	{
128
		$pdoStatement = $this->pdo->prepare($this->getDeleteQuery());
129
		$pdoStatement->execute(['id' => $this->id]);
130
131
		$this->id = null;
132
		return $this;
133
	}
134
135
	/**
136
	 * Returns the delete query.
137
	 *
138
	 * @return string the delete query.
139
	 */
140
	private function getDeleteQuery()
141
	{
142
		return sprintf('SELECT * FROM `%s` WHERE `id` = :id', $this->getActiveRecordName());
143
	}
144
145
	/**
146
	 * {@inheritdoc}
147
	 */
148
	public function exists()
149
	{
150
		return $this->id !== null;
151
	}
152
153
	/**
154
	 * Returns the PDO.
155
	 *
156
	 * @return \PDO the PDO.
157
	 */
158
	public function getPdo()
159
	{
160
		return $this->pdo;
161
	}
162
163
	/**
164
	 * Set the PDO.
165
	 *
166
	 * @param \PDO $pdo
167
	 * @return null
168
	 */
169
	protected function setPdo($pdo)
170
	{
171
		$this->pdo = $pdo;
172
	}
173
174
	/**
175
	 * Returns the ID.
176
	 *
177
	 * @return null|int The ID.
178
	 */
179
	public function getId()
180
	{
181
		return $this->id;
182
	}
183
184
	/**
185
	 * Set the ID.
186
	 *
187
	 * @param int $id
188
	 * @return null
189
	 */
190
	protected function setId($id)
191
	{
192
		$this->id = $id;
193
	}
194
195
	/**
196
	 * Returns the active record name.
197
	 *
198
	 * @return string the active record name.
199
	 */
200
	abstract protected function getActiveRecordName();
201
202
	/**
203
	 * Returns the active record data.
204
	 *
205
	 * @return array the active record data.
206
	 */
207
	abstract protected function getActiveRecordData();
208
}
209