1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Markus Tacker <[email protected]> |
5
|
|
|
* @copyright 2013-2016 Verein zur Förderung der Netzkultur im Rhein-Main-Gebiet e.V. | http://netzkultur-rheinmain.de/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace BCRM\WebBundle\Controller; |
9
|
|
|
|
10
|
|
|
use BCRM\BackendBundle\Entity\Event\Event; |
11
|
|
|
use BCRM\BackendBundle\Entity\Event\EventRepository; |
12
|
|
|
use BCRM\BackendBundle\Entity\Event\Registration; |
13
|
|
|
use BCRM\BackendBundle\Entity\Event\Ticket; |
14
|
|
|
use BCRM\BackendBundle\Entity\Event\TicketRepository; |
15
|
|
|
use BCRM\BackendBundle\Service\Concierge\CheckinCommand; |
16
|
|
|
use BCRM\BackendBundle\Service\Concierge\PayRegistrationConciergeCommand; |
17
|
|
|
use BCRM\BackendBundle\Service\Event\CreateTicketCommand; |
18
|
|
|
use BCRM\BackendBundle\Service\Event\RegisterCommand; |
19
|
|
|
use BCRM\WebBundle\Content\ContentReader; |
20
|
|
|
use BCRM\WebBundle\Exception\AccesDeniedHttpException; |
21
|
|
|
use BCRM\WebBundle\Exception\BadRequestException; |
22
|
|
|
use BCRM\WebBundle\Form\TicketType; |
23
|
|
|
use Carbon\Carbon; |
24
|
|
|
use LiteCQRS\Bus\CommandBus; |
25
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
26
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
27
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
28
|
|
|
use Symfony\Component\HttpFoundation\Request; |
29
|
|
|
use Symfony\Component\HttpFoundation\Response; |
30
|
|
|
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; |
31
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
32
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
33
|
|
|
use Symfony\Component\Routing\RouterInterface; |
34
|
|
|
use Symfony\Component\Validator\Constraints\DateTime; |
35
|
|
|
use Symfony\Component\Security\Core\Util\SecureRandom; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Dashboard for the event concierge. |
39
|
|
|
*/ |
40
|
|
|
class ConciergeController |
41
|
|
|
{ |
42
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
43
|
|
|
EventRepository $eventRepo, |
44
|
|
|
TicketRepository $ticketRepo, |
45
|
|
|
CommandBus $commandBus, |
46
|
|
|
FormFactoryInterface $formFactory, |
47
|
|
|
RouterInterface $router) |
48
|
|
|
{ |
49
|
|
|
$this->eventRepo = $eventRepo; |
|
|
|
|
50
|
|
|
$this->ticketRepo = $ticketRepo; |
|
|
|
|
51
|
|
|
$this->commandBus = $commandBus; |
|
|
|
|
52
|
|
|
$this->formFactory = $formFactory; |
|
|
|
|
53
|
|
|
$this->router = $router; |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Dashboard index |
58
|
|
|
* |
59
|
|
|
* @Template() |
60
|
|
|
*/ |
61
|
|
|
public function indexAction() |
62
|
|
|
{ |
63
|
|
|
/* @var $event Event */ |
64
|
|
|
$event = $this->eventRepo->getNextEvent()->getOrThrow(new AccesDeniedHttpException('No event.')); |
65
|
|
|
|
66
|
|
|
$stats = array(); |
67
|
|
|
foreach (array(Ticket::DAY_SATURDAY, Ticket::DAY_SUNDAY) as $day) { |
68
|
|
|
$stats[$day] = array( |
69
|
|
|
'tickets' => $this->ticketRepo->getTicketCountForEvent($event, $day), |
70
|
|
|
'checkins' => $this->ticketRepo->getCheckinCountForEvent($event, $day), |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
$data = array('stats' => $stats); |
74
|
|
|
return $data; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Concierges must be able to create Tickets manually. |
79
|
|
|
* |
80
|
|
|
* @Template() |
81
|
|
|
*/ |
82
|
|
|
public function createTicketAction(Request $request) |
83
|
|
|
{ |
84
|
|
|
$event = $this->eventRepo->getNextEvent()->getOrThrow(new AccessDeniedHttpException('No event.')); |
85
|
|
|
$registration = new Registration(); |
86
|
|
|
$registration->setEvent($event); |
87
|
|
|
$generator = new SecureRandom(); |
88
|
|
|
$registration->setUuid(sha1($generator->nextBytes(16))); |
89
|
|
|
$form = $this->formFactory->create(new TicketType(), $registration, array('action' => $request->getPathInfo())); |
90
|
|
|
$form->handleRequest($request); |
91
|
|
|
if ($form->isValid()) { |
92
|
|
|
/* @var Registration $formData */ |
93
|
|
|
$formData = $form->getData(); |
94
|
|
|
$registerCommand = new RegisterCommand(); |
95
|
|
|
$registerCommand->event = $event; |
96
|
|
|
$registerCommand->email = $formData->getEmail(); |
97
|
|
|
$registerCommand->name = $formData->getName(); |
98
|
|
|
$registerCommand->twitter = $formData->getTwitter(); |
99
|
|
|
$registerCommand->saturday = $formData->getSaturday(); |
100
|
|
|
$registerCommand->sunday = $formData->getSunday(); |
101
|
|
|
$registerCommand->tags = $formData->getTags(); |
102
|
|
|
$registerCommand->type = $formData->getType(); |
103
|
|
|
$registerCommand->uuid = $formData->getUuid(); |
104
|
|
|
$registerCommand->payment = 'concierge'; |
105
|
|
|
$this->commandBus->handle($registerCommand); |
106
|
|
|
|
107
|
|
|
// Create tickets |
108
|
|
|
/* @var FlashBagInterface $fb */ |
109
|
|
|
$fb = $request->getSession()->getFlashBag(); |
110
|
|
|
foreach ( |
111
|
|
|
array( |
112
|
|
|
Ticket::DAY_SATURDAY => $formData->getSaturday(), |
113
|
|
|
Ticket::DAY_SUNDAY => $formData->getSunday(), |
114
|
|
|
) as $day => $want) { |
115
|
|
|
if (!$want) continue; |
116
|
|
|
$ticketCommand = new CreateTicketCommand(); |
117
|
|
|
$ticketCommand->day = $day; |
|
|
|
|
118
|
|
|
$ticketCommand->event = $event; |
119
|
|
|
$ticketCommand->registration = $registration; |
120
|
|
|
$this->commandBus->handle($ticketCommand); |
121
|
|
|
$fb->add( |
122
|
|
|
'info', |
123
|
|
|
sprintf( |
124
|
|
|
'Ticket für %s / %s angelegt.', |
125
|
|
|
$registration->getEmail(), |
126
|
|
|
$day == Ticket::DAY_SATURDAY ? 'Samstag' : 'Sonntag' |
127
|
|
|
) |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
// Create payment |
132
|
|
|
$paymentCommand = new PayRegistrationConciergeCommand(); |
133
|
|
|
$paymentCommand->uuid = $registerCommand->uuid; |
134
|
|
|
$this->commandBus->handle($paymentCommand); |
135
|
|
|
|
136
|
|
|
return new RedirectResponse($this->router->generate('bcrmweb_concierge_index')); |
137
|
|
|
} |
138
|
|
|
return array( |
139
|
|
|
'form' => $form->createView(), |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Allows the concierge to checkin attendees which did not bring their ticket, |
145
|
|
|
* |
146
|
|
|
* @Template() |
147
|
|
|
*/ |
148
|
|
|
public function manualCheckinAction() |
149
|
|
|
{ |
150
|
|
|
$event = $this->eventRepo->getNextEvent()->getOrThrow(new AccessDeniedHttpException('No event.')); |
|
|
|
|
151
|
|
|
return array(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Search for tickets |
156
|
|
|
* |
157
|
|
|
* @param Request $request |
158
|
|
|
*/ |
159
|
|
|
public function searchTicketAction(Request $request) |
160
|
|
|
{ |
161
|
|
|
$event = $this->eventRepo->getNextEvent()->getOrThrow(new AccessDeniedHttpException('No event.')); |
162
|
|
|
// Do not allow checkins on the wrong day |
163
|
|
|
/* @var $event Event */ |
164
|
|
|
$now = new Carbon(); |
165
|
|
|
$start = Carbon::createFromTimestamp($event->getStart()->getTimestamp()); |
166
|
|
|
$start->setTime(0, 0, 0); |
167
|
|
|
$end = clone $start; |
168
|
|
|
$end->setTime(23, 59, 59); |
169
|
|
|
$day = $now->between($start, $end) ? Ticket::DAY_SATURDAY : Ticket::DAY_SUNDAY; |
170
|
|
|
|
171
|
|
|
$tickets = array_map(function (Ticket $ticket) { |
172
|
|
|
return array( |
173
|
|
|
'code' => $ticket->getCode(), |
174
|
|
|
'email' => $ticket->getEmail(), |
175
|
|
|
'name' => $ticket->getName(), |
176
|
|
|
'checkedIn' => $ticket->isCheckedIn(), |
177
|
|
|
'day' => $ticket->getDay(), |
178
|
|
|
'id' => $ticket->getId(), |
179
|
|
|
); |
180
|
|
|
}, $this->ticketRepo->searchTickets($event, $day, $request->get('q'))); |
181
|
|
|
$data = array('items' => $tickets); |
182
|
|
|
$response = new Response(json_encode($data)); |
183
|
|
|
$response->setCharset('utf-8'); |
184
|
|
|
$response->headers->set('Content-Type', 'application/json'); |
185
|
|
|
return $response; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
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.