Completed
Push — master ( 8817c3...c1b66d )
by Jacob
03:14
created

MysqlTwitterRepository::insertTweet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 19
rs 9.4285
cc 1
eloc 10
nc 1
nop 3
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