1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jacobemerick\Web\Domain\Stream\DailyMile; |
4
|
|
|
|
5
|
|
|
use Aura\Sql\ConnectionLocator; |
6
|
|
|
use DateTime; |
7
|
|
|
|
8
|
|
View Code Duplication |
class MysqlDailyMileRepository implements DailyMileRepositoryInterface |
|
|
|
|
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 getEntries($limit = null, $offset = 0) |
29
|
|
|
{ |
30
|
|
|
$query = " |
31
|
|
|
SELECT `id`, `entry_id`, `datetime` |
32
|
|
|
FROM `jpemeric_stream`.`dailymile` |
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 $entryId |
47
|
|
|
* |
48
|
|
|
* @return array|false |
49
|
|
|
*/ |
50
|
|
|
public function getEntryByEntryId($entryId) |
51
|
|
|
{ |
52
|
|
|
$query = " |
53
|
|
|
SELECT * |
54
|
|
|
FROM `jpemeric_stream`.`dailymile` |
55
|
|
|
WHERE `entry_id` = :entry_id |
56
|
|
|
LIMIT 1"; |
57
|
|
|
|
58
|
|
|
$bindings = [ |
59
|
|
|
'entry_id' => $entryId, |
60
|
|
|
]; |
61
|
|
|
|
62
|
|
|
return $this |
63
|
|
|
->connections |
64
|
|
|
->getRead() |
65
|
|
|
->fetchOne($query, $bindings); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getDailyMilesUpdatedSince(DateTime $datetime) |
69
|
|
|
{ |
70
|
|
|
$query = " |
71
|
|
|
SELECT * |
72
|
|
|
FROM `jpemeric_stream`.`dailymile` |
73
|
|
|
WHERE `updated_at` >= :last_update"; |
74
|
|
|
|
75
|
|
|
$bindings = [ |
76
|
|
|
'last_update' => $datetime->format('Y-m-d H:i:s'), |
77
|
|
|
]; |
78
|
|
|
|
79
|
|
|
return $this |
80
|
|
|
->connections |
81
|
|
|
->getRead() |
82
|
|
|
->fetchAll($query, $bindings); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param integer $entryId |
87
|
|
|
* @param string $entryType |
88
|
|
|
* @param DateTime $datetime |
89
|
|
|
* @param array $metadata |
90
|
|
|
* |
91
|
|
|
* @return |
92
|
|
|
*/ |
93
|
|
|
public function insertEntry($entryId, $entryType, DateTime $datetime, array $metadata) |
94
|
|
|
{ |
95
|
|
|
$query = " |
96
|
|
|
INSERT INTO `jpemeric_stream`.`dailymile` |
97
|
|
|
(`entry_id`, `type`, `datetime`, `metadata`) |
98
|
|
|
VALUES |
99
|
|
|
(:entry_id, :entry_type, :datetime, :metadata)"; |
100
|
|
|
|
101
|
|
|
$bindings = [ |
102
|
|
|
'entry_id' => $entryId, |
103
|
|
|
'entry_type' => $entryType, |
104
|
|
|
'datetime' => $datetime->format('Y-m-d H:i:s'), |
105
|
|
|
'metadata' => json_encode($metadata), |
106
|
|
|
]; |
107
|
|
|
|
108
|
|
|
return $this |
109
|
|
|
->connections |
110
|
|
|
->getWrite() |
111
|
|
|
->perform($query, $bindings); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
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.