1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PlaygroundGame\Controller\Frontend; |
4
|
|
|
|
5
|
|
|
class LotteryController extends GameController |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var gameService |
9
|
|
|
*/ |
10
|
|
|
protected $gameService; |
11
|
|
|
|
12
|
|
|
public function playAction() |
13
|
|
|
{ |
14
|
|
|
$redirectFb = $this->checkFbRegistration($this->user, $this->game); |
15
|
|
|
if ($redirectFb) { |
16
|
|
|
return $redirectFb; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
$entry = $this->getGameService()->play($this->game, $this->user); |
20
|
|
View Code Duplication |
if (!$entry) { |
|
|
|
|
21
|
|
|
// the user has already taken part of this game and the participation limit has been reached |
22
|
|
|
$this->flashMessenger()->addMessage('Vous avez déjà participé'); |
23
|
|
|
|
24
|
|
|
return $this->redirect()->toUrl( |
25
|
|
|
$this->frontendUrl()->fromRoute( |
|
|
|
|
26
|
|
|
'lottery/result', |
27
|
|
|
array('id' => $this->game->getIdentifier()) |
28
|
|
|
) |
29
|
|
|
); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
// Every entry is eligible to draw |
33
|
|
|
$entry->setDrawable(true); |
34
|
|
|
$entry->setActive(false); |
35
|
|
|
$this->getGameService()->getEntryMapper()->update($entry); |
36
|
|
|
|
37
|
|
|
return $this->redirect()->toUrl( |
38
|
|
|
$this->frontendUrl()->fromRoute( |
|
|
|
|
39
|
|
|
$this->game->getClassType() . '/'. $this->game->nextStep($this->params('action')), |
40
|
|
|
array('id' => $this->game->getIdentifier()) |
41
|
|
|
) |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function resultAction() |
46
|
|
|
{ |
47
|
|
|
$statusMail = null; |
48
|
|
|
$secretKey = strtoupper(substr(sha1(uniqid('pg_', true).'####'.time()), 0, 15)); |
49
|
|
|
$socialLinkUrl = $this->frontendUrl()->fromRoute( |
|
|
|
|
50
|
|
|
'lottery', |
51
|
|
|
array('id' => $this->game->getIdentifier()), |
52
|
|
|
array('force_canonical' => true) |
53
|
|
|
).'?key='.$secretKey; |
54
|
|
|
// With core shortener helper |
55
|
|
|
$socialLinkUrl = $this->shortenUrl()->shortenUrl($socialLinkUrl); |
|
|
|
|
56
|
|
|
|
57
|
|
|
$lastEntry = $this->getGameService()->findLastInactiveEntry($this->game, $this->user); |
58
|
|
View Code Duplication |
if (!$lastEntry) { |
|
|
|
|
59
|
|
|
return $this->redirect()->toUrl( |
60
|
|
|
$this->frontendUrl()->fromRoute( |
|
|
|
|
61
|
|
|
'lottery', |
62
|
|
|
array('id' => $this->game->getIdentifier()), |
63
|
|
|
array('force_canonical' => true) |
64
|
|
|
) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_sharemail_form'); |
69
|
|
|
$form->setAttribute('method', 'post'); |
70
|
|
|
|
71
|
|
|
if ($this->getRequest()->isPost()) { |
|
|
|
|
72
|
|
|
$data = $this->getRequest()->getPost()->toArray(); |
|
|
|
|
73
|
|
|
$form->setData($data); |
74
|
|
|
if ($form->isValid()) { |
75
|
|
|
$result = $this->getGameService()->sendShareMail($data, $this->game, $this->user, $lastEntry); |
76
|
|
|
if ($result) { |
77
|
|
|
$statusMail = true; |
78
|
|
|
$this->getGameService()->addAnotherChance($this->game, $this->user, 1); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// buildView must be before sendMail because it adds the game template path to the templateStack |
84
|
|
|
$viewModel = $this->buildView($this->game); |
85
|
|
|
|
86
|
|
|
$this->getGameService()->sendMail($this->game, $this->user, $lastEntry); |
87
|
|
|
|
88
|
|
|
$viewModel->setVariables(array( |
|
|
|
|
89
|
|
|
'statusMail' => $statusMail, |
90
|
|
|
'form' => $form, |
91
|
|
|
'socialLinkUrl' => $socialLinkUrl, |
92
|
|
|
'secretKey' => $secretKey, |
93
|
|
|
)); |
94
|
|
|
|
95
|
|
|
return $viewModel; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
View Code Duplication |
public function fbshareAction() |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$result = parent::fbshareAction(); |
101
|
|
|
$bonusEntry = false; |
102
|
|
|
|
103
|
|
|
if ($result->getVariable('success')) { |
104
|
|
|
$bonusEntry = $this->getGameService()->addAnotherChance($this->game, $this->user, 1); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$response = $this->getResponse(); |
108
|
|
|
$response->setContent(\Zend\Json\Json::encode(array( |
109
|
|
|
'success' => $result, |
110
|
|
|
'playBonus' => $bonusEntry |
111
|
|
|
))); |
112
|
|
|
|
113
|
|
|
return $response; |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
View Code Duplication |
public function fbrequestAction() |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$result = parent::fbrequestAction(); |
119
|
|
|
$bonusEntry = false; |
120
|
|
|
|
121
|
|
|
if ($result->getVariable('success')) { |
122
|
|
|
$bonusEntry = $this->getGameService()->addAnotherChance($this->game, $this->user, 1); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$response = $this->getResponse(); |
126
|
|
|
$response->setContent(\Zend\Json\Json::encode(array( |
127
|
|
|
'success' => $result, |
128
|
|
|
'playBonus' => $bonusEntry |
129
|
|
|
))); |
130
|
|
|
|
131
|
|
|
return $response; |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
View Code Duplication |
public function tweetAction() |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
$result = parent::tweetAction(); |
137
|
|
|
$bonusEntry = false; |
138
|
|
|
|
139
|
|
|
if ($result->getVariable('success')) { |
140
|
|
|
$bonusEntry = $this->getGameService()->addAnotherChance($this->game, $this->user, 1); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$response = $this->getResponse(); |
144
|
|
|
$response->setContent(\Zend\Json\Json::encode(array( |
145
|
|
|
'success' => $result, |
146
|
|
|
'playBonus' => $bonusEntry |
147
|
|
|
))); |
148
|
|
|
|
149
|
|
|
return $response; |
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
152
|
|
View Code Duplication |
public function googleAction() |
|
|
|
|
153
|
|
|
{ |
154
|
|
|
$result = parent::googleAction(); |
155
|
|
|
$bonusEntry = false; |
156
|
|
|
|
157
|
|
|
if ($result->getVariable('success')) { |
158
|
|
|
$bonusEntry = $this->getGameService()->addAnotherChance($this->game, $this->user, 1); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$response = $this->getResponse(); |
162
|
|
|
$response->setContent(\Zend\Json\Json::encode(array( |
163
|
|
|
'success' => $result, |
164
|
|
|
'playBonus' => $bonusEntry |
165
|
|
|
))); |
166
|
|
|
|
167
|
|
|
return $response; |
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function getGameService() |
171
|
|
|
{ |
172
|
|
|
if (!$this->gameService) { |
173
|
|
|
$this->gameService = $this->getServiceLocator()->get('playgroundgame_lottery_service'); |
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return $this->gameService; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
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.