1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jacobemerick\Web\Domain\Stream\Blog; |
4
|
|
|
|
5
|
|
|
use Aura\Sql\ConnectionLocator; |
6
|
|
|
use DateTime; |
7
|
|
|
|
8
|
|
|
class MysqlBlogRepository implements BlogRepositoryInterface |
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 $id |
24
|
|
|
* |
25
|
|
|
* @return array|false |
26
|
|
|
*/ |
27
|
|
|
public function getBlogById($id) |
28
|
|
|
{ |
29
|
|
|
$query = " |
30
|
|
|
SELECT * |
31
|
|
|
FROM `jpemeric_stream`.`blog` |
32
|
|
|
WHERE `id` = :id |
33
|
|
|
LIMIT 1"; |
34
|
|
|
$bindings = [ |
35
|
|
|
'id' => $id, |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
return $this |
39
|
|
|
->connections |
40
|
|
|
->getRead() |
41
|
|
|
->fetchOne($query, $bindings); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
View Code Duplication |
public function getBlogByPermalink($permalink) |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$query = " |
47
|
|
|
SELECT * |
48
|
|
|
FROM `jpemeric_stream`.`blog2` |
49
|
|
|
WHERE `permalink` = :permalink |
50
|
|
|
LIMIT 1"; |
51
|
|
|
$bindings = [ |
52
|
|
|
'permalink' => $permalink, |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
return $this |
56
|
|
|
->connections |
57
|
|
|
->getRead() |
58
|
|
|
->fetchOne($query, $bindings); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $title |
63
|
|
|
* |
64
|
|
|
* @return array|false |
65
|
|
|
*/ |
66
|
|
|
public function getBlogByTitle($title) |
67
|
|
|
{ |
68
|
|
|
$query = " |
69
|
|
|
SELECT * |
70
|
|
|
FROM `jpemeric_stream`.`blog` |
71
|
|
|
WHERE `title` = :title |
72
|
|
|
LIMIT 1"; |
73
|
|
|
$bindings = [ |
74
|
|
|
'title' => $title, |
75
|
|
|
]; |
76
|
|
|
|
77
|
|
|
return $this |
78
|
|
|
->connections |
79
|
|
|
->getRead() |
80
|
|
|
->fetchOne($query, $bindings); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getBlogs($limit = null, $offset = 0) |
84
|
|
|
{ |
85
|
|
|
$query = " |
86
|
|
|
SELECT `id`, `permalink`, `datetime` |
87
|
|
|
FROM `jpemeric_stream`.`blog2` |
88
|
|
|
ORDER BY `datetime` DESC"; |
89
|
|
|
if (!is_null($limit)) { |
90
|
|
|
$query .= " |
91
|
|
|
LIMIT {$limit}, {$offset}"; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this |
95
|
|
|
->connections |
96
|
|
|
->getRead() |
97
|
|
|
->fetchAll($query); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getBlogsUpdatedSince(DateTime $datetime) |
101
|
|
|
{ |
102
|
|
|
$query = " |
103
|
|
|
SELECT * |
104
|
|
|
FROM `jpemeric_stream`.`blog2` |
105
|
|
|
WHERE `updated_at` >= :last_update"; |
106
|
|
|
|
107
|
|
|
$bindings = [ |
108
|
|
|
'last_update' => $datetime->format('Y-m-d H:i:s'), |
109
|
|
|
]; |
110
|
|
|
|
111
|
|
|
return $this |
112
|
|
|
->connections |
113
|
|
|
->getRead() |
114
|
|
|
->fetchAll($query, $bindings); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
View Code Duplication |
public function insertBlog($permalink, DateTime $datetime, array $metadata) |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
$query = " |
120
|
|
|
INSERT INTO `jpemeric_stream`.`blog2` |
121
|
|
|
(`permalink`, `datetime`, `metadata`) |
122
|
|
|
VALUES |
123
|
|
|
(:permalink, :datetime, :metadata)"; |
124
|
|
|
|
125
|
|
|
$bindings = [ |
126
|
|
|
'permalink' => $permalink, |
127
|
|
|
'datetime' => $datetime->format('Y-m-d H:i:s'), |
128
|
|
|
'metadata' => json_encode($metadata), |
129
|
|
|
]; |
130
|
|
|
|
131
|
|
|
return $this |
132
|
|
|
->connections |
133
|
|
|
->getWrite() |
134
|
|
|
->perform($query, $bindings); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
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.