1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PlaygroundGame\Service; |
4
|
|
|
|
5
|
|
|
use Zend\ServiceManager\ServiceManagerAwareInterface; |
6
|
|
|
use PlaygroundGame\Mapper\GameInterface as GameMapperInterface; |
7
|
|
|
use Zend\Stdlib\ErrorHandler; |
8
|
|
|
|
9
|
|
|
class TradingCard extends Game implements ServiceManagerAwareInterface |
10
|
|
|
{ |
11
|
|
|
protected $tradingcardMapper; |
12
|
|
|
protected $tradingcardmodelMapper; |
13
|
|
|
protected $tradingcardcardMapper; |
14
|
|
|
|
15
|
|
View Code Duplication |
public function getPath($model) |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
$path = $this->getOptions()->getMediaPath() . DIRECTORY_SEPARATOR; |
18
|
|
|
$path .= 'game' . $model->getGame()->getId() . DIRECTORY_SEPARATOR; |
19
|
|
|
if (!is_dir($path)) { |
20
|
|
|
mkdir($path, 0777, true); |
21
|
|
|
} |
22
|
|
|
$path .= 'model'. $model->getId() . DIRECTORY_SEPARATOR; |
23
|
|
|
if (!is_dir($path)) { |
24
|
|
|
mkdir($path, 0777, true); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
return $path; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
View Code Duplication |
public function getMediaUrl($model) |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
$media_url = $this->getOptions()->getMediaUrl() . '/'; |
33
|
|
|
$media_url .= 'game' . $model->getGame()->getId() . '/' . 'model'. $model->getId() . '/'; |
34
|
|
|
|
35
|
|
|
return $media_url; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param array $data |
40
|
|
|
* @return \PlaygroundGame\Entity\Game |
41
|
|
|
*/ |
42
|
|
|
public function updateModel(array $data, $model) |
43
|
|
|
{ |
44
|
|
|
$path = $this->getPath($model); |
45
|
|
|
$media_url = $this->getMediaUrl($model); |
46
|
|
|
$form = $this->getServiceManager()->get('playgroundgame_tradingcardmodel_form'); |
47
|
|
|
$tradingcard = $this->getGameMapper()->findById($data['trading_card_id']); |
48
|
|
|
|
49
|
|
|
$model->setGame($tradingcard); |
50
|
|
|
$form->bind($model); |
51
|
|
|
$form->setData($data); |
52
|
|
|
|
53
|
|
|
if (!$form->isValid()) { |
54
|
|
|
return false; |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
View Code Duplication |
if (!empty($data['upload_image']['tmp_name'])) { |
|
|
|
|
58
|
|
|
ErrorHandler::start(); |
59
|
|
|
$data['upload_image']['name'] = $this->fileNewname( |
60
|
|
|
$path, |
61
|
|
|
$model->getId() . "-" . $data['upload_image']['name'] |
62
|
|
|
); |
63
|
|
|
move_uploaded_file($data['upload_image']['tmp_name'], $path . $data['upload_image']['name']); |
64
|
|
|
$model->setImage($media_url . $data['upload_image']['name']); |
65
|
|
|
ErrorHandler::stop(true); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// if (isset($data['delete_image']) && empty($data['upload_image']['tmp_name'])) { |
69
|
|
|
// ErrorHandler::start(); |
70
|
|
|
// $image = $model->getImage(); |
71
|
|
|
// $image = str_replace($media_url, '', $image); |
72
|
|
|
// if (file_exists($path .$image)) { |
73
|
|
|
// unlink($path .$image); |
74
|
|
|
// } |
75
|
|
|
// $model->setImage(null); |
76
|
|
|
// ErrorHandler::stop(true); |
77
|
|
|
// } |
78
|
|
|
|
79
|
|
|
$this->getTradingCardModelMapper()->update($model); |
80
|
|
|
$this->getEventManager()->trigger( |
81
|
|
|
__FUNCTION__.'.post', |
82
|
|
|
$this, |
83
|
|
|
array('model' => $model, 'data' => $data) |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
return $model; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getBooster($game, $user, $entry) |
90
|
|
|
{ |
91
|
|
|
// get booster config from $game |
92
|
|
|
$nb = $game->getBoosterCardNumber(); |
93
|
|
|
$booster = []; |
94
|
|
|
$models = $this->getTradingCardModelMapper()->findBy(array('game' => $game)); |
95
|
|
|
shuffle($models); |
96
|
|
|
|
97
|
|
|
$eventModels = $this->getEventManager()->trigger( |
98
|
|
|
__FUNCTION__.'.pre', |
99
|
|
|
$this, |
100
|
|
|
array( |
101
|
|
|
'game' => $game, |
102
|
|
|
'user' => $user, |
103
|
|
|
'entry' => $entry, |
104
|
|
|
'models' => $models |
105
|
|
|
) |
106
|
|
|
)->last(); |
107
|
|
|
|
108
|
|
|
if ($eventModels) { |
109
|
|
|
$models = $eventModels; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
for ($i=1; $i<=$nb; $i++) { |
113
|
|
|
$model = $models[$i]; |
114
|
|
|
$card = new \PlaygroundGame\Entity\TradingCardCard(); |
115
|
|
|
$card->setUser($user); |
116
|
|
|
$card->setModel($model); |
117
|
|
|
$card->setGame($game); |
118
|
|
|
$card->setEntry($entry); |
119
|
|
|
$card = $this->getTradingCardCardMapper()->insert($card); |
120
|
|
|
|
121
|
|
|
$booster[] = $card; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$eventBooster = $this->getEventManager()->trigger( |
125
|
|
|
__FUNCTION__.'.post', |
126
|
|
|
$this, |
127
|
|
|
array( |
128
|
|
|
'game' => $game, |
129
|
|
|
'user' => $user, |
130
|
|
|
'entry' => $entry, |
131
|
|
|
'booster' => $booster |
132
|
|
|
) |
133
|
|
|
)->last(); |
134
|
|
|
|
135
|
|
|
if ($eventBooster) { |
136
|
|
|
$booster = $eventBooster; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
// sending a booster represents an entry. We close the entry after that |
140
|
|
|
$entry->setActive(0); |
141
|
|
|
$entry = $this->getEntryMapper()->update($entry); |
|
|
|
|
142
|
|
|
|
143
|
|
|
return $booster; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function getAlbum($game, $user) |
147
|
|
|
{ |
148
|
|
|
// get collection of cards from the user for this game |
149
|
|
|
$album = $this->getTradingCardCardMapper()->findBy(array('game' => $game, 'user' => $user)); |
150
|
|
|
$eventAlbum = $this->getEventManager()->trigger( |
151
|
|
|
__FUNCTION__.'.post', |
152
|
|
|
$this, |
153
|
|
|
array( |
154
|
|
|
'game' => $game, |
155
|
|
|
'user' => $user, |
156
|
|
|
'album' => $album |
157
|
|
|
) |
158
|
|
|
)->last(); |
159
|
|
|
|
160
|
|
|
if ($eventAlbum) { |
161
|
|
|
$album = $eventAlbum; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $album; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function getGameEntity() |
168
|
|
|
{ |
169
|
|
|
return new \PlaygroundGame\Entity\TradingCard; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function getTradingCardMapper() |
173
|
|
|
{ |
174
|
|
|
if (null === $this->tradingcardMapper) { |
175
|
|
|
$this->tradingcardMapper = $this->getServiceManager()->get('playgroundgame_tradingcard_mapper'); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $this->tradingcardMapper; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function getTradingCardCardMapper() |
182
|
|
|
{ |
183
|
|
|
if (null === $this->tradingcardcardMapper) { |
184
|
|
|
$this->tradingcardcardMapper = $this->getServiceManager()->get('playgroundgame_tradingcardcard_mapper'); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $this->tradingcardcardMapper; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function getTradingCardModelMapper() |
191
|
|
|
{ |
192
|
|
|
if (null === $this->tradingcardmodelMapper) { |
193
|
|
|
$this->tradingcardmodelMapper = $this->getServiceManager()->get('playgroundgame_tradingcardmodel_mapper'); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return $this->tradingcardmodelMapper; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
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.