1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jacobemerick\Web\Domain\Stream\Post; |
4
|
|
|
|
5
|
|
|
use Aura\Sql\ConnectionLocator; |
6
|
|
|
|
7
|
|
|
class MysqlPostRepository implements PostRepositoryInterface |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** @var ConnectionLocator */ |
11
|
|
|
protected $connections; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @param ConnectonLocator $connections |
15
|
|
|
*/ |
16
|
|
|
public function __construct(ConnectionLocator $connections) |
17
|
|
|
{ |
18
|
|
|
$this->connections = $connections; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param integer $id |
23
|
|
|
* |
24
|
|
|
* @return array|false |
25
|
|
|
*/ |
26
|
|
|
public function getPostById($id) |
27
|
|
|
{ |
28
|
|
|
$query = " |
29
|
|
|
SELECT * |
30
|
|
|
FROM `jpemeric_stream`.`post` |
31
|
|
|
WHERE `id` = :id |
32
|
|
|
LIMIT 1"; |
33
|
|
|
$bindings = [ |
34
|
|
|
'id' => $id, |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
return $this |
38
|
|
|
->connections |
39
|
|
|
->getRead() |
40
|
|
|
->fetchOne($query, $bindings); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $type |
45
|
|
|
* @param integer $type_id |
46
|
|
|
* |
47
|
|
|
* @return array|false |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
public function getPostByType($type, $type_id) |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$query = " |
52
|
|
|
SELECT * |
53
|
|
|
FROM `jpemeric_stream`.`post` |
54
|
|
|
WHERE `type` = :type && `type_id` = :type_id |
55
|
|
|
LIMIT 1"; |
56
|
|
|
$bindings = [ |
57
|
|
|
'type' => $type, |
58
|
|
|
'type_id' => $type_id, |
59
|
|
|
]; |
60
|
|
|
|
61
|
|
|
return $this |
62
|
|
|
->connections |
63
|
|
|
->getRead() |
64
|
|
|
->fetchOne($query, $bindings); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param integer $limit |
69
|
|
|
* @param integer $offset |
70
|
|
|
* |
71
|
|
|
* @return array|false |
72
|
|
|
*/ |
73
|
|
|
public function getPosts($limit = null, $offset = 0) |
74
|
|
|
{ |
75
|
|
|
$query = " |
76
|
|
|
SELECT * |
77
|
|
|
FROM `jpemeric_stream`.`post` |
78
|
|
|
ORDER BY `date` DESC"; |
79
|
|
|
if (!is_null($limit)) { |
80
|
|
|
$query .= " |
81
|
|
|
LIMIT {$offset}, {$limit}"; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this |
85
|
|
|
->connections |
86
|
|
|
->getRead() |
87
|
|
|
->fetchAll($query); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getPostsCount() |
91
|
|
|
{ |
92
|
|
|
$query = " |
93
|
|
|
SELECT COUNT(1) AS `count` |
94
|
|
|
FROM `jpemeric_stream`.`post`"; |
95
|
|
|
|
96
|
|
|
return $this |
97
|
|
|
->connections |
98
|
|
|
->getRead() |
99
|
|
|
->fetchValue($query); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
View Code Duplication |
public function getPostsByTag($tag, $limit = null, $offset = 0) |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$query = " |
105
|
|
|
SELECT * |
106
|
|
|
FROM `jpemeric_stream`.`post` |
107
|
|
|
WHERE `tag` = :tag |
108
|
|
|
ORDER BY `date` DESC"; |
109
|
|
|
if (!is_null($limit)) { |
110
|
|
|
$query .= " |
111
|
|
|
LIMIT {$offset}, {$limit}"; |
112
|
|
|
} |
113
|
|
|
$bindings = [ |
114
|
|
|
'tag' => $tag, |
115
|
|
|
]; |
116
|
|
|
|
117
|
|
|
return $this |
118
|
|
|
->connections |
119
|
|
|
->getRead() |
120
|
|
|
->fetchAll($query, $bindings); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function getPostsByTagCount($tag) |
124
|
|
|
{ |
125
|
|
|
$query = " |
126
|
|
|
SELECT COUNT(1) AS `count` |
127
|
|
|
FROM `jpemeric_stream`.`post` |
128
|
|
|
WHERE `tag` = :tag"; |
129
|
|
|
$bindings = [ |
130
|
|
|
'tag' => $tag, |
131
|
|
|
]; |
132
|
|
|
|
133
|
|
|
return $this |
134
|
|
|
->connections |
135
|
|
|
->getRead() |
136
|
|
|
->fetchAll($query, $bindings); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
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.