1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PlaygroundGame\Controller\Frontend; |
4
|
|
|
|
5
|
|
|
class InstantWinController 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
|
|
|
if ($this->game->getOccurrenceType()=='datetime') { |
20
|
|
|
$entry = $this->getGameService()->play($this->game, $this->user); |
21
|
|
View Code Duplication |
if (!$entry) { |
|
|
|
|
22
|
|
|
// the user has already taken part of this game and the participation limit has been reached |
23
|
|
|
$this->flashMessenger()->addMessage('Vous avez déjà participé'); |
24
|
|
|
|
25
|
|
|
return $this->redirect()->toUrl( |
26
|
|
|
$this->frontendUrl()->fromRoute( |
|
|
|
|
27
|
|
|
'instantwin/result', |
28
|
|
|
array('id' => $this->game->getIdentifier()) |
29
|
|
|
) |
30
|
|
|
); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
// update the winner attribute in entry. |
34
|
|
|
$occurrence = $this->getGameService()->IsInstantWinner($this->game, $this->user); |
35
|
|
|
|
36
|
|
|
$viewVariables = array( |
37
|
|
|
'occurrence' => $occurrence, |
38
|
|
|
'entry' => $entry |
39
|
|
|
); |
40
|
|
|
} elseif ($this->game->getOccurrenceType()=='code') { |
41
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_instantwinoccurrencecode_form'); |
42
|
|
|
$form->setAttribute( |
43
|
|
|
'action', |
44
|
|
|
$this->frontendUrl()->fromRoute( |
|
|
|
|
45
|
|
|
'instantwin/play', |
46
|
|
|
array('id' => $this->game->getIdentifier()), |
47
|
|
|
array('force_canonical' => true) |
48
|
|
|
) |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
if ($this->getRequest()->isPost()) { |
|
|
|
|
52
|
|
|
$form->setData($this->getRequest()->getPost()); |
|
|
|
|
53
|
|
|
if ($form->isValid()) { |
54
|
|
|
$data = $form->getData('code-input'); |
55
|
|
|
$code = filter_var($data['code-input'], FILTER_SANITIZE_STRING); |
56
|
|
|
$occurrence = $this->getGameService()->isInstantWinner($this->game, $this->user, $code); |
57
|
|
|
if (!$occurrence) { |
58
|
|
|
$this->flashMessenger()->addMessage('Le code entré est invalide ou a déjà été utilisé !'); |
59
|
|
|
return $this->redirect()->toUrl( |
60
|
|
|
$this->frontendUrl()->fromRoute( |
|
|
|
|
61
|
|
|
'instantwin/play', |
62
|
|
|
array('id' => $this->game->getIdentifier()), |
63
|
|
|
array('force_canonical' => true) |
64
|
|
|
) |
65
|
|
|
); |
66
|
|
|
} else { |
67
|
|
|
return $this->redirect()->toUrl( |
68
|
|
|
$this->frontendUrl()->fromRoute( |
|
|
|
|
69
|
|
|
'instantwin/result', |
70
|
|
|
array('id' => $this->game->getIdentifier()) |
71
|
|
|
) |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
$viewVariables = array('form' => $form); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$viewModel = $this->buildView($this->game); |
80
|
|
|
if ($viewModel instanceof \Zend\View\Model\ViewModel) { |
81
|
|
|
$viewModel->setVariables($viewVariables); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $viewModel; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function resultAction() |
88
|
|
|
{ |
89
|
|
|
$lastEntry = $this->getGameService()->findLastInactiveEntry($this->game, $this->user); |
90
|
|
|
|
91
|
|
View Code Duplication |
if (!$lastEntry) { |
|
|
|
|
92
|
|
|
return $this->redirect()->toUrl( |
93
|
|
|
$this->frontendUrl()->fromRoute( |
|
|
|
|
94
|
|
|
'instantwin', |
95
|
|
|
array('id' => $this->game->getIdentifier(), ), |
96
|
|
|
array('force_canonical' => true) |
97
|
|
|
) |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
$winner = $lastEntry->getWinner(); |
101
|
|
|
$occurrence = null; |
102
|
|
|
|
103
|
|
|
// On tente de récupèrer l'occurrence si elle existe pour avoir accés au lot associé |
104
|
|
|
$occurrences = $this->getGameService()->getInstantWinOccurrenceMapper()->findBy( |
105
|
|
|
array('instantwin' => $this->game->getId(), 'entry' => $lastEntry->getId()) |
106
|
|
|
); |
107
|
|
|
if (!empty($occurrences)) { |
108
|
|
|
$occurrence = current($occurrences); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$secretKey = strtoupper(substr(sha1(uniqid('pg_', true).'####'.time()), 0, 15)); |
112
|
|
|
$socialLinkUrl = $this->frontendUrl()->fromRoute( |
|
|
|
|
113
|
|
|
'instantwin', |
114
|
|
|
array('id' => $this->game->getIdentifier(), ), |
115
|
|
|
array('force_canonical' => true) |
116
|
|
|
).'?key='.$secretKey; |
117
|
|
|
// With core shortener helper |
118
|
|
|
$socialLinkUrl = $this->shortenUrl()->shortenUrl($socialLinkUrl); |
|
|
|
|
119
|
|
|
|
120
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_sharemail_form'); |
121
|
|
|
$form->setAttribute('method', 'post'); |
122
|
|
|
|
123
|
|
|
$statusMail = null; |
124
|
|
|
if ($this->getRequest()->isPost()) { |
|
|
|
|
125
|
|
|
$data = $this->getRequest()->getPost()->toArray(); |
|
|
|
|
126
|
|
|
$form->setData($data); |
127
|
|
|
if ($form->isValid()) { |
128
|
|
|
if (isset($data['email1']) || isset($data['email2']) || isset($data['email3'])) { |
129
|
|
|
$result = $this->getGameService()->sendShareMail($data, $this->game, $this->user, $lastEntry); |
130
|
|
|
if ($result) { |
131
|
|
|
$statusMail = true; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$prize = null; |
138
|
|
|
if ($occurrence instanceof \PlaygroundGame\Entity\InstantWinOccurrence) { |
139
|
|
|
$prize = $occurrence->getPrize(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
// buildView must be before sendMail because it adds the game template path to the templateStack |
143
|
|
|
$viewModel = $this->buildView($this->game); |
144
|
|
|
|
145
|
|
|
$this->getGameService()->sendMail($this->game, $this->user, $lastEntry, $prize); |
146
|
|
|
|
147
|
|
|
if ($viewModel instanceof \Zend\View\Model\ViewModel) { |
148
|
|
|
$viewModel->setVariables(array( |
149
|
|
|
'occurrence' => $occurrence, |
150
|
|
|
'statusMail' => $statusMail, |
151
|
|
|
'winner' => $winner, |
152
|
|
|
'form' => $form, |
153
|
|
|
'socialLinkUrl' => $socialLinkUrl, |
154
|
|
|
'secretKey' => $secretKey, |
155
|
|
|
)); |
156
|
|
|
} |
157
|
|
|
return $viewModel; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function getGameService() |
161
|
|
|
{ |
162
|
|
|
if (!$this->gameService) { |
163
|
|
|
$this->gameService = $this->getServiceLocator()->get('playgroundgame_instantwin_service'); |
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $this->gameService; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
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.