1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jacobemerick\Web\Domain\Stream\Activity; |
4
|
|
|
|
5
|
|
|
use Aura\Sql\ConnectionLocator; |
6
|
|
|
use DateTime; |
7
|
|
|
|
8
|
|
|
class MysqlActivityRepository implements ActivityRepositoryInterface |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** @var ConnectionLocator */ |
12
|
|
|
protected $connections; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @param ConnectonLocator $connections |
16
|
|
|
*/ |
17
|
|
|
public function __construct(ConnectionLocator $connections) |
18
|
|
|
{ |
19
|
|
|
$this->connections = $connections; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param integer $id |
24
|
|
|
* |
25
|
|
|
* @return array|false |
26
|
|
|
*/ |
27
|
|
|
public function getActivityById($id) |
28
|
|
|
{ |
29
|
|
|
$query = " |
30
|
|
|
SELECT * |
31
|
|
|
FROM `jpemeric_stream`.`activity` |
32
|
|
|
WHERE `id` = :id |
33
|
|
|
LIMIT 1"; |
34
|
|
|
$bindings = [ |
35
|
|
|
'id' => $id, |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
return $this |
39
|
|
|
->connections |
40
|
|
|
->getRead() |
41
|
|
|
->fetchOne($query, $bindings); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getActivityByTypeId($type, $typeId) |
45
|
|
|
{ |
46
|
|
|
$query = " |
47
|
|
|
SELECT * |
48
|
|
|
FROM `jpemeric_stream`.`activity` |
49
|
|
|
WHERE `type` = :type && `type_id` = :type_id |
50
|
|
|
LIMIT 1"; |
51
|
|
|
$bindings = [ |
52
|
|
|
'type' => $type, |
53
|
|
|
'type_id' => $typeId, |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
return $this |
57
|
|
|
->connections |
58
|
|
|
->getRead() |
59
|
|
|
->fetchOne($query, $bindings); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
View Code Duplication |
public function getActivityLastUpdateByType($type) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$query = " |
65
|
|
|
SELECT * |
66
|
|
|
FROM `jpemeric_stream`.`activity` |
67
|
|
|
WHERE `type` = :type |
68
|
|
|
ORDER BY `updated_at` DESC |
69
|
|
|
LIMIT 1"; |
70
|
|
|
|
71
|
|
|
$bindings = [ |
72
|
|
|
'type' => $type, |
73
|
|
|
]; |
74
|
|
|
|
75
|
|
|
return $this |
76
|
|
|
->connections |
77
|
|
|
->getRead() |
78
|
|
|
->fetchOne($query, $bindings); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param integer $limit |
83
|
|
|
* @param integer $offset |
84
|
|
|
* |
85
|
|
|
* @return array|false |
86
|
|
|
*/ |
87
|
|
|
public function getActivities($limit = null, $offset = 0) |
88
|
|
|
{ |
89
|
|
|
$query = " |
90
|
|
|
SELECT * |
91
|
|
|
FROM `jpemeric_stream`.`activity` |
92
|
|
|
ORDER BY `datetime` DESC"; |
93
|
|
|
if (!is_null($limit)) { |
94
|
|
|
$query .= " |
95
|
|
|
LIMIT {$offset}, {$limit}"; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this |
99
|
|
|
->connections |
100
|
|
|
->getRead() |
101
|
|
|
->fetchAll($query); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function getActivitiesCount() |
105
|
|
|
{ |
106
|
|
|
$query = " |
107
|
|
|
SELECT COUNT(1) AS `count` |
108
|
|
|
FROM `jpemeric_stream`.`activity`"; |
109
|
|
|
|
110
|
|
|
return $this |
111
|
|
|
->connections |
112
|
|
|
->getRead() |
113
|
|
|
->fetchValue($query); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function getActivitiesByType($type, $limit = null, $offset = 0) |
117
|
|
|
{ |
118
|
|
|
$query = " |
119
|
|
|
SELECT * |
120
|
|
|
FROM `jpemeric_stream`.`activity` |
121
|
|
|
WHERE `type` = :type |
122
|
|
|
ORDER BY `datetime` DESC"; |
123
|
|
|
if (!is_null($limit)) { |
124
|
|
|
$query .= " |
125
|
|
|
LIMIT {$offset}, {$limit}"; |
126
|
|
|
} |
127
|
|
|
$bindings = [ |
128
|
|
|
'type' => $type, |
129
|
|
|
]; |
130
|
|
|
|
131
|
|
|
return $this |
132
|
|
|
->connections |
133
|
|
|
->getRead() |
134
|
|
|
->fetchAll($query, $bindings); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function getActivitiesByTypeCount($type) |
138
|
|
|
{ |
139
|
|
|
$query = " |
140
|
|
|
SELECT COUNT(1) AS `count` |
141
|
|
|
FROM `jpemeric_stream`.`activity` |
142
|
|
|
WHERE `type` = :type"; |
143
|
|
|
$bindings = [ |
144
|
|
|
'type' => $type, |
145
|
|
|
]; |
146
|
|
|
|
147
|
|
|
return $this |
148
|
|
|
->connections |
149
|
|
|
->getRead() |
150
|
|
|
->fetchValue($query, $bindings); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function insertActivity( |
154
|
|
|
$message, |
155
|
|
|
$messageLong, |
156
|
|
|
DateTime $datetime, |
157
|
|
|
array $metadata, |
158
|
|
|
$type, |
159
|
|
|
$typeId |
160
|
|
|
) { |
161
|
|
|
$query = " |
162
|
|
|
INSERT INTO `jpemeric_stream`.`activity` |
163
|
|
|
(`message`, `message_long`, `datetime`, `metadata`, `type`, `type_id`) |
164
|
|
|
VALUES |
165
|
|
|
(:message, :message_long, :datetime, :metadata, :type, :type_id)"; |
166
|
|
|
|
167
|
|
|
$bindings = [ |
168
|
|
|
'message' => $message, |
169
|
|
|
'message_long' => $messageLong, |
170
|
|
|
'datetime' => $datetime->format('Y-m-d H:i:s'), |
171
|
|
|
'metadata' => json_encode($metadata), |
172
|
|
|
'type' => $type, |
173
|
|
|
'type_id' => $typeId, |
174
|
|
|
]; |
175
|
|
|
|
176
|
|
|
return $this |
177
|
|
|
->connections |
178
|
|
|
->getWrite() |
179
|
|
|
->perform($query, $bindings); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
View Code Duplication |
public function updateActivityMetadata($activityId, array $metadata) |
|
|
|
|
183
|
|
|
{ |
184
|
|
|
$query = " |
185
|
|
|
UPDATE `jpemeric_stream`.`activity` |
186
|
|
|
SET `metadata` = :metadata |
187
|
|
|
WHERE `id` = :id"; |
188
|
|
|
|
189
|
|
|
$bindings = [ |
190
|
|
|
'metadata' => json_encode($metadata), |
191
|
|
|
'id' => $activityId, |
192
|
|
|
]; |
193
|
|
|
|
194
|
|
|
return $this |
195
|
|
|
->connections |
196
|
|
|
->getWrite() |
197
|
|
|
->perform($query, $bindings); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
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.