Conditions | 9 |
Paths | 1 |
Total Lines | 59 |
Code Lines | 35 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | } |
||
99 |