SellTickets::rules()   B
last analyzed

Complexity

Conditions 9
Paths 1

Size

Total Lines 61
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 36
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 61
rs 8.0555

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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