| Conditions | 15 |
| Paths | 147 |
| Total Lines | 110 |
| Code Lines | 66 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 32 | public function startPayment(PayTickets $request) |
||
| 33 | { |
||
| 34 | // Only allow requests for sessions with existing customer data |
||
| 35 | if (!session()->has('customerData')) { |
||
| 36 | return redirect()->route('ts.customerData'); |
||
| 37 | } |
||
| 38 | $cData = session('customerData'); |
||
| 39 | |||
| 40 | // Start a transaction for inserting all session data into database |
||
| 41 | // and generating a purchase. |
||
| 42 | // Checks at the end validate if tickets are still free/available |
||
| 43 | DB::beginTransaction(); |
||
| 44 | |||
| 45 | // Retrieve selected event and refresh it by loading all related data |
||
| 46 | // from database |
||
| 47 | $event = session('event'); |
||
| 48 | $event->refresh(); |
||
| 49 | |||
| 50 | $tickets = session('tickets'); |
||
| 51 | $ticketSum = array_sum($tickets); |
||
| 52 | if ($event->freeTickets() < $ticketSum) { |
||
| 53 | // End transaction |
||
| 54 | DB::rollback(); |
||
| 55 | // Empty session data so user must select new tickets |
||
| 56 | $request->session()->forget('tickets'); |
||
| 57 | // Redirect user to select a valid amount of tickets |
||
| 58 | return redirect()->route('ts.seatmap') |
||
| 59 | ->with('status', 'There are not as many tickets as you chose left. Please only choose a lesser amount of tickets!'); |
||
| 60 | } |
||
| 61 | |||
| 62 | if ($event->seatMap->layout !== null) { |
||
| 63 | // Check if seats are still free and not booked by a concurrent transaction in the meantime |
||
| 64 | $seats = session('seats'); |
||
| 65 | if (!$event->areSeatsFree($seats)) { |
||
| 66 | // End transaction |
||
| 67 | DB::rollback(); |
||
| 68 | // Empty session data in order to select new seats |
||
| 69 | $request->session()->forget('seats'); |
||
| 70 | // redirect user to select a new set of seats |
||
| 71 | return redirect()->route('ts.seatmap') |
||
| 72 | ->with('status', 'Not all of your selected seats are still free. Please choose a new set of seats!'); |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | try { |
||
| 77 | $customer = User::where('email', $cData['email'])->firstOrFail(); |
||
| 78 | } catch (ModelNotFoundException $e) { |
||
| 79 | $customer = User::create([ |
||
| 80 | 'name' => $cData['name'], |
||
| 81 | 'email' => $cData['email'], |
||
| 82 | 'password' => '', |
||
| 83 | ]); |
||
| 84 | |||
| 85 | if (array_key_exists('newsletter', $cData) && $cData['newsletter'] == 'true') { |
||
|
|
|||
| 86 | $customer->roles()->attach(Role::where('name', 'NewsletterReceiver')->first()); |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | $prices = $event->priceList->categories; |
||
| 91 | $prices = $prices->keyBy('id'); |
||
| 92 | |||
| 93 | $purchase = new Purchase(); |
||
| 94 | $purchase->generateSecrets(); |
||
| 95 | $purchase->customer_id = $customer->id; |
||
| 96 | // If someone already has bought tickets with the same email address |
||
| 97 | // but with a different name, we store the custom name to the purchase. |
||
| 98 | if ($cData['name'] != $customer->name) { |
||
| 99 | $purchase->customer_name = $cData['name']; |
||
| 100 | } |
||
| 101 | $purchase->vendor_id = User::where('name', $request->validated()['paymethod'])->first()->id; |
||
| 102 | $purchase->save(); |
||
| 103 | |||
| 104 | |||
| 105 | $seatsIndex = 0; |
||
| 106 | foreach ($tickets as $ticketCategory => $ticketCount) { |
||
| 107 | for ($i = 0; $i < $ticketCount; $i++) { |
||
| 108 | Ticket::create([ |
||
| 109 | 'random_id' => Str::random(32), |
||
| 110 | 'seat_number' => isset($seats) ? $seats[$seatsIndex] : 0, |
||
| 111 | 'purchase_id' => $purchase->id, |
||
| 112 | 'event_id' => $event->id, |
||
| 113 | 'price_category_id' => $prices[$ticketCategory]->id |
||
| 114 | ]); |
||
| 115 | $seatsIndex++; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | switch($request->validated()['paymethod']) |
||
| 120 | { |
||
| 121 | case 'Klarna': |
||
| 122 | $paymentProvider = resolve('App\PaymentProvider\Klarna'); |
||
| 123 | break; |
||
| 124 | case 'PayPal': |
||
| 125 | $paymentProvider = resolve('App\PaymentProvider\PayPal'); |
||
| 126 | break; |
||
| 127 | case 'Mollie': |
||
| 128 | $paymentProvider = resolve('App\PaymentProvider\Mollie'); |
||
| 129 | break; |
||
| 130 | default: |
||
| 131 | DB::rollBack(); |
||
| 132 | return redirect()->route('ts.overview') |
||
| 133 | ->with('status', 'The selected PaymentProvider is not supported!'); |
||
| 134 | } |
||
| 135 | |||
| 136 | DB::commit(); |
||
| 137 | // forget all session data that has been written into database |
||
| 138 | $request->session()->forget(['customerData', 'event', 'tickets', 'seats']); |
||
| 139 | |||
| 140 | return redirect()->away( |
||
| 141 | $paymentProvider->getPaymentUrl($purchase) |
||
| 142 | ); |
||
| 145 |