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

MysqlTwitterRepository   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 7
Bugs 1 Features 3
Metric Value
wmc 4
c 7
b 1
f 3
lcom 1
cbo 2
dl 0
loc 70
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getTwitterByTweetId() 0 17 1
A insertTweet() 0 19 1
A updateTweetMetadata() 0 17 1
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