CheckinController::checkinAction()   B
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 21
nc 5
nop 3
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\RegistrationRepository;
13
use BCRM\BackendBundle\Entity\Event\Ticket;
14
use BCRM\BackendBundle\Entity\Event\TicketRepository;
15
use BCRM\BackendBundle\Service\Concierge\CheckinCommand;
16
use BCRM\WebBundle\Content\ContentReader;
17
use BCRM\WebBundle\Exception\AccesDeniedHttpException;
18
use BCRM\WebBundle\Exception\BadRequestException;
19
use Carbon\Carbon;
20
use LiteCQRS\Bus\CommandBus;
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
22
use Symfony\Component\HttpFoundation\RedirectResponse;
23
use Symfony\Component\HttpFoundation\Request;
24
use Symfony\Component\HttpFoundation\Response;
25
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
26
use Symfony\Component\Routing\RouterInterface;
27
use Symfony\Component\Validator\Constraints\DateTime;
28
29
/**
30
 * Manages event checkins.
31
 */
32
class CheckinController
33
{
34
    public function __construct(
35
        EventRepository $eventRepo,
36
        TicketRepository $ticketRepo,
37
        RegistrationRepository $registrationRepo,
38
        CommandBus $commandBus
39
    )
40
    {
41
        $this->ticketRepo       = $ticketRepo;
0 ignored issues
show
Bug introduced by
The property ticketRepo does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
42
        $this->eventRepo        = $eventRepo;
0 ignored issues
show
Bug introduced by
The property eventRepo does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
43
        $this->registrationRepo = $registrationRepo;
0 ignored issues
show
Bug introduced by
The property registrationRepo does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
44
        $this->commandBus       = $commandBus;
0 ignored issues
show
Bug introduced by
The property commandBus does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
45
    }
46
47
    /**
48
     * @param Request $request
49
     * @param         $id
50
     * @param         $code
51
     *
52
     * @return array|RedirectResponse
53
     * @Template()
54
     */
55
    public function checkinAction(Request $request, $id, $code)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
56
    {
57
        /* @var $ticket Ticket */
58
        $ticket = $this->ticketRepo->getTicketByIdAndCode($id, $code)->getOrThrow(new NotFoundHttpException('Unknown ticket.'));
59
60
        // Do not allow double checkins
61
        if ($ticket->isCheckedIn()) {
62
            throw new BadRequestException('Already checked in!');
63
        }
64
65
        // Do not allow checkins on the wrong day
66
        /* @var $event Event */
67
        $event = $this->eventRepo->getNextEvent()->getOrThrow(new AccesDeniedHttpException('No event.'));
68
        $now   = new Carbon();
69
        $start = Carbon::createFromTimestamp($event->getStart()->getTimestamp());
70
        $start->setTime(0, 0, 0);
71
        if ($ticket->isSunday()) {
72
            $start->modify('+1day');
73
        }
74
        $end = clone $start;
75
        $end->setTime(23, 59, 59);
76
        if (!$now->between($start, $end)) {
77
            throw new BadRequestException('Wrong day!');
78
        }
79
80
        $registrationOption = $this->registrationRepo->getRegistrationForEmail($ticket->getEvent(), $ticket->getEmail());
81
82
83
        // Record checkin
84
        $command         = new CheckinCommand();
85
        $command->ticket = $ticket;
86
        $this->commandBus->handle($command);
87
88
        return array(
89
            'ticket'       => $ticket,
90
            'registration' => $registrationOption->getOrElse(null),
91
        );
92
    }
93
}
94