1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PlaygroundGame\Controller\Admin; |
4
|
|
|
|
5
|
|
|
use PlaygroundGame\Service\Game as AdminGameService; |
6
|
|
|
use PlaygroundGame\Entity\Lottery; |
7
|
|
|
use PlaygroundGame\Controller\Admin\GameController; |
8
|
|
|
use Zend\View\Model\ViewModel; |
9
|
|
|
|
10
|
|
|
class LotteryController extends GameController |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var GameService |
14
|
|
|
*/ |
15
|
|
|
protected $adminGameService; |
16
|
|
|
|
17
|
|
|
public function createLotteryAction() |
18
|
|
|
{ |
19
|
|
|
$service = $this->getAdminGameService(); |
20
|
|
|
$viewModel = new ViewModel(); |
21
|
|
|
$viewModel->setTemplate('playground-game/lottery/lottery'); |
22
|
|
|
|
23
|
|
|
$gameForm = new ViewModel(); |
24
|
|
|
$gameForm->setTemplate('playground-game/game/game-form'); |
25
|
|
|
|
26
|
|
|
$lottery = new Lottery(); |
27
|
|
|
|
28
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_lottery_form'); |
29
|
|
|
$form->bind($lottery); |
30
|
|
|
$form->get('submit')->setAttribute('label', 'Add'); |
31
|
|
|
$form->setAttribute( |
32
|
|
|
'action', |
33
|
|
|
$this->url()->fromRoute( |
34
|
|
|
'admin/playgroundgame/create-lottery', |
35
|
|
|
array('gameId' => 0) |
36
|
|
|
) |
37
|
|
|
); |
38
|
|
|
$form->setAttribute('method', 'post'); |
39
|
|
|
|
40
|
|
|
$request = $this->getRequest(); |
41
|
|
|
if ($request->isPost()) { |
42
|
|
|
$data = array_replace_recursive( |
43
|
|
|
$this->getRequest()->getPost()->toArray(), |
|
|
|
|
44
|
|
|
$this->getRequest()->getFiles()->toArray() |
|
|
|
|
45
|
|
|
); |
46
|
|
|
if (empty($data['prizes'])) { |
47
|
|
|
$data['prizes'] = array(); |
48
|
|
|
} |
49
|
|
|
if (isset($data['drawDate']) && $data['drawDate']) { |
50
|
|
|
$data['drawDate'] = \DateTime::createFromFormat('d/m/Y', $data['drawDate']); |
51
|
|
|
} |
52
|
|
|
$game = $service->create($data, $lottery, 'playgroundgame_lottery_form'); |
53
|
|
|
if ($game) { |
54
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The game was created'); |
55
|
|
|
|
56
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/list'); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
$gameForm->setVariables(array('form' => $form, 'game' => $lottery)); |
60
|
|
|
$viewModel->addChild($gameForm, 'game_form'); |
61
|
|
|
|
62
|
|
|
return $viewModel->setVariables(array('form' => $form, 'title' => 'Create lottery')); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function editLotteryAction() |
66
|
|
|
{ |
67
|
|
|
$this->checkGame(); |
68
|
|
|
|
69
|
|
|
return $this->editGame( |
70
|
|
|
'playground-game/lottery/lottery', |
71
|
|
|
'playgroundgame_lottery_form' |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getAdminGameService() |
76
|
|
|
{ |
77
|
|
|
if (!$this->adminGameService) { |
78
|
|
|
$this->adminGameService = $this->getServiceLocator()->get('playgroundgame_lottery_service'); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $this->adminGameService; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function setAdminGameService(AdminGameService $adminGameService) |
85
|
|
|
{ |
86
|
|
|
$this->adminGameService = $adminGameService; |
|
|
|
|
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: