Completed
Push — master ( 989695...7cfad9 )
by Jacob
07:09
created

MysqlGoodreadRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 4
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 View Code Duplication
class MysqlGoodreadRepository implements GoodreadRepositoryInterface
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 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 getGoodreadsUpdatedSince(DateTime $datetime)
64
    {
65
        $query = "
66
            SELECT *
67
            FROM `jpemeric_stream`.`goodread`
68
            WHERE `updated_at` >= :last_update";
69
70
        $bindings = [
71
            'last_update' => $datetime->format('Y-m-d H:i:s'),
72
        ];
73
74
        return $this
75
            ->connections
76
            ->getRead()
77
            ->fetchAll($query, $bindings);
78
    }
79
80
    public function insertReview($permalink, $bookId, DateTime $datetime, array $metadata)
81
    {
82
        $query = "
83
            INSERT INTO `jpemeric_stream`.`goodread`
84
                (`permalink`, `book_id`, `datetime`, `metadata`)
85
            VALUES
86
                (:permalink, :book_id, :datetime, :metadata)";
87
88
        $bindings = [
89
            'permalink' => $permalink,
90
            'book_id' => $bookId,
91
            'datetime' => $datetime->format('Y-m-d H:i:s'),
92
            'metadata' => json_encode($metadata),
93
        ];
94
95
        return $this
96
            ->connections
97
            ->getWrite()
98
            ->perform($query, $bindings);
99
    }
100
}
101