Completed
Push — master ( dcf8d4...d88694 )
by Bram
01:52 queued 11s
created

ReservationForm   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 10
dl 0
loc 80
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 28 4
B goToNextStep() 0 32 6
1
<?php
2
/**
3
 * ReservationForm.php
4
 *
5
 * @author Bram de Leeuw
6
 * Date: 13/03/17
7
 */
8
9
namespace Broarm\EventTickets;
10
11
use FieldList;
12
use FormAction;
13
use RequiredFields;
14
15
/**
16
 * Class ReservationForm
17
 *
18
 * @package Broarm\EventTickets
19
 */
20
class ReservationForm extends FormStep
21
{
22
    /**
23
     * Set this to true if you want to require credentials for all attendees
24
     *
25
     * @config
26
     * @var bool
27
     */
28
    private static $require_all_attendees = false;
29
30
    public function __construct($controller, $name, Reservation $reservation = null)
31
    {
32
        $requiredFields = array();
33
        $fields = FieldList::create();
34
        if ($this->reservation = $reservation) {
35
            // Ask details about created attendees
36
            foreach ($reservation->Attendees() as $index => $attendee) {
37
                // The main reservation information is the first field
38
                $main = $index === 0;
39
                // Set required to true for all attendees or for the first only
40
                $required = self::config()->get('require_all_attendees')
41
                    ? self::config()->get('require_all_attendees')
42
                    : $main;
43
44
                $fields->add($field = AttendeeField::create($attendee, $main, $required));
45
                $requiredFields = array_merge($requiredFields, $field->getRequiredFields());
46
            }
47
        }
48
49
        $actions = FieldList::create(
50
            FormAction::create('goToNextStep', _t('ReservationForm.PAYMENT', 'Continue to payment'))
51
        );
52
53
        $required = new RequiredFields($requiredFields);
54
55
        parent::__construct($controller, $name, $fields, $actions, $required);
56
        $this->extend('updateForm');
57
    }
58
59
    /**
60
     * Finish the registration and continue to checkout
61
     *
62
     * @param array           $data
63
     * @param ReservationForm $form
64
     *
65
     * @return \SS_HTTPResponse
66
     */
67
    public function goToNextStep(array $data, ReservationForm $form)
68
    {
69
        $reservation = $form->getReservation();
70
        foreach ($data['Attendee'] as $attendeeID => $attendeeData) {
71
            /** @var Attendee $attendee */
72
            $attendee = Attendee::get()->byID($attendeeID);
73
74
            // populate the attendees
75
            foreach ($attendeeData as $field => $value) {
76
                if (is_int($field)) {
77
                    $attendee->Fields()->add($field, array('Value' => $value));
78
                } else {
79
                    $attendee->setField($field, $value);
80
                }
81
            }
82
83
            $attendee->write();
84
85
            // Set the main contact
86
            if (isset($attendeeData['Main']) && (bool)$attendeeData['Main']) {
87
                $reservation->setMainContact($attendeeID);
88
            }
89
        }
90
91
        // add the tax modifier
92
        $reservation->PriceModifiers()->add(TaxModifier::findOrMake($reservation));
0 ignored issues
show
Bug introduced by
It seems like \Broarm\EventTickets\Tax...indOrMake($reservation) can be null; however, add() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
93
        $reservation->calculateTotal();
94
        $reservation->write();
95
96
        $this->extend('beforeNextStep', $data, $form);
97
        return $this->nextStep();
98
    }
99
}
100