ReservationForm   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 28 4
B goToNextStep() 0 44 8
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
use SS_HTTPResponse;
15
use ValidationException;
16
17
/**
18
 * Class ReservationForm
19
 *
20
 * @package Broarm\EventTickets
21
 */
22
class ReservationForm extends FormStep
23
{
24
    /**
25
     * Set this to true if you want to require credentials for all attendees
26
     *
27
     * @config
28
     * @var bool
29
     */
30
    private static $require_all_attendees = false;
31
32
    public function __construct($controller, $name, Reservation $reservation = null)
33
    {
34
        $requiredFields = array();
35
        $fields = FieldList::create();
36
        if ($this->reservation = $reservation) {
37
            // Ask details about created attendees
38
            foreach ($reservation->Attendees() as $index => $attendee) {
39
                // The main reservation information is the first field
40
                $main = $index === 0;
41
                // Set required to true for all attendees or for the first only
42
                $required = self::config()->get('require_all_attendees')
43
                    ? self::config()->get('require_all_attendees')
44
                    : $main;
45
46
                $fields->add($field = AttendeeField::create($attendee, $main, $required));
47
                $requiredFields = array_merge($requiredFields, $field->getRequiredFields());
48
            }
49
        }
50
51
        $actions = FieldList::create(
52
            FormAction::create('goToNextStep', _t('ReservationForm.PAYMENT', 'Continue to payment'))
53
        );
54
55
        $required = new RequiredFields($requiredFields);
56
57
        parent::__construct($controller, $name, $fields, $actions, $required);
58
        $this->extend('updateForm');
59
    }
60
61
    /**
62
     * Finish the registration and continue to checkout
63
     *
64
     * @param array $data
65
     * @param ReservationForm $form
66
     * @return bool|SS_HTTPResponse
67
     * @throws ValidationException
68
     */
69
    public function goToNextStep(array $data, ReservationForm $form)
70
    {
71
        $reservation = $form->getReservation();
72
        foreach ($data['Attendee'] as $attendeeID => $attendeeData) {
73
            /** @var Attendee $attendee */
74
            $attendee = Attendee::get()->byID($attendeeID);
75
76
            // populate the attendees
77
            foreach ($attendeeData as $field => $value) {
78
                // Array value means we have a composite field.
79
                // This is used by the email field, main attendee to check if we have a correct mail address set.
80
                // todo, should be moved somewhere in the field classes.
81
                if (is_array($value)) {
82
                    if (count(array_unique($value)) !== 1) {
83
                        $form->addErrorMessage($field, 'Make sure your email address is spelled correctly', 'bad');
84
                        return $form->getController()->redirectBack();
85
                    } else {
86
                        $value = array_shift($value);
87
                    }
88
                }
89
90
                if (is_int($field)) {
91
                    $attendee->Fields()->add($field, array('Value' => $value));
92
                } else {
93
                    $attendee->setField($field, $value);
94
                }
95
            }
96
97
            $attendee->write();
98
99
            // Set the main contact
100
            if (isset($attendeeData['Main']) && (bool)$attendeeData['Main']) {
101
                $reservation->setMainContact($attendeeID);
102
            }
103
        }
104
105
        // add the tax modifier
106
        $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...
107
        $reservation->calculateTotal();
108
        $reservation->write();
109
110
        $this->extend('beforeNextStep', $data, $form);
111
        return $this->nextStep();
112
    }
113
}
114