1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2016-2016} Andreas Heigl<[email protected]> |
4
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
5
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
6
|
|
|
* in the Software without restriction, including without limitation the rights |
7
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
8
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
9
|
|
|
* furnished to do so, subject to the following conditions: |
10
|
|
|
* The above copyright notice and this permission notice shall be included in |
11
|
|
|
* all copies or substantial portions of the Software. |
12
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
13
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
14
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
15
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
16
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
17
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
18
|
|
|
* THE SOFTWARE. |
19
|
|
|
* |
20
|
|
|
* @author Andreas Heigl<[email protected]> |
21
|
|
|
* @copyright 2016-2016 Andreas Heigl |
22
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT-License |
23
|
|
|
* @version 0.0 |
24
|
|
|
* @since 19.01.2016 |
25
|
|
|
* @link http://github.com/heiglandreas/callingallpapers |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
namespace Callingallpapers\Api\PersistenceLayer; |
29
|
|
|
|
30
|
|
|
use Callingallpapers\Api\Entity\Cfp; |
31
|
|
|
use Monolog\Logger; |
32
|
|
|
|
33
|
|
|
class CfpPersistenceLayer |
34
|
|
|
{ |
35
|
|
|
protected $pdo; |
36
|
|
|
|
37
|
|
|
public function __construct(\PDO $pdo) |
38
|
|
|
{ |
39
|
|
|
$this->pdo = $pdo; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function insert(Cfp $cfp) |
43
|
|
|
{ |
44
|
|
|
try { |
45
|
|
|
$this->select($cfp->getHash()); |
46
|
|
|
$cfpExists = true; |
47
|
|
|
} catch (\UnexpectedValueException $e) { |
48
|
|
|
$cfpExists = false; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ($cfpExists) { |
52
|
|
|
throw new \UnexpectedValueException(sprintf( |
53
|
|
|
'There is already a CFP with URL "%1$s".', |
54
|
|
|
$cfp->getEventUri() |
55
|
|
|
), 400); |
56
|
|
|
} |
57
|
|
|
$statement = 'INSERT into `cfp`(`dateCfpStart`, `dateCfpEnd`, `dateEventStart`, `dateEventEnd`, `name`, `uri`, `hash`, `timezone`, `description`, `eventUri`, `iconUri`, `latitude`, `longitude`, `location`, `tags`, `lastUpdate`) ' . |
58
|
|
|
'VALUES (:dateCfpStart, :dateCfpEnd, :dateEventStart, :dateEventEnd, :name, :uri, :hash, :timezone, :description, :eventUri, :iconUri, :latitude, :longitude, :location, :tags, :lastUpdate);'; |
59
|
|
|
$statement = $this->pdo->prepare($statement); |
60
|
|
|
|
61
|
|
|
$values = [ |
62
|
|
|
'dateCfpStart' => $cfp->getDateCfpStart()->format('c'), |
63
|
|
|
'dateCfpEnd' => $cfp->getDateCfpEnd()->format('c'), |
64
|
|
|
'dateEventStart' => $cfp->getDateEventStart()->format('c'), |
65
|
|
|
'dateEventEnd' => $cfp->getDateEventEnd()->format('c'), |
66
|
|
|
'name' => $cfp->getName(), |
67
|
|
|
'uri' => $cfp->getUri(), |
68
|
|
|
'hash' => $cfp->getHash(), |
69
|
|
|
'timezone' => $cfp->getTimezone()->getName(), |
70
|
|
|
'description' => $cfp->getDescription(), |
71
|
|
|
'eventUri' => $cfp->getEventUri(), |
72
|
|
|
'iconUri' => $cfp->getIconUri(), |
73
|
|
|
'latitude' => $cfp->getLatitude(), |
74
|
|
|
'longitude' => $cfp->getLongitude(), |
75
|
|
|
'location' => $cfp->getLocation(), |
76
|
|
|
'tags' => implode(',', $cfp->getTags()), |
77
|
|
|
'lastUpdate' => (new \DateTime('now', new \DateTimezone('UTC')))->format('c'), |
78
|
|
|
]; |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
if ($statement->execute($values)) { |
82
|
|
|
return $values['hash']; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
throw new \UnexpectedValueException('The CfP could not be stored', 400); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function update(Cfp $cfp, $fetchHash) |
89
|
|
|
{ |
90
|
|
|
if (! $fetchHash) { |
91
|
|
|
throw new \UnexpectedValueException('No Hash given', 400); |
92
|
|
|
} |
93
|
|
|
$oldValues = $this->select($fetchHash); |
94
|
|
|
if ($oldValues->count() != 1) { |
95
|
|
|
throw new \UnexpectedValueException('There is no CFP with this URL.', 404); |
96
|
|
|
} |
97
|
|
|
$statementElements = []; |
98
|
|
|
$values = []; |
99
|
|
|
$oldValues = $oldValues->current(); |
100
|
|
|
$options = [ |
101
|
|
|
'dateCfpStart', |
102
|
|
|
'dateCfpEnd', |
103
|
|
|
'name', |
104
|
|
|
'uri', |
105
|
|
|
'hash', |
106
|
|
|
'timezone', |
107
|
|
|
'dateEventStart', |
108
|
|
|
'dateEventEnd', |
109
|
|
|
'description', |
110
|
|
|
'eventUri', |
111
|
|
|
'iconUri', |
112
|
|
|
'latitude', |
113
|
|
|
'longitude', |
114
|
|
|
'location', |
115
|
|
|
'tags', |
116
|
|
|
]; |
117
|
|
|
|
118
|
|
|
foreach ($options as $option) { |
119
|
|
|
$method = 'get' . $option; |
120
|
|
|
if ($cfp->$method() != $oldValues->$method()) { |
121
|
|
|
$statementElements[] = '`' . $option . '` = :' . $option; |
122
|
|
|
$values[$option] = $cfp->$method(); |
123
|
|
|
if ($values[$option] instanceof \DateTimeInterface) { |
124
|
|
|
$values[$option] = $values[$option]->format('c'); |
125
|
|
|
} |
126
|
|
|
if (is_array($values[$option])) { |
127
|
|
|
$values[$option] = implode(',', $values[$option]); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// No values to update, just, return |
133
|
|
|
if (empty($values)) { |
134
|
|
|
return $cfp->getHash(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$statement = 'UPDATE `cfp` SET ' |
138
|
|
|
. implode(',', $statementElements) . ',' |
139
|
|
|
. '`lastUpdate` = :lastUpdate ' |
140
|
|
|
. 'WHERE `hash` = :fetchHash'; |
141
|
|
|
$statement = $this->pdo->prepare($statement); |
142
|
|
|
$values['fetchHash'] = $fetchHash; |
143
|
|
|
$values['lastUpdate'] = (new \DateTime('now', new \DateTimezone('UTC')))->format('c'); |
144
|
|
|
|
145
|
|
|
if ($statement->execute($values)) { |
146
|
|
|
return $cfp->getHash(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
throw new \UnexpectedValueException('The CfP could not be updated', 400); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
|
153
|
|
|
public function select($hash = null) |
154
|
|
|
{ |
155
|
|
|
$statement = 'SELECT * FROM `cfp`'; |
156
|
|
|
$values = []; |
157
|
|
|
if ($hash !== null) { |
158
|
|
|
$statement .= ' WHERE `hash`= :hash'; |
159
|
|
|
$values['hash'] = $hash; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
|
164
|
|
|
$statement = $this->pdo->prepare($statement); |
165
|
|
|
|
166
|
|
|
$list = new \Callingallpapers\Api\Entity\CfpList(); |
167
|
|
|
$statement->execute($values); |
168
|
|
|
$content = $statement->fetchAll(); |
169
|
|
|
if (count($content) < 1) { |
170
|
|
|
throw new \UnexpectedValueException('No CFPs found', 404); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
View Code Duplication |
foreach ($content as $item) { |
|
|
|
|
174
|
|
|
$cfp = new \Callingallpapers\Api\Entity\Cfp(); |
175
|
|
|
$cfp->setName($item['name']); |
176
|
|
|
$cfp->setDateCfpEnd(new \DateTimeImmutable($item['dateCfpEnd'])); |
177
|
|
|
$cfp->setDateCfpStart(new \DateTimeImmutable($item['dateCfpStart'])); |
178
|
|
|
$cfp->setUri($item['uri']); |
179
|
|
|
$cfp->setTimezone(new \DateTimeZone($item['timezone'])); |
180
|
|
|
$cfp->setDateEventStart(new \DateTimeImmutable($item['dateEventStart'])); |
181
|
|
|
$cfp->setDateEventEnd(new \DateTimeImmutable($item['dateEventEnd'])); |
182
|
|
|
$cfp->setDescription($item['description']); |
183
|
|
|
$cfp->setEventUri($item['eventUri']); |
184
|
|
|
$cfp->setIconUri($item['iconUri']); |
185
|
|
|
$cfp->setLatitude($item['latitude']); |
186
|
|
|
$cfp->setLongitude($item['longitude']); |
187
|
|
|
$cfp->setLocation($item['location']); |
188
|
|
|
$cfp->setTags(explode(',', $item['tags'])); |
189
|
|
|
$cfp->setLastUpdated(new \DateTimeImmutable($item['lastUpdate'])); |
190
|
|
|
|
191
|
|
|
$list->add($cfp); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return $list; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function delete($hash) |
198
|
|
|
{ |
199
|
|
|
$statement = 'DELETE FROM `cfp` WHERE `hash` = :hash'; |
200
|
|
|
|
201
|
|
|
$statement = $this->pdo->prepare($statement); |
202
|
|
|
|
203
|
|
|
return $statement->execute(['hash' => $hash]); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function getCurrent() |
207
|
|
|
{ |
208
|
|
|
$statement = 'SELECT * FROM `cfp` WHERE dateCfpEnd > :now AND dateCfpStart < :now'; |
209
|
|
|
|
210
|
|
|
$statement = $this->pdo->prepare($statement); |
211
|
|
|
|
212
|
|
|
$list = new \Callingallpapers\Api\Entity\CfpList(); |
213
|
|
|
$statement->execute(['now' => (new \DateTime())->format('c')]); |
214
|
|
|
$content = $statement->fetchAll(); |
215
|
|
|
if (count($content) < 1) { |
216
|
|
|
throw new \UnexpectedValueException('No CFPs found', 404); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
View Code Duplication |
foreach ($content as $item) { |
|
|
|
|
220
|
|
|
$cfp = new \Callingallpapers\Api\Entity\Cfp(); |
221
|
|
|
$cfp->setName($item['name']); |
222
|
|
|
$cfp->setDateCfpEnd(new \DateTimeImmutable($item['dateCfpEnd'])); |
223
|
|
|
$cfp->setDateCfpStart(new \DateTimeImmutable($item['dateCfpStart'])); |
224
|
|
|
$cfp->setUri($item['uri']); |
225
|
|
|
$cfp->setTimezone(new \DateTimeZone($item['timezone'])); |
226
|
|
|
$cfp->setDateEventStart(new \DateTimeImmutable($item['dateEventStart'])); |
227
|
|
|
$cfp->setDateEventEnd(new \DateTimeImmutable($item['dateEventEnd'])); |
228
|
|
|
$cfp->setDescription($item['description']); |
229
|
|
|
$cfp->setEventUri($item['eventUri']); |
230
|
|
|
$cfp->setIconUri($item['iconUri']); |
231
|
|
|
$cfp->setLatitude($item['latitude']); |
232
|
|
|
$cfp->setLongitude($item['longitude']); |
233
|
|
|
$cfp->setLocation($item['location']); |
234
|
|
|
$cfp->setTags(explode(',', $item['tags'])); |
235
|
|
|
$cfp->setLastUpdated(new \DateTimeImmutable($item['lastUpdate'])); |
236
|
|
|
|
237
|
|
|
$list->add($cfp); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return $list; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
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.