1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jacobemerick\Web\Domain\Stream\Twitter; |
4
|
|
|
|
5
|
|
|
use Aura\Sql\ConnectionLocator; |
6
|
|
|
use DateTime; |
7
|
|
|
|
8
|
|
|
class MysqlTwitterRepository implements TwitterRepositoryInterface |
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 getTwitterById($id) |
28
|
|
|
{ |
29
|
|
|
$query = " |
30
|
|
|
SELECT * |
31
|
|
|
FROM `jpemeric_stream`.`twitter` |
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 getTwitterByTweetId($tweetId) |
45
|
|
|
{ |
46
|
|
|
$query = " |
47
|
|
|
SELECT `id`, `tweet_id`, `datetime`, `metadata` |
48
|
|
|
FROM `jpemeric_stream`.`twitter2` |
49
|
|
|
WHERE `tweet_id` = :tweet_id |
50
|
|
|
LIMIT 1"; |
51
|
|
|
|
52
|
|
|
$bindings = [ |
53
|
|
|
'tweet_id' => $tweetId, |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
return $this |
57
|
|
|
->connections |
58
|
|
|
->getRead() |
59
|
|
|
->fetchOne($query, $bindings); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param DateTime $date |
64
|
|
|
* @param string $text |
65
|
|
|
* |
66
|
|
|
* @return array|false |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
public function getTwitterByFields(DateTime $date, $text) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$query = " |
71
|
|
|
SELECT * |
72
|
|
|
FROM `jpemeric_stream`.`twitter` |
73
|
|
|
WHERE `date` = :date AND `text` = :text |
74
|
|
|
LIMIT 1"; |
75
|
|
|
$bindings = [ |
76
|
|
|
'date' => $date->format('Y-m-d H:i:s'), |
77
|
|
|
'text' => $text, |
78
|
|
|
]; |
79
|
|
|
|
80
|
|
|
return $this |
81
|
|
|
->connections |
82
|
|
|
->getRead() |
83
|
|
|
->fetchOne($query, $bindings); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return array|false |
88
|
|
|
*/ |
89
|
|
|
public function getUnmappedTwitters() |
90
|
|
|
{ |
91
|
|
|
$query = " |
92
|
|
|
SELECT `id`, `date` |
93
|
|
|
FROM `jpemeric_stream`.`twitter` |
94
|
|
|
LEFT JOIN `jpemeric_stream`.`post` |
95
|
|
|
ON `post`.`type_id` = `twitter`.`id` AND `post`.`id` IS NULL"; |
96
|
|
|
|
97
|
|
|
return $this |
98
|
|
|
->connections |
99
|
|
|
->getRead() |
100
|
|
|
->fetchAll($query); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function getTwittersUpdatedSince(DateTime $datetime) |
104
|
|
|
{ |
105
|
|
|
$query = " |
106
|
|
|
SELECT * |
107
|
|
|
FROM `jpemeric_stream`.`twitter2` |
108
|
|
|
WHERE `updated_at` >= :last_update"; |
109
|
|
|
|
110
|
|
|
$bindings = [ |
111
|
|
|
'last_update' => $datetime->format('Y-m-d H:i:s'), |
112
|
|
|
]; |
113
|
|
|
|
114
|
|
|
return $this |
115
|
|
|
->connections |
116
|
|
|
->getRead() |
117
|
|
|
->fetchAll($query, $bindings); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function insertTweet($tweetId, DateTime $datetime, array $metadata) |
121
|
|
|
{ |
122
|
|
|
$query = " |
123
|
|
|
INSERT INTO `jpemeric_stream`.`twitter2` |
124
|
|
|
(`tweet_id`, `datetime`, `metadata`) |
125
|
|
|
VALUES |
126
|
|
|
(:tweet_id, :datetime, :metadata)"; |
127
|
|
|
|
128
|
|
|
$bindings = [ |
129
|
|
|
'tweet_id' => $tweetId, |
130
|
|
|
'datetime' => $datetime->format('Y-m-d H:i:s'), |
131
|
|
|
'metadata' => json_encode($metadata), |
132
|
|
|
]; |
133
|
|
|
|
134
|
|
|
return $this |
135
|
|
|
->connections |
136
|
|
|
->getWrite() |
137
|
|
|
->perform($query, $bindings); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function updateTweetMetadata($tweetId, array $metadata) |
141
|
|
|
{ |
142
|
|
|
$query = " |
143
|
|
|
UPDATE `jpemeric_stream`.`twitter2` |
144
|
|
|
SET `metadata` = :metadata |
145
|
|
|
WHERE `tweet_id` = :tweet_id"; |
146
|
|
|
|
147
|
|
|
$bindings = [ |
148
|
|
|
'metadata' => json_encode($metadata), |
149
|
|
|
'tweet_id' => $tweetId, |
150
|
|
|
]; |
151
|
|
|
|
152
|
|
|
return $this |
153
|
|
|
->connections |
154
|
|
|
->getWrite() |
155
|
|
|
->perform($query, $bindings); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
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.