Completed
Push — master ( 2a1bc1...ce03f9 )
by Jacob
08:53
created

MysqlGoodreadRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Jacobemerick\Web\Domain\Stream\Goodread;
4
5
use Aura\Sql\ConnectionLocator;
6
use DateTime;
7
8
class MysqlGoodreadRepository implements GoodreadRepositoryInterface
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 getReviews($limit = null, $offset = 0)
29
    {
30
        $query = "
31
            SELECT `id`, `permalink`, `datetime`
32
            FROM `jpemeric_stream`.`goodread`
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
    public function getReviewByPermalink($permalink)
46
    {
47
        $query = "
48
            SELECT *
49
            FROM `jpemeric_stream`.`goodread`
50
            WHERE `permalink` = :permalink
51
            LIMIT 1";
52
53
        $bindings = [
54
            'permalink' => $permalink,
55
        ];
56
57
        return $this
58
            ->connections
59
            ->getRead()
60
            ->fetchOne($query, $bindings);
61
    }
62
63
    public function insertReview($permalink, $bookId, DateTime $datetime, array $metadata)
64
    {
65
        $query = "
66
            INSERT INTO `jpemeric_stream`.`goodread`
67
                (`permalink`, `book_id`, `datetime`, `metadata`)
68
            VALUES
69
                (:permalink, :book_id, :datetime, :metadata)";
70
71
        $bindings = [
72
            'permalink' => $permalink,
73
            'book_id' => $bookId,
74
            'datetime' => $datetime->format('Y-m-d H:i:s'),
75
            'metadata' => json_encode($metadata),
76
        ];
77
78
        return $this
79
            ->connections
80
            ->getWrite()
81
            ->perform($query, $bindings);
82
    }
83
}
84