|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PlaygroundGame\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Zend\Stdlib\ErrorHandler; |
|
6
|
|
|
|
|
7
|
|
|
class TradingCard extends Game |
|
8
|
|
|
{ |
|
9
|
|
|
protected $tradingcardMapper; |
|
10
|
|
|
protected $tradingcardmodelMapper; |
|
11
|
|
|
protected $tradingcardcardMapper; |
|
12
|
|
|
|
|
13
|
|
|
public function getModelPath($model) |
|
14
|
|
|
{ |
|
15
|
|
|
$path = $this->getOptions()->getMediaPath().DIRECTORY_SEPARATOR; |
|
16
|
|
|
$path .= 'game'.$model->getGame()->getId().DIRECTORY_SEPARATOR; |
|
17
|
|
|
if (!is_dir($path)) { |
|
18
|
|
|
mkdir($path, 0777, true); |
|
19
|
|
|
} |
|
20
|
|
|
$path .= 'models'.DIRECTORY_SEPARATOR; |
|
21
|
|
|
if (!is_dir($path)) { |
|
22
|
|
|
mkdir($path, 0777, true); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
return $path; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function getModelMediaUrl($model) |
|
29
|
|
|
{ |
|
30
|
|
|
$media_url = $this->getOptions()->getMediaUrl().'/'; |
|
31
|
|
|
$media_url .= 'game'.$model->getGame()->getId().'/models/'; |
|
32
|
|
|
|
|
33
|
|
|
return $media_url; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param array $data |
|
38
|
|
|
* @return \PlaygroundGame\Entity\Game |
|
39
|
|
|
*/ |
|
40
|
|
|
public function updateModel(array $data, $model) |
|
41
|
|
|
{ |
|
42
|
|
|
$form = $this->serviceLocator->get('playgroundgame_tradingcardmodel_form'); |
|
43
|
|
|
$tradingcard = $this->getGameMapper()->findById($data['trading_card_id']); |
|
44
|
|
|
$model->setGame($tradingcard); |
|
45
|
|
|
$path = $this->getModelPath($model); |
|
46
|
|
|
$media_url = $this->getModelMediaUrl($model); |
|
47
|
|
|
|
|
48
|
|
|
$form->bind($model); |
|
49
|
|
|
$form->setData($data); |
|
50
|
|
|
|
|
51
|
|
|
if (!$form->isValid()) { |
|
52
|
|
|
return false; |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
View Code Duplication |
if (!empty($data['upload_image']['tmp_name'])) { |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
ErrorHandler::start(); |
|
57
|
|
|
$data['upload_image']['name'] = $this->fileNewname( |
|
58
|
|
|
$path, |
|
59
|
|
|
$model->getId()."-".$data['upload_image']['name'] |
|
60
|
|
|
); |
|
61
|
|
|
move_uploaded_file($data['upload_image']['tmp_name'], $path.$data['upload_image']['name']); |
|
62
|
|
|
$model->setImage($media_url.$data['upload_image']['name']); |
|
63
|
|
|
ErrorHandler::stop(true); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$this->getTradingCardModelMapper()->update($model); |
|
67
|
|
|
$this->getEventManager()->trigger( |
|
68
|
|
|
__FUNCTION__ .'.post', |
|
69
|
|
|
$this, |
|
70
|
|
|
array('model' => $model, 'data' => $data) |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
return $model; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function getBooster($game, $user, $entry) |
|
77
|
|
|
{ |
|
78
|
|
|
// get booster config from $game |
|
79
|
|
|
$em = $this->serviceLocator->get('doctrine.entitymanager.orm_default'); |
|
80
|
|
|
$nb = $game->getBoosterCardNumber(); |
|
81
|
|
|
$booster = []; |
|
82
|
|
|
|
|
83
|
|
|
$today = new \DateTime("now"); |
|
84
|
|
|
$today = $today->format('Y-m-d H:i:s'); |
|
85
|
|
|
|
|
86
|
|
|
$qb = $em->createQueryBuilder(); |
|
87
|
|
|
$and = $qb->expr()->andx(); |
|
88
|
|
|
$and->add( |
|
89
|
|
|
$qb->expr()->orX( |
|
90
|
|
|
$qb->expr()->lte('g.availability', ':date'), |
|
91
|
|
|
$qb->expr()->isNull('g.availability') |
|
92
|
|
|
) |
|
93
|
|
|
); |
|
94
|
|
|
|
|
95
|
|
|
$qb->setParameter('date', $today); |
|
96
|
|
|
$qb->select('g') |
|
97
|
|
|
->from('PlaygroundGame\Entity\TradingCardModel', 'g') |
|
98
|
|
|
->where($and); |
|
99
|
|
|
|
|
100
|
|
|
$query = $qb->getQuery(); |
|
101
|
|
|
$models = $query->getResult(); |
|
102
|
|
|
|
|
103
|
|
|
shuffle($models); |
|
104
|
|
|
|
|
105
|
|
|
$eventModels = $this->getEventManager()->trigger( |
|
106
|
|
|
__FUNCTION__ .'.pre', |
|
107
|
|
|
$this, |
|
108
|
|
|
array( |
|
109
|
|
|
'game' => $game, |
|
110
|
|
|
'user' => $user, |
|
111
|
|
|
'entry' => $entry, |
|
112
|
|
|
'models' => $models, |
|
113
|
|
|
) |
|
114
|
|
|
)->last(); |
|
115
|
|
|
|
|
116
|
|
|
if ($eventModels) { |
|
117
|
|
|
$models = $eventModels; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
for ($i = 0; $i < $nb; $i++) { |
|
121
|
|
|
$model = $models[$i]; |
|
122
|
|
|
$card = new \PlaygroundGame\Entity\TradingCardCard(); |
|
123
|
|
|
$card->setUser($user); |
|
124
|
|
|
$card->setModel($model); |
|
125
|
|
|
$card->setGame($game); |
|
126
|
|
|
$card->setEntry($entry); |
|
127
|
|
|
$card = $this->getTradingCardCardMapper()->insert($card); |
|
128
|
|
|
|
|
129
|
|
|
$booster[] = $card; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$eventBooster = $this->getEventManager()->trigger( |
|
133
|
|
|
__FUNCTION__ .'.post', |
|
134
|
|
|
$this, |
|
135
|
|
|
array( |
|
136
|
|
|
'game' => $game, |
|
137
|
|
|
'user' => $user, |
|
138
|
|
|
'entry' => $entry, |
|
139
|
|
|
'booster' => $booster, |
|
140
|
|
|
) |
|
141
|
|
|
)->last(); |
|
142
|
|
|
|
|
143
|
|
|
if ($eventBooster) { |
|
144
|
|
|
$booster = $eventBooster; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
// sending a booster represents an entry. We close the entry after that |
|
148
|
|
|
$entry->setActive(0); |
|
149
|
|
|
$entry = $this->getEntryMapper()->update($entry); |
|
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
return $booster; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function getAlbum($game, $user) |
|
155
|
|
|
{ |
|
156
|
|
|
// all the models of the album |
|
157
|
|
|
$models = $this->getTradingCardModelMapper()->findAll(); |
|
158
|
|
|
$em = $this->serviceLocator->get('doctrine.entitymanager.orm_default'); |
|
159
|
|
|
|
|
160
|
|
|
if ($user) { |
|
161
|
|
|
$qb = $em->createQueryBuilder(); |
|
162
|
|
|
$and = $qb->expr()->andx(); |
|
163
|
|
|
$and->add($qb->expr()->eq('g.id', ':game')); |
|
164
|
|
|
$qb->setParameter('game', $game); |
|
165
|
|
|
$and->add($qb->expr()->eq('u.id', ':user')); |
|
166
|
|
|
$qb->setParameter('user', $user); |
|
167
|
|
|
$qb->select('c') |
|
168
|
|
|
->from('PlaygroundGame\Entity\TradingCardCard', 'c') |
|
169
|
|
|
->innerJoin('c.game', 'g') |
|
170
|
|
|
->innerJoin('c.model', 'm') |
|
171
|
|
|
->innerJoin('c.user', 'u') |
|
172
|
|
|
->where($and) |
|
173
|
|
|
->orderBy('m.id', 'ASC') |
|
174
|
|
|
->groupBy('c.model'); |
|
175
|
|
|
$query = $qb->getQuery(); |
|
176
|
|
|
} elseif ($game->getAnonymousAllowed() && $this->getAnonymousIdentifier()) { |
|
177
|
|
|
$limitDate = $this->getLimitDate('always'); |
|
178
|
|
|
$entries = $this->getEntryMapper()->findLastEntriesByAnonymousIdentifier( |
|
179
|
|
|
$game, |
|
180
|
|
|
$this->getAnonymousIdentifier(), |
|
181
|
|
|
$limitDate |
|
182
|
|
|
); |
|
183
|
|
|
$qb = $em->createQueryBuilder(); |
|
184
|
|
|
$qb->andWhere('e.id IN (:entries)')->setParameter('entries', $entries); |
|
185
|
|
|
$qb->select('c') |
|
186
|
|
|
->from('PlaygroundGame\Entity\TradingCardCard', 'c') |
|
187
|
|
|
->innerJoin('c.model', 'm') |
|
188
|
|
|
->innerJoin('c.game', 'g') |
|
189
|
|
|
->innerJoin('c.entry', 'e') |
|
190
|
|
|
->orderBy('m.id', 'ASC') |
|
191
|
|
|
->groupBy('c.model'); |
|
192
|
|
|
$query = $qb->getQuery(); |
|
193
|
|
|
} elseif ($game->getAnonymousAllowed()) { |
|
194
|
|
|
$limitDate = $this->getLimitDate('always'); |
|
195
|
|
|
$entries = $this->getEntryMapper()->findLastEntriesByIp( |
|
196
|
|
|
$game, |
|
197
|
|
|
$this->getIp(), |
|
198
|
|
|
$limitDate |
|
199
|
|
|
); |
|
200
|
|
|
$qb = $em->createQueryBuilder(); |
|
201
|
|
|
$qb->andWhere('e.id IN (:entries)')->setParameter('entries', $entries); |
|
202
|
|
|
$qb->select('c') |
|
203
|
|
|
->from('PlaygroundGame\Entity\TradingCardCard', 'c') |
|
204
|
|
|
->innerJoin('c.model', 'm') |
|
205
|
|
|
->innerJoin('c.game', 'g') |
|
206
|
|
|
->innerJoin('c.entry', 'e') |
|
207
|
|
|
->orderBy('m.id', 'ASC') |
|
208
|
|
|
->groupBy('c.model'); |
|
209
|
|
|
$query = $qb->getQuery(); |
|
210
|
|
View Code Duplication |
} else { |
|
|
|
|
|
|
211
|
|
|
// If the game is supposed to be a regular user game or an anonymous identified game, |
|
212
|
|
|
// it means that the registration/login is at the end of the game |
|
213
|
|
|
if((!$user && !$game->getAnonymousAllowed()) || ($game->getAnonymousAllowed() && $game->getAnonymousIdentifier())) { |
|
214
|
|
|
return 0; |
|
215
|
|
|
} |
|
216
|
|
|
return $this->getEntryMapper()->findLastEntriesByIp($game, $this->getIp(), $limitDate); |
|
|
|
|
|
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
|
|
220
|
|
|
// all the cards of the user |
|
221
|
|
|
$cards = $query->getResult(); |
|
222
|
|
|
$cardsArray = []; |
|
223
|
|
|
foreach ($cards as $card) { |
|
224
|
|
|
$cardsArray[$card->getModel()->getId()] = $card; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
$album = []; |
|
228
|
|
|
|
|
229
|
|
|
// I create the complete album including the cards of the user |
|
230
|
|
|
foreach($models as $model) { |
|
231
|
|
|
$sticker['model'] = $model; |
|
|
|
|
|
|
232
|
|
|
if(isset($cardsArray[$model->getId()])) { |
|
233
|
|
|
$sticker['card'] = $cardsArray[$model->getId()]; |
|
234
|
|
|
} else { |
|
235
|
|
|
$sticker['card'] = null; |
|
|
|
|
|
|
236
|
|
|
} |
|
237
|
|
|
$album[] = $sticker; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
return $album; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
public function getGameEntity() |
|
244
|
|
|
{ |
|
245
|
|
|
return new \PlaygroundGame\Entity\TradingCard; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
public function getTradingCardMapper() |
|
249
|
|
|
{ |
|
250
|
|
|
if (null === $this->tradingcardMapper) { |
|
251
|
|
|
$this->tradingcardMapper = $this->serviceLocator->get('playgroundgame_tradingcard_mapper'); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
return $this->tradingcardMapper; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
public function getTradingCardCardMapper() |
|
258
|
|
|
{ |
|
259
|
|
|
if (null === $this->tradingcardcardMapper) { |
|
260
|
|
|
$this->tradingcardcardMapper = $this->serviceLocator->get('playgroundgame_tradingcard_card_mapper'); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
return $this->tradingcardcardMapper; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
public function getTradingCardModelMapper() |
|
267
|
|
|
{ |
|
268
|
|
|
if (null === $this->tradingcardmodelMapper) { |
|
269
|
|
|
$this->tradingcardmodelMapper = $this->serviceLocator->get('playgroundgame_tradingcard_model_mapper'); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
return $this->tradingcardmodelMapper; |
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.