1
|
|
|
<?php |
2
|
|
|
/** EntriesBinding */ |
3
|
|
|
|
4
|
|
|
namespace Battis\SharedLogs\Database\Bindings; |
5
|
|
|
|
6
|
|
|
use Battis\SharedLogs\Database\AbstractBinding; |
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 AbstractBinding |
22
|
|
|
{ |
23
|
|
|
use EntriesBindingTrait, LogsBindingTrait, UsersBindingTrait; |
24
|
|
|
|
25
|
|
|
const SCOPE_COUNT = 'count'; |
26
|
|
|
|
27
|
|
|
const INCLUDE_USER = 'user'; |
28
|
|
|
const INCLUDE_LOG = 'log'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Constuct an entries binding from a database connector |
32
|
|
|
* |
33
|
|
|
* @param PDO $database |
34
|
|
|
* |
35
|
|
|
* @throws BindingException |
36
|
|
|
*/ |
37
|
|
|
public function __construct(PDO $database) |
38
|
|
|
{ |
39
|
|
|
parent::__construct($database, 'entries', Entry::class); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Retrieve an entry by ID |
44
|
|
|
* |
45
|
|
|
* By default, entries contain both log and user sub-objects. |
46
|
|
|
* |
47
|
|
|
* @param int|string $id |
48
|
|
|
* @param array $params |
49
|
|
|
* |
50
|
|
|
* @return Entry|null |
51
|
|
|
*/ |
52
|
|
|
public function get($id, $params = [self::SCOPE_INCLUDE => [self::INCLUDE_LOG, self::INCLUDE_USER]]) |
53
|
|
|
{ |
54
|
|
|
return parent::get($id, $params); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Instantiate an entry retrieved via `get()` |
59
|
|
|
* |
60
|
|
|
* @used-by EntriesBinding::instantiateListedObject() |
61
|
|
|
* |
62
|
|
|
* @param array $databaseRow |
63
|
|
|
* @param array $params |
64
|
|
|
* |
65
|
|
|
* @return Entry |
66
|
|
|
*/ |
67
|
|
|
protected function instantiateObject($databaseRow, $params) |
68
|
|
|
{ |
69
|
|
|
$log = Entry::SUPPRESS_LOG; |
70
|
|
|
$user = Entry::SUPPRESS_USER; |
71
|
|
View Code Duplication |
if (self::parameterValueExists($params,self::SCOPE_INCLUDE,self::INCLUDE_LOG)) { |
|
|
|
|
72
|
|
|
$params = self::consumeParameterValue($params,self::SCOPE_INCLUDE,self::INCLUDE_LOG); |
73
|
|
|
$params = self::consumeParameterValue($params,self::SCOPE_INCLUDE,LogsBinding::INCLUDE_ENTRIES); |
74
|
|
|
$log = $this->logs()->get($databaseRow[Log::ID], $params); |
75
|
|
|
} |
76
|
|
|
if (self::parameterValueExists($params, self::SCOPE_INCLUDE, self::INCLUDE_USER)) { |
77
|
|
|
$params = self::consumeParameterValue($params, self::SCOPE_INCLUDE,self::INCLUDE_USER); |
78
|
|
|
$user = $this->users()->get($databaseRow[User::ID], $params); |
79
|
|
|
} |
80
|
|
|
return $this->object($databaseRow, $log, $user); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Instantiate an entry retrieved via `all()` |
85
|
|
|
* |
86
|
|
|
* @param array $databaseRow |
87
|
|
|
* @param array $params |
88
|
|
|
* |
89
|
|
|
* @uses EntriesBinding::instantiateObject() |
90
|
|
|
* |
91
|
|
|
* @return Entry |
92
|
|
|
*/ |
93
|
|
|
protected function instantiateListedObject($databaseRow, $params) |
94
|
|
|
{ |
95
|
|
|
return $this->instantiateObject($databaseRow, $params); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Retrieve all entries in a specific log, by log ID |
100
|
|
|
* |
101
|
|
|
* By default, entries retrieved by this method contain user sub-object, but _not_ a log sub-object. |
102
|
|
|
* |
103
|
|
|
* @param integer|string $id Numeric log ID |
104
|
|
|
* @param array $params (Optional) Associative array of additional request parameters |
105
|
|
|
* @return Entry[] |
106
|
|
|
*/ |
107
|
|
|
public function listByLog($id, $params = [self::SCOPE_INCLUDE => [self::INCLUDE_USER]]) |
108
|
|
|
{ |
109
|
|
|
$statement = $this->database()->prepare(" |
110
|
|
|
SELECT * |
111
|
|
|
FROM `" . $this->databaseTable() . "` |
112
|
|
|
WHERE |
113
|
|
|
`" . Log::ID . "` = :id |
114
|
|
|
ORDER BY |
115
|
|
|
" . $this->listOrder() . " |
116
|
|
|
" . (($count = self::getScope($params, self::SCOPE_COUNT) !== null) ? "LIMIT $count" : "") . " |
117
|
|
|
"); |
118
|
|
|
$list = []; |
119
|
|
|
if ($statement->execute(['id' => $id])) { |
120
|
|
|
while ($row = $statement->fetch()) { |
121
|
|
|
$list[] = $this->instantiateListedObject($row, $params); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
return $list; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
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.