SellTickets   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
c 1
b 0
f 0
dl 0
loc 78
rs 10
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A authorize() 0 3 1
B rules() 0 61 9
1
<?php
2
3
namespace App\Http\Requests;
4
5
use Illuminate\Foundation\Http\FormRequest;
6
use App\PriceCategory;
7
8
class SellTickets extends FormRequest
9
{
10
    /**
11
     * Determine if the user is authorized to make this request.
12
     *
13
     * @return bool
14
     */
15
    public function authorize()
16
    {
17
        return $this->user()->hasPermission('SELL_TICKETS');
18
    }
19
20
    /**
21
     * Get the validation rules that apply to the request.
22
     *
23
     * @return array
24
     */
25
    public function rules()
26
    {
27
        return [
28
            'action' => [
29
                'required',
30
                'in:paid,reserved,free'
31
            ],
32
            'tickets' => [
33
                'required',
34
                'array',
35
                function ($attribute, $value, $fail) {
36
                    foreach ($value as $category => $count) {
37
                        if (!PriceCategory::find($category)) {
38
                            $fail('Please select only offered price categories!');
39
                        }
40
                    }
41
42
                    $ticketSum = array_sum($value);
43
                    if ($ticketSum === 0) {
44
                        $fail('Please select at least one or up to 8 tickets!');
45
                    }
46
                    $event = $this->route('event');
47
                    if ($event->freeTickets() < $ticketSum) {
48
                        $fail('There are not as many tickets as you chose left. Please only choose a valid amount of tickets!');
49
                    }
50
                }
51
            ],
52
            // CustomerData is always required unless the action is sell --> then no contact information is required
53
            'customer-name' => [
54
                'required_if:action,free,reserved',
55
                'max:255'
56
            ],
57
            'selected-seats' => [
58
                'sometimes',
59
                'required',
60
                'array',
61
                function ($attribute, $value, $fail) {
62
                    // We do not check if the amount of selected seats, because we already do this in the checks
63
                    // for the amount of tickets. Since the amount of tickets and seats
64
                    // has to be equal, the previous check implicits that the seats are
65
                    // free by the amount.
66
                    $seatsSum = count($value);
67
                    $event = $this->route('event');
68
69
                    // Check if number of tickets and seats is equal
70
                    $numberOfTickets = array_sum($this->validator->getData()['tickets']);
71
                    if ($seatsSum != $numberOfTickets) {
72
                        $fail('Please select as much seats as tickets!');
73
                    }
74
75
                    // Check if the seatIds are in the range of 1 - event's seats amount
76
                    $validSeats = array_filter($value, function ($arrItem) use ($event) {
77
                        return $arrItem > 0 && $arrItem <= $event->seatMap->seats;
78
                    });
79
                    if (count($validSeats) != $seatsSum) {
80
                        $fail('Please only select seats displayed on the map!');
81
                    }
82
83
                    // Check if the selected seats are still free
84
                    if (!$event->areSeatsFree($value)) {
85
                        $fail('There are not as many tickets as you chose left. Please only choose a valid amount of tickets!');
86
                    }
87
                }
88
            ]
89
        ];
90
    }
91
}
92