SelectTickets   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 39
c 2
b 0
f 0
dl 0
loc 89
rs 10
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A authorize() 0 3 1
B rules() 0 59 9
A messages() 0 4 1
1
<?php
2
3
namespace App\Http\Requests;
4
5
use Illuminate\Foundation\Http\FormRequest;
6
7
class SelectTickets extends FormRequest
8
{
9
    /**
10
     * Determine if the user is authorized to make this request.
11
     *
12
     * @return bool Always true since this is a public function
13
     */
14
    public function authorize()
15
    {
16
        return true;
17
    }
18
19
    /**
20
     * Get the validation rules that apply to the request.
21
     *
22
     * @return array
23
     */
24
    public function rules()
25
    {
26
        return [
27
            'tickets' => [
28
                'required',
29
                'array',
30
                function ($attribute, $value, $fail) {
31
                    // First check if the selected categories are linked
32
                    // to the selected event. Else attackers could select
33
                    // cheaper categories for their tickets
34
                    $event = $this->route('event');
35
                    $allowedPriceCategories = $event->priceList->categories->pluck('id')->toArray();
36
                    $selectedCategoryIds = array_keys( $value );
37
                    $notAllowedCategories = array_diff($selectedCategoryIds, $allowedPriceCategories);
38
                    if(!empty($notAllowedCategories)) {
39
                        $fail('Please only select offered price categories!');
40
                    }
41
                    // Check if the ticket sum is between 1 and 8
42
                    $ticketSum = array_sum($value);
43
                    if ($ticketSum === 0) {
44
                        $fail('Please select at least one or up to 8 tickets!');
45
                    } elseif ($ticketSum > 8) {
46
                        $fail('Please select not more than 8 tickets!');
47
                    }
48
                    $event = $this->route('event');
49
                    if ($event->freeTickets() < $ticketSum) {
50
                        $fail('There are not as many tickets as you chose left. Please only choose a valid amount of tickets!');
51
                    }
52
                }
53
            ],
54
            'selected-seats' => [
55
                'sometimes',
56
                'required',
57
                'array',
58
                function ($attribute, $value, $fail) {
59
                    // We do not check if the amount of selected seats, because we already do this in the checks
60
                    // for the amount of tickets. Since the amount of tickets and seats
61
                    // has to be equal, the previous check implicits that the seats are
62
                    // free by the amount.
63
                    $seatsSum = count($value);
64
                    $event = $this->route('event');
65
66
                    // Check if number of tickets and seats is equal
67
                    $numberOfTickets = array_sum($this->validator->getData()['tickets']);
68
                    if ($seatsSum != $numberOfTickets) {
69
                        $fail('Please select as much seats as tickets!');
70
                    }
71
72
                    // Check if the seatIds are in the range of 1 - event's seats amount
73
                    $validSeats = array_filter($value, function ($arrItem) use ($event) {
74
                        return $arrItem > 0 && $arrItem <= $event->seatMap->seats;
75
                    });
76
                    if (count($validSeats) != $seatsSum) {
77
                        $fail('Please only select seats displayed on the map!');
78
                    }
79
80
                    // Check if the selected seats are still free
81
                    if (!$event->areSeatsFree($value)) {
82
                        $fail('There are not as many tickets as you chose left. Please only choose a valid amount of tickets!');
83
                    }
84
                }
85
            ]
86
        ];
87
    }
88
89
    /**
90
     * Overwrite error messages displayed on validition violation
91
     */
92
    public function messages()
93
    {
94
        return [
95
            'tickets.required' => 'Please select at least one or up to 8 tickets!',
96
        ];
97
    }
98
}
99