Completed
Branch develop (bcdff2)
by Michael
04:41 queued 01:33
created

AbstractActiveRecord::read()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 16
ccs 0
cts 12
cp 0
rs 9.4285
cc 2
eloc 9
nc 2
nop 1
crap 6
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
		// TODO enable PDO exceptions?
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
37
	}
38
39
	/**
40
	 * {@inheritdoc}
41
	 */
42
	public function create()
43
	{
44
		$pdoStatement = $this->pdo->prepare($this->getCreateQuery());
45
		$pdoStatement->execute($this->getActiveRecordData());
46
47
		$this->id = intval($this->pdo->lastInsertId());
48
		return $this;
49
	}
50
51
	/**
52
	 * Returns the create query.
53
	 *
54
	 * @return string the create query.
55
	 */
56 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...
57
	{
58
		$columns = array_keys($this->getActiveRecordData());
59
		$values = [];
60
61
		foreach ($columns as $key => $value) {
62
			$values[] = ':' . $value;
63
		}
64
65
		return sprintf('INSERT INTO %s (%s) VALUES (%s)', $this->getActiveRecordName(), implode(', ', $columns), implode(', ', $values));
66
	}
67
68
	/**
69
	 * {@inheritdoc}
70
	 */
71
	public function read($id)
72
	{
73
		$data = $this->getActiveRecordData();
74
75
		$pdoStatement = $this->pdo->prepare($this->getReadQuery());
76
		$pdoStatement->execute(['id' => $id]);
77
		$result = $pdoStatement->fetch(\PDO::FETCH_ASSOC);
78
79
		$this->id = $id;
80
81
		foreach ($data as $key => &$value) {
82
			$value = $result[$key];
83
		}
84
85
		return $this;
86
	}
87
88
	/**
89
	 * Returns the read query.
90
	 *
91
	 * @return string the read query.
92
	 */
93
	private function getReadQuery()
94
	{
95
		return sprintf('SELECT * FROM `%s` WHERE `id` = :id', $this->getActiveRecordName());
96
	}
97
98
	/**
99
	 * {@inheritdoc}
100
	 */
101
	public function update()
102
	{
103
		$pdoStatement = $this->pdo->prepare($this->getUpdateQuery());
104
		$pdoStatement->execute(['id' => $this->id] + $this->getActiveRecordData());
105
106
		return $this;
107
	}
108
109
	/**
110
	 * Returns the update query.
111
	 *
112
	 * @return string the update query.
113
	 */
114 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...
115
	{
116
		$values = [];
117
118
		foreach (array_keys($this->getActiveRecordData()) as $key => $value) {
119
			$values[] = $value . ' = :' . $value;
120
		}
121
122
		return sprintf('UPDATE %s SET %s WHERE `id` = :id', $this->getActiveRecordName(), implode(', ', $values));
123
	}
124
125
	/**
126
	 * {@inheritdoc}
127
	 */
128
	public function delete()
129
	{
130
		$pdoStatement = $this->pdo->prepare($this->getDeleteQuery());
131
		$pdoStatement->execute(['id' => $this->id]);
132
133
		$this->id = null;
134
		return $this;
135
	}
136
137
	/**
138
	 * Returns the delete query.
139
	 *
140
	 * @return string the delete query.
141
	 */
142
	private function getDeleteQuery()
143
	{
144
		return sprintf('SELECT * FROM `%s` WHERE `id` = :id', $this->getActiveRecordName());
145
	}
146
147
	/**
148
	 * {@inheritdoc}
149
	 */
150
	public function exists()
151
	{
152
		return $this->id !== null;
153
	}
154
155
	/**
156
	 * Returns the PDO.
157
	 *
158
	 * @return PDO the PDO.
0 ignored issues
show
Documentation introduced by
Should the return type not be \PDO?

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.

Loading history...
159
	 */
160
	public function getPdo()
161
	{
162
		return $this->pdo;
163
	}
164
165
	/**
166
	 * Returns the ID.
167
	 *
168
	 * @return null|int The ID.
169
	 */
170
	public function getId()
171
	{
172
		return $this->id;
173
	}
174
175
	/**
176
	 * Returns the active record name.
177
	 *
178
	 * @return string the active record name.
179
	 */
180
	abstract protected function getActiveRecordName();
181
182
	/**
183
	 * Returns the active record data.
184
	 *
185
	 * @return array the active record data.
186
	 */
187
	abstract protected function getActiveRecordData();
188
}
189