ConciergeController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 13
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 13
loc 13
rs 9.4285
cc 1
eloc 11
nc 1
nop 5
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(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
43
        EventRepository $eventRepo,
44
        TicketRepository $ticketRepo,
45
        CommandBus $commandBus,
46
        FormFactoryInterface $formFactory,
47
        RouterInterface $router)
48
    {
49
        $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...
50
        $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...
51
        $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...
52
        $this->formFactory = $formFactory;
0 ignored issues
show
Bug introduced by
The property formFactory 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...
53
        $this->router      = $router;
0 ignored issues
show
Bug introduced by
The property router 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...
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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $day can also be of type string. However, the property $day is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
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.'));
0 ignored issues
show
Unused Code introduced by
$event is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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