Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 40 | class Event implements LoggerAwareInterface |
||
| 41 | { |
||
| 42 | /** |
||
| 43 | * @var \LiteCQRS\Bus\CommandBus |
||
| 44 | */ |
||
| 45 | private $commandBus; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var \Symfony\Component\Routing\RouterInterface |
||
| 49 | */ |
||
| 50 | private $router; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var \BCRM\BackendBundle\Entity\Event\RegistrationRepository |
||
| 54 | */ |
||
| 55 | private $registrationRepo; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var \LiteCQRS\Bus\EventMessageBus |
||
| 59 | */ |
||
| 60 | private $eventMessageBus; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var \BCRM\BackendBundle\Entity\Event\UnregistrationRepository |
||
| 64 | */ |
||
| 65 | private $unregistrationRepo; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var \BCRM\BackendBundle\Entity\Event\TicketRepository |
||
| 69 | */ |
||
| 70 | private $ticketRepo; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var LoggerInterface |
||
| 74 | */ |
||
| 75 | private $logger; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param CommandBus $commandBus |
||
| 79 | * @param RouterInterface $router |
||
| 80 | */ |
||
| 81 | public function __construct(CommandBus $commandBus, EventMessageBus $eventMessageBus, RouterInterface $router, RegistrationRepository $registrationRepo, UnregistrationRepository $unregistrationRepo, TicketRepository $ticketRepo) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Sets a logger instance on the object |
||
| 93 | * |
||
| 94 | * @param LoggerInterface $logger |
||
| 95 | * |
||
| 96 | * @return null |
||
| 97 | */ |
||
| 98 | public function setLogger(LoggerInterface $logger) |
||
| 99 | { |
||
| 100 | $this->logger = $logger; |
||
| 101 | } |
||
| 102 | |||
| 103 | |||
| 104 | public function register(RegisterCommand $command) |
||
| 105 | { |
||
| 106 | $createRegistrationCommand = new CreateResourceCommand(); |
||
| 107 | $createRegistrationCommand->class = '\BCRM\BackendBundle\Entity\Event\Registration'; |
||
| 108 | $createRegistrationCommand->data = array( |
||
| 109 | 'event' => $command->event, |
||
| 110 | 'email' => $command->email, |
||
| 111 | 'name' => $command->name, |
||
| 112 | 'twitter' => $command->twitter, |
||
| 113 | 'saturday' => $command->saturday, |
||
| 114 | 'sunday' => $command->sunday, |
||
| 115 | 'food' => $command->food, |
||
| 116 | 'tags' => $command->tags, |
||
| 117 | 'type' => $command->type, |
||
| 118 | 'participantList' => $command->participantList, |
||
| 119 | 'uuid' => $command->uuid, |
||
| 120 | 'paymentMethod' => $command->payment, |
||
| 121 | 'donation' => (int)$command->donation |
||
| 122 | ); |
||
| 123 | $this->commandBus->handle($createRegistrationCommand); |
||
| 124 | } |
||
| 125 | |||
| 126 | public function sendPaymentNotificationMail(SendPaymentNotificationMailCommand $command) |
||
| 127 | { |
||
| 128 | $updateCommand = new UpdateResourceCommand(); |
||
| 129 | $updateCommand->class = '\BCRM\BackendBundle\Entity\Event\Registration'; |
||
| 130 | $updateCommand->id = $command->registration->getId(); |
||
| 131 | $updateCommand->data = array('paymentNotified' => new \DateTime()); |
||
| 132 | $this->commandBus->handle($updateCommand); |
||
| 133 | |||
| 134 | $emailCommand = new SendTemplateMailCommand(); |
||
| 135 | $emailCommand->email = $command->registration->getEmail(); |
||
| 136 | $emailCommand->template = 'RegistrationPayment'; |
||
| 137 | $emailCommand->templateData = array( |
||
| 138 | 'registration' => $command->registration, |
||
| 139 | 'payment_link' => rtrim($command->schemeAndHost, '/') . $this->router->generate('bcrmweb_registration_payment', array('id' => $command->registration->getUuid())) |
||
| 140 | ); |
||
| 141 | $this->commandBus->handle($emailCommand); |
||
| 142 | } |
||
| 143 | |||
| 144 | public function createTicket(CreateTicketCommand $command) |
||
| 145 | { |
||
| 146 | $sr = new SecureRandom(); |
||
| 147 | $code = ''; |
||
| 148 | $max = 6; |
||
| 149 | while (strlen($code) < $max) { |
||
| 150 | $seq = preg_replace('/[^A-Z0-9]/', '', $sr->nextBytes(256)); |
||
| 151 | for ($i = 0; $i < strlen($seq) && strlen($code) < $max; $i++) { |
||
| 152 | $code .= $seq[$i]; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | $createCommand = new CreateResourceCommand(); |
||
| 156 | $createCommand->class = '\BCRM\BackendBundle\Entity\Event\Ticket'; |
||
| 157 | $createCommand->data = array( |
||
| 158 | 'event' => $command->event, |
||
| 159 | 'email' => $command->registration->getEmail(), |
||
| 160 | 'name' => $command->registration->getName(), |
||
| 161 | 'day' => $command->day, |
||
| 162 | 'type' => $command->registration->getType(), |
||
| 163 | 'code' => $code, |
||
| 164 | ); |
||
| 165 | $this->commandBus->handle($createCommand); |
||
| 166 | } |
||
| 167 | |||
| 168 | public function sendTicketMail(SendTicketMailCommand $command) |
||
| 169 | { |
||
| 170 | $qrCode = new QrCode(); |
||
| 171 | $qrCode->setText( |
||
| 172 | rtrim($command->schemeAndHost, '/') . $this->router->generate( |
||
| 173 | 'bcrmweb_event_checkin', |
||
| 174 | array('id' => $command->ticket->getId(), 'code' => $command->ticket->getCode()) |
||
| 175 | ) |
||
| 176 | ); |
||
| 177 | |||
| 178 | $qrCode->setSize(300); |
||
| 179 | $qrCode->setPadding(10); |
||
| 180 | $qrfile = tempnam(sys_get_temp_dir(), 'qrcode-') . '.png'; |
||
| 181 | $qrCode->render($qrfile); |
||
| 182 | |||
| 183 | $emailCommand = new SendTemplateMailCommand(); |
||
| 184 | $emailCommand->email = $command->ticket->getEmail(); |
||
| 185 | $emailCommand->template = 'Ticket'; |
||
| 186 | $emailCommand->templateData = array( |
||
| 187 | 'ticket' => $command->ticket, |
||
| 188 | 'event' => $command->event, |
||
| 189 | 'cancel_link' => rtrim($command->schemeAndHost, '/') . $this->router->generate( |
||
| 190 | 'bcrmweb_event_cancel_ticket', |
||
| 191 | array('id' => $command->ticket->getId(), 'code' => $command->ticket->getCode()) |
||
| 192 | ) |
||
| 193 | ); |
||
| 194 | $registrationOptional = $this->registrationRepo->getRegistrationForEmail($command->event, $command->ticket->getEmail()); |
||
|
|
|||
| 195 | if ($registrationOptional->isDefined()) { |
||
| 196 | $emailCommand->templateData['registration'] = $registrationOptional->get(); |
||
| 197 | } |
||
| 198 | $emailCommand->image = $qrfile; |
||
| 199 | $emailCommand->format = 'text/html'; |
||
| 200 | $this->commandBus->handle($emailCommand); |
||
| 201 | |||
| 202 | $event = new TicketMailSentEvent(); |
||
| 203 | $event->ticket = $command->ticket; |
||
| 204 | $this->eventMessageBus->publish($event); |
||
| 205 | } |
||
| 206 | |||
| 207 | public function unregister(UnregisterCommand $command) |
||
| 220 | |||
| 221 | public function sendUnregistrationConfirmationMail(SendUnregistrationConfirmationMailCommand $command) |
||
| 222 | { |
||
| 223 | $sr = new SecureRandom(); |
||
| 224 | $key = sha1($sr->nextBytes(256), false); |
||
| 225 | $updateCommand = new UpdateResourceCommand(); |
||
| 240 | |||
| 241 | View Code Duplication | public function confirmUnregistration(ConfirmUnregistrationCommand $command) |
|
| 249 | |||
| 250 | public function unregisterTicket(UnregisterTicketCommand $command) |
||
| 251 | { |
||
| 252 | // Create a new registration matching the unregistration |
||
| 253 | $registration = $this->registrationRepo->getRegistrationForEmail($command->event, $command->unregistration->getEmail()); |
||
| 254 | if ($registration->isDefined()) { |
||
| 255 | /** @var Registration $r */ |
||
| 256 | $r = $registration->get(); |
||
| 257 | $registrationData = array( |
||
| 258 | 'event' => $command->event, |
||
| 259 | 'email' => $command->unregistration->getEmail(), |
||
| 260 | 'name' => $r->getName(), |
||
| 261 | 'twitter' => $r->getTwitter(), |
||
| 262 | 'food' => $r->getFood(), |
||
| 263 | 'tags' => $r->getTags(), |
||
| 264 | 'confirmed' => 1, |
||
| 265 | 'saturday' => $r->getSaturday(), |
||
| 266 | 'sunday' => $r->getSunday(), |
||
| 267 | 'participantList' => $r->isParticipantList(), |
||
| 268 | 'uuid' => $r->getUuid(), |
||
| 269 | 'donation' => $r->getDonation(), |
||
| 270 | 'payment' => $r->getPayment(), |
||
| 271 | 'paymentMethod' => $r->getPaymentMethod(), |
||
| 272 | ); |
||
| 273 | if ($command->unregistration->getSaturday()) { |
||
| 274 | $registrationData['saturday'] = false; |
||
| 275 | } |
||
| 276 | if ($command->unregistration->getSunday()) { |
||
| 277 | $registrationData['sunday'] = false; |
||
| 278 | } |
||
| 279 | $createRegistrationCommand = new CreateResourceCommand(); |
||
| 280 | $createRegistrationCommand->class = '\BCRM\BackendBundle\Entity\Event\Registration'; |
||
| 281 | $createRegistrationCommand->data = $registrationData; |
||
| 282 | $this->commandBus->handle($createRegistrationCommand); |
||
| 283 | } |
||
| 284 | |||
| 285 | // Delete tickets |
||
| 286 | foreach ($this->ticketRepo->getTicketsForEmail($command->event, $command->unregistration->getEmail()) as $ticket) { |
||
| 287 | if ( |
||
| 288 | ($ticket->isSaturday() && $command->unregistration->getSaturday()) |
||
| 289 | || ($ticket->isSunday() && $command->unregistration->getSunday()) |
||
| 290 | ) { |
||
| 291 | $deleteTicketCommand = new DeleteResourceCommand(); |
||
| 292 | $deleteTicketCommand->class = '\BCRM\BackendBundle\Entity\Event\Ticket'; |
||
| 293 | $deleteTicketCommand->id = $ticket->getId(); |
||
| 294 | $this->commandBus->handle($deleteTicketCommand); |
||
| 295 | |||
| 296 | $event = new TicketDeletedEvent(); |
||
| 297 | $event->ticket = $ticket; |
||
| 298 | $this->eventMessageBus->publish($event); |
||
| 299 | } |
||
| 300 | } |
||
| 301 | |||
| 302 | // Mark unregistration as processed |
||
| 303 | $updateCommand = new UpdateResourceCommand(); |
||
| 304 | $updateCommand->class = '\BCRM\BackendBundle\Entity\Event\Unregistration'; |
||
| 305 | $updateCommand->id = $command->unregistration->getId(); |
||
| 306 | $updateCommand->data = array('processed' => true); |
||
| 307 | $this->commandBus->handle($updateCommand); |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Once a payment has been verified, find the registration it belongs to and mark it as paid |
||
| 312 | * so the tickets can be assigned. |
||
| 313 | * |
||
| 314 | * @param PaymentVerifiedEvent $event |
||
| 315 | */ |
||
| 316 | public function onPaymentVerified(PaymentVerifiedEvent $event) |
||
| 333 | |||
| 334 | public function payRegistration(PayRegistrationCommand $command) |
||
| 347 | } |
||
| 348 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: