| Conditions | 4 |
| Paths | 24 |
| Total Lines | 54 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 138 | public function testTicket(Event $event, Request $request) |
||
| 139 | { |
||
| 140 | // Wrap dummy data creation in a transaction in order |
||
| 141 | // to not actually store it in the production database. |
||
| 142 | // |
||
| 143 | // We have to use eloquent models and cannot use factories, |
||
| 144 | // because factories are not available on prod installations. |
||
| 145 | DB::beginTransaction(); |
||
| 146 | $now = now(); |
||
| 147 | |||
| 148 | $purchase = new Purchase(); |
||
| 149 | $purchase->state = 'paid'; |
||
| 150 | $purchase->state_updated = $now; |
||
| 151 | $purchase->random_id = Str::random(20); |
||
| 152 | $purchase->payment_secret = Str::random(20); |
||
| 153 | $purchase->customer_id = Auth::user()->id; |
||
| 154 | $purchase->vendor_id = Auth::user()->id; |
||
| 155 | $purchase->payment_id = 'dummy-reference'; |
||
| 156 | $purchase->save(); |
||
| 157 | |||
| 158 | $backupPriceCategory = PriceCategory::create([ |
||
| 159 | 'name' => 'StandardPrice', |
||
| 160 | 'price' => 40, |
||
| 161 | 'description' => 'Default Standard pricing' |
||
| 162 | ]); |
||
| 163 | |||
| 164 | for($i = 0; $i < 8; $i++) { |
||
| 165 | $ticket = new Ticket(); |
||
| 166 | $ticket->random_id = Str::random(20); |
||
| 167 | $ticket->seat_number = $i + 1000; |
||
| 168 | $ticket->event_id = $event->id; |
||
| 169 | $ticket->purchase_id = $purchase->id; |
||
| 170 | // If the event already has a pricelist attached use the first item of it. Else use the backup price category |
||
| 171 | $ticket->price_category_id = $event->priceList->priceCategories ? $event->priceList->priceCategories[0]->id : $backupPriceCategory->id; |
||
| 172 | $ticket->state = 'consumed'; |
||
| 173 | $ticket->save(); |
||
| 174 | } |
||
| 175 | try { |
||
| 176 | $html2pdf = new HTML2PDF('P', 'A4', 'de', true, 'UTF-8', 0); |
||
| 177 | $html2pdf->pdf->SetDisplayMode('fullpage'); |
||
| 178 | $html2pdf->pdf->SetAuthor(config('app.name')); |
||
| 179 | $html2pdf->pdf->SetTitle('Purchase #' . $purchase->id); |
||
| 180 | |||
| 181 | // Generate pdf-content by passing the tickets to the view |
||
| 182 | $content = view('pdfs.ticket-v2', ['tickets' => $purchase->tickets])->render(); |
||
| 183 | $html2pdf->writeHTML($content); |
||
| 184 | |||
| 185 | $html2pdf->output('tickets-' . $purchase->id . '.pdf'); |
||
| 186 | } catch (Html2PdfException $e) { |
||
| 187 | $html2pdf->clean(); |
||
| 188 | DB::rollBack(); |
||
| 189 | return redirect()->route('ticket.purchase', ['purchase' => $purchase])->with('state', $e->getMessage()); |
||
| 190 | } |
||
| 191 | DB::rollBack(); |
||
| 192 | } |
||
| 194 |