Completed
Push — master ( 7cfad9...ca95cf )
by Jacob
03:19
created

MysqlGithubRepository::getGithubsUpdatedSince()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 14
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 14
loc 14
rs 9.4286
cc 1
eloc 8
nc 1
nop 1
1
<?php
2
3
namespace Jacobemerick\Web\Domain\Stream\Github;
4
5
use Aura\Sql\ConnectionLocator;
6
use DateTime;
7
8 View Code Duplication
class MysqlGithubRepository implements GithubRepositoryInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

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.

Loading history...
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 $limit
24
     * @param integer $offset
25
     *
26
     * @return array|false
27
     */
28
    public function getEvents($limit = null, $offset = 0)
29
    {
30
        $query = "
31
            SELECT `id`, `event_id`, `datetime`
32
            FROM `jpemeric_stream`.`github`
33
            ORDER BY `datetime` DESC";
34
        if (!is_null($limit)) {
35
            $query .= "
36
            LIMIT {$offset}, {$limit}";
37
        }
38
39
        return $this
40
            ->connections
41
            ->getRead()
42
            ->fetchAll($query);
43
    }
44
45
    /**
46
     * @param integer $eventId
47
     *
48
     * @return array|false
49
     */
50
    public function getEventByEventId($eventId)
51
    {
52
        $query = "
53
            SELECT *
54
            FROM `jpemeric_stream`.`github`
55
            WHERE `event_id` = :event_id
56
            LIMIT 1";
57
58
        $bindings = [
59
            'event_id' => $eventId,
60
        ];
61
62
        return $this
63
            ->connections
64
            ->getRead()
65
            ->fetchOne($query, $bindings);
66
    }
67
68
    public function getGithubsUpdatedSince(DateTime $datetime)
69
    {
70
        $query = "
71
            SELECT *
72
            FROM `jpemeric_stream`.`github`
73
            WHERE `updated_at` >= :last_update";
74
        $bindings = [
75
            'last_update' => $datetime->format('Y-m-d H:i:s'),
76
        ];
77
        return $this
78
            ->connections
79
            ->getRead()
80
            ->fetchAll($query, $bindings);
81
    }
82
83
    /**
84
     * @param integer  $eventId
85
     * @param string   $eventType
86
     * @param DateTime $datetime
87
     * @param array    $metadata
88
     *
89
     * @return
90
     */
91
    public function insertEvent($eventId, $eventType, DateTime $datetime, array $metadata)
92
    {
93
        $query = "
94
            INSERT INTO `jpemeric_stream`.`github`
95
                (`event_id`, `type`, `datetime`, `metadata`)
96
            VALUES
97
                (:event_id, :event_type, :datetime, :metadata)";
98
99
        $bindings = [
100
            'event_id' => $eventId,
101
            'event_type' => $eventType,
102
            'datetime' => $datetime->format('Y-m-d H:i:s'),
103
            'metadata' => json_encode($metadata),
104
        ];
105
106
        return $this
107
            ->connections
108
            ->getWrite()
109
            ->perform($query, $bindings);
110
    }
111
}
112