Completed
Push — master ( 86bf52...1063cc )
by Bram
03:31
created

CheckInForm   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 6
dl 0
loc 113
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
C doCheckIn() 0 78 8
1
<?php
2
/**
3
 * CheckInForm.php
4
 *
5
 * @author Bram de Leeuw
6
 * Date: 07/04/17
7
 */
8
9
namespace Broarm\EventTickets;
10
11
use FieldList;
12
use Form;
13
use FormAction;
14
use RequiredFields;
15
use TextField;
16
17
class CheckInForm extends Form
18
{
19
    /**
20
     * Allow people to check out
21
     *
22
     * @config
23
     * @var bool
24
     */
25
    private static $allow_checkout = false;
0 ignored issues
show
Unused Code introduced by
The property $allow_checkout is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
26
27
    public function __construct($controller, $name = 'CheckInForm')
28
    {
29
        $fields = FieldList::create(
30
            TextField::create('TicketCode', _t('CheckInForm.TICKET_CODE', 'Ticket code'))
31
        );
32
33
        $actions = FieldList::create(
34
            FormAction::create('doCheckIn', _t('CheckInForm.CHECK_IN', 'Check in'))
35
        );
36
37
        $required = new RequiredFields(array('TicketCode'));
38
39
        parent::__construct($controller, $name, $fields, $actions, $required);
40
    }
41
42
    /**
43
     * Do the check in, if all checks pass return a success
44
     *
45
     * @param             $data
46
     * @param CheckInForm $form
47
     *
48
     * @return bool
49
     */
50
    public function doCheckIn($data, CheckInForm $form)
51
    {
52
        /** @var CheckInController $controller */
53
        $controller = $form->getController();
54
55
        // Check if the ticket code is set
56
        if (!isset($data['TicketCode'])) {
57
            $form->addErrorMessage('TicketCode', _t(
58
                'CheckInForm.NO_CODE',
59
                'Please submit a ticket code'
60
            ), 'error');
61
62
            $controller->redirect("{$controller->Link()}?success=-3");
63
            return false;
64
        }
65
66
        // Check if the event has registered attendees
67
        if (!$controller->Attendees()->exists()) {
68
            $form->addErrorMessage('TicketCode', _t(
69
                'CheckInForm.NO_ATTENDEES',
70
                'This event has no registered attendees'
71
            ), 'error');
72
73
            $controller->redirect("{$controller->Link()}?success=-2");
74
            return false;
75
        }
76
77
        // Check if the ticket is found on the current event
78
        /** @var Attendee $attendee */
79
        if (!$attendee = $controller->Attendees()->find('TicketCode', $data['TicketCode'])) {
80
            $form->addErrorMessage('TicketCode', _t(
81
                'CheckInForm.CODE_NOT_FOUND',
82
                'The given ticket is not found on this event'
83
            ), 'error');
84
85
            $controller->redirect("{$controller->Link()}?success=-1");
86
            return false;
87
        }
88
89
        // Check if the ticket is already used
90
        if ($attendee->CheckedIn && !(bool)self::config()->get('allow_checkout')) {
91
            $form->addErrorMessage('TicketCode', _t(
92
                'CheckInForm.ALREADY_CHECKED_IN',
93
                'This ticket is already checked in'
94
            ), 'error');
95
96
            $controller->redirect("{$controller->Link()}?success=0");
97
            return false;
98
        } else {
99
            if ((bool)$attendee->CheckedIn && (bool)self::config()->get('allow_checkout')) {
100
                $attendee->CheckedIn = false;
101
                $message = 'CHECK_OUT_SUCCESS';
102
                $messageType = 'notice';
103
            } else {
104
                $attendee->CheckedIn = true;
105
                $message = 'SUCCESS';
106
                $messageType = 'good';
107
            }
108
109
            $attendee->write();
110
            $messages = array(
111
                'SUCCESS' => 'The ticket is valid. {name} has a {ticket} ticket with number {number}',
112
                'CHECK_OUT_SUCCESS' => 'The ticket with number {number} is checked out.'
113
            );
114
115
            $form->sessionMessage(_t(
116
                "CheckInForm.$message",
117
                $messages[$message],
118
                null, array(
0 ignored issues
show
Documentation introduced by
array('name' => $attende... $attendee->TicketCode) is of type array<string,?,{"name":"...ket":"?","number":"?"}>, but the function expects a string.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
119
                    'name' => $attendee->getName(),
120
                    'ticket' => $attendee->Ticket()->Title,
121
                    'number' => $attendee->TicketCode
122
                )
123
            ), $messageType, false);
124
            $controller->redirect("{$controller->Link()}?success=1");
125
            return true;
126
        }
127
    }
128
129
}