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
|
|
|
public function getTwitterByTweetId($tweetId) |
23
|
|
|
{ |
24
|
|
|
$query = " |
25
|
|
|
SELECT `id`, `tweet_id`, `datetime`, `metadata` |
26
|
|
|
FROM `jpemeric_stream`.`twitter` |
27
|
|
|
WHERE `tweet_id` = :tweet_id |
28
|
|
|
LIMIT 1"; |
29
|
|
|
|
30
|
|
|
$bindings = [ |
31
|
|
|
'tweet_id' => $tweetId, |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
return $this |
35
|
|
|
->connections |
36
|
|
|
->getRead() |
37
|
|
|
->fetchOne($query, $bindings); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function insertTweet($tweetId, DateTime $datetime, array $metadata) |
41
|
|
|
{ |
42
|
|
|
$query = " |
43
|
|
|
INSERT INTO `jpemeric_stream`.`twitter` |
44
|
|
|
(`tweet_id`, `datetime`, `metadata`) |
45
|
|
|
VALUES |
46
|
|
|
(:tweet_id, :datetime, :metadata)"; |
47
|
|
|
|
48
|
|
|
$bindings = [ |
49
|
|
|
'tweet_id' => $tweetId, |
50
|
|
|
'datetime' => $datetime->format('Y-m-d H:i:s'), |
51
|
|
|
'metadata' => json_encode($metadata), |
52
|
|
|
]; |
53
|
|
|
|
54
|
|
|
return $this |
55
|
|
|
->connections |
56
|
|
|
->getWrite() |
57
|
|
|
->perform($query, $bindings); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function updateTweetMetadata($tweetId, array $metadata) |
61
|
|
|
{ |
62
|
|
|
$query = " |
63
|
|
|
UPDATE `jpemeric_stream`.`twitter` |
64
|
|
|
SET `metadata` = :metadata |
65
|
|
|
WHERE `tweet_id` = :tweet_id"; |
66
|
|
|
|
67
|
|
|
$bindings = [ |
68
|
|
|
'metadata' => json_encode($metadata), |
69
|
|
|
'tweet_id' => $tweetId, |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
return $this |
73
|
|
|
->connections |
74
|
|
|
->getWrite() |
75
|
|
|
->perform($query, $bindings); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|