1
|
|
|
<?php |
2
|
|
|
/** EntriesBinding */ |
3
|
|
|
|
4
|
|
|
namespace Battis\SharedLogs\Database\Bindings; |
5
|
|
|
|
6
|
|
|
use Battis\SharedLogs\Database\Binding; |
7
|
|
|
use Battis\SharedLogs\Database\Bindings\Traits\EntriesBindingTrait; |
8
|
|
|
use Battis\SharedLogs\Database\Bindings\Traits\LogsBindingTrait; |
9
|
|
|
use Battis\SharedLogs\Database\Bindings\Traits\UsersBindingTrait; |
10
|
|
|
use Battis\SharedLogs\Exceptions\BindingException; |
11
|
|
|
use Battis\SharedLogs\Objects\Entry; |
12
|
|
|
use Battis\SharedLogs\Objects\Log; |
13
|
|
|
use Battis\SharedLogs\Objects\User; |
14
|
|
|
use PDO; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* A binding between `Entry` objects and the `entries` database table |
18
|
|
|
* |
19
|
|
|
* @author Seth Battis <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class EntriesBinding extends Binding |
22
|
|
|
{ |
23
|
|
|
use EntriesBindingTrait, LogsBindingTrait, UsersBindingTrait; |
24
|
|
|
|
25
|
|
|
const INCLUDE_USER = 'user'; |
26
|
|
|
const INCLUDE_LOG = 'log'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Constuct an entries binding from a database connector |
30
|
|
|
* |
31
|
|
|
* @param PDO $database |
32
|
|
|
* |
33
|
|
|
* @throws BindingException |
34
|
|
|
*/ |
35
|
|
|
public function __construct(PDO $database) |
36
|
|
|
{ |
37
|
|
|
parent::__construct($database, 'entries', Entry::class); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Retrieve an entry by ID |
42
|
|
|
* |
43
|
|
|
* By default, entries contain both log and user sub-objects. |
44
|
|
|
* |
45
|
|
|
* @param int|string $id |
46
|
|
|
* @param array $params |
47
|
|
|
* |
48
|
|
|
* @return Entry|null |
49
|
|
|
*/ |
50
|
|
|
public function get($id, $params = [self::INCLUDE => [self::INCLUDE_LOG, self::INCLUDE_USER]]) |
51
|
|
|
{ |
52
|
|
|
return parent::get($id, $params); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Instantiate an entry retrieved via `get()` |
57
|
|
|
* |
58
|
|
|
* @used-by EntriesBinding::instantiateListedObject() |
59
|
|
|
* |
60
|
|
|
* @param array $databaseRow |
61
|
|
|
* @param array $params |
62
|
|
|
* |
63
|
|
|
* @return Entry |
64
|
|
|
*/ |
65
|
|
|
protected function instantiateObject($databaseRow, $params) |
66
|
|
|
{ |
67
|
|
|
$log = Entry::SUPPRESS_LOG; |
68
|
|
|
$user = Entry::SUPPRESS_USER; |
69
|
|
|
if (!empty($params[self::INCLUDE]) && is_array($params[self::INCLUDE])) { |
70
|
|
View Code Duplication |
if (in_array(self::INCLUDE_LOG, $params[self::INCLUDE])) { |
|
|
|
|
71
|
|
|
$log = $this->logs()->get($databaseRow[Log::ID], [self::INCLUDE => []]); |
72
|
|
|
} |
73
|
|
View Code Duplication |
if (in_array(self::INCLUDE_USER, $params[self::INCLUDE])) { |
|
|
|
|
74
|
|
|
$user = $this->users()->get($databaseRow[User::ID], [self::INCLUDE => []]); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
return $this->object($databaseRow, $log, $user); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Instantiate an entry retrieved via `all()` |
82
|
|
|
* |
83
|
|
|
* @param array $databaseRow |
84
|
|
|
* @param array $params |
85
|
|
|
* |
86
|
|
|
* @uses EntriesBinding::instantiateObject() |
87
|
|
|
* |
88
|
|
|
* @return Entry |
89
|
|
|
*/ |
90
|
|
|
protected function instantiateListedObject($databaseRow, $params) |
91
|
|
|
{ |
92
|
|
|
return $this->instantiateObject($databaseRow, $params); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Retrieve all entries in a specific log, by log ID |
97
|
|
|
* |
98
|
|
|
* By default, entries retrieved by this method contain user sub-object, but _not_ a log sub-object. |
99
|
|
|
* |
100
|
|
|
* @param integer|string $id Numeric log ID |
101
|
|
|
* @param array $params (Optional) Associative array of additional request parameters |
102
|
|
|
* @return Entry[] |
103
|
|
|
*/ |
104
|
|
View Code Duplication |
public function listByLog($id, $params = [self::INCLUDE => [self::INCLUDE_USER]]) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$statement = $this->database()->prepare(" |
107
|
|
|
SELECT * |
108
|
|
|
FROM `" . $this->databaseTable() . "` |
109
|
|
|
WHERE |
110
|
|
|
`" . Log::ID . "` = :id |
111
|
|
|
ORDER BY |
112
|
|
|
" . $this->listOrder() . " |
113
|
|
|
"); |
114
|
|
|
$list = []; |
115
|
|
|
if ($statement->execute(['id' => $id])) { |
116
|
|
|
while ($row = $statement->fetch()) { |
117
|
|
|
$list[] = $this->instantiateListedObject($row, $params); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
return $list; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Retrieve most recent entry of a specific log |
125
|
|
|
* |
126
|
|
|
* "Most recent" is detemermined by the listOrder() method, which defaults to descending creation date. By default, |
127
|
|
|
* entries retrieved by this method contain user sub-object, but _not_ a log sub-object. |
128
|
|
|
* |
129
|
|
|
* @param integer|string $id Numeric log ID |
130
|
|
|
* @param array $params (Optional) Associative array of additional request parameters |
131
|
|
|
* @return Entry|null |
132
|
|
|
*/ |
133
|
|
|
public function recent($id, $params = [self::INCLUDE => [self::INCLUDE_USER]]) |
134
|
|
|
{ |
135
|
|
|
$statement = $this->database()->prepare(" |
136
|
|
|
SELECT * |
137
|
|
|
FROM `" . $this->databaseTable() . "` |
138
|
|
|
WHERE |
139
|
|
|
`" . Log::ID . "` = :id |
140
|
|
|
ORDER BY |
141
|
|
|
" . $this->listOrder() . " |
142
|
|
|
LIMIT 1 |
143
|
|
|
"); |
144
|
|
|
if ($statement->execute(['id' => $id])) { |
145
|
|
|
return $this->instantiateObject($statement->fetch(), $params); |
146
|
|
|
} |
147
|
|
|
return null; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
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.