TicketForm   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A getEvent() 0 3 1
A handleTicketForm() 0 21 3
1
<?php
2
/**
3
 * TicketForm.php
4
 *
5
 * @author Bram de Leeuw
6
 * Date: 10/03/17
7
 */
8
9
namespace Broarm\EventTickets;
10
11
use CalendarEvent;
12
use DataList;
13
use FieldList;
14
use Form;
15
use FormAction;
16
use RequiredFields;
17
18
/**
19
 * Class TicketForm
20
 *
21
 * @package Broarm\EventTickets
22
 */
23
class TicketForm extends FormStep
24
{
25
    /**
26
     * @var DataList
27
     */
28
    protected $tickets;
29
30
    /**
31
     * @var CalendarEvent
32
     */
33
    protected $event;
34
35
    public function __construct($controller, $name, DataList $tickets = null, CalendarEvent $event = null)
36
    {
37
        $this->event = $event;
38
        $fields = FieldList::create(
39
            TicketsField::create('Tickets', '', $this->tickets = $tickets)
40
        );
41
42
        $actions = FieldList::create(
43
            FormAction::create('handleTicketForm', _t('TicketForm.MAKE_RESERVATION', 'Make reservation'))
44
                ->setDisabled($event->getAvailability() === 0)
45
        );
46
47
        $requiredFields = RequiredFields::create(array('Tickets'));
48
49
        parent::__construct($controller, $name, $fields, $actions, $requiredFields);
50
    }
51
52
    /**
53
     * Get the attached reservation
54
     *
55
     * @return CalendarEvent
56
     */
57
    public function getEvent() {
58
        return $this->event;
59
    }
60
61
    /**
62
     * Handle the ticket form registration
63
     *
64
     * @param array      $data
65
     * @param TicketForm $form
66
     *
67
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be \SS_HTTPResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
68
     */
69
    public function handleTicketForm(array $data, TicketForm $form)
70
    {
71
        $reservation = ReservationSession::start($this->getEvent());
72
73
        foreach ($data['Tickets'] as $ticketID => $ticketData) {
74
            for ($i = 0; $i < $ticketData['Amount']; $i++) {
75
                $attendee = Attendee::create();
76
                $attendee->TicketID = $ticketID;
77
                $attendee->ReservationID = $reservation->ID;
78
                $attendee->EventID = $reservation->EventID;
79
                $attendee->write();
80
                $reservation->Attendees()->add($attendee);
81
            }
82
        }
83
84
        $reservation->calculateTotal();
85
        $reservation->write();
86
87
        $this->extend('beforeNextStep', $data, $form, $reservation);
88
        return $this->nextStep();
89
    }
90
}