| Conditions | 1 |
| Paths | 1 |
| Total Lines | 79 |
| Code Lines | 62 |
| 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 |
||
| 55 | public function exportCode(Request $request) |
||
| 56 | { |
||
| 57 | $request->validate([ |
||
| 58 | 'book_student_id' => 'required', |
||
| 59 | ]); |
||
| 60 | |||
| 61 | $book = DB::table('book_student')->find($request->book_student_id); |
||
| 62 | $student = Student::find($book->student_id); |
||
| 63 | |||
| 64 | $image = imagecreatefrompng(storage_path('afloja/vignette_livre.png')); |
||
| 65 | $black = imagecolorallocate($image, 0, 0, 0); |
||
| 66 | $font = storage_path('afloja/fonts/OpenSans-ExtraBold.ttf'); |
||
| 67 | |||
| 68 | // First line of text |
||
| 69 | $font_size = 42; |
||
| 70 | $text = 'Alumno:'; |
||
| 71 | $angle = 0; |
||
| 72 | $width = imagesx($image); |
||
| 73 | $centerX = $width / 2; |
||
| 74 | [$left, , $right] = imageftbbox($font_size, $angle, $font, $text); |
||
| 75 | $left_offset = ($right - $left) / 2; |
||
| 76 | $x = $centerX - $left_offset; |
||
| 77 | imagettftext($image, $font_size, $angle, $x, 450, $black, $font, $text); |
||
| 78 | |||
| 79 | // Name of student |
||
| 80 | $text = $student->lastname.' '.$student->firstname; |
||
| 81 | $angle = 0; |
||
| 82 | $width = imagesx($image); |
||
| 83 | $centerX = $width / 2; |
||
| 84 | [$left, , $right] = imageftbbox($font_size, $angle, $font, $text); |
||
| 85 | $left_offset = ($right - $left) / 2; |
||
| 86 | $x = $centerX - $left_offset; |
||
| 87 | imagettftext($image, $font_size, $angle, $x, 525, $black, $font, $text); |
||
| 88 | |||
| 89 | // First line of text |
||
| 90 | $text = 'Código Premium:'; |
||
| 91 | $angle = 0; |
||
| 92 | $width = imagesx($image); |
||
| 93 | $centerX = $width / 2; |
||
| 94 | [$left, , $right] = imageftbbox($font_size, $angle, $font, $text); |
||
| 95 | $left_offset = ($right - $left) / 2; |
||
| 96 | $x = $centerX - $left_offset; |
||
| 97 | imagettftext($image, $font_size, $angle, $x, 620, $black, $font, $text); |
||
| 98 | |||
| 99 | // Code |
||
| 100 | $text = $book->code; |
||
| 101 | $angle = 0; |
||
| 102 | $width = imagesx($image); |
||
| 103 | $centerX = $width / 2; |
||
| 104 | [$left, , $right] = imageftbbox($font_size, $angle, $font, $text); |
||
| 105 | $left_offset = ($right - $left) / 2; |
||
| 106 | $x = $centerX - $left_offset; |
||
| 107 | imagettftext($image, $font_size, $angle, $x, 690, $black, $font, $text); |
||
| 108 | |||
| 109 | // last line of text |
||
| 110 | $font = storage_path('afloja/fonts/OpenSans-Light.ttf'); |
||
| 111 | $font_size = 30; |
||
| 112 | $expiry_date = Carbon::parse($book->expiry_date)->format('j/m/Y'); |
||
| 113 | $text = "(Valido hasta el $expiry_date)"; |
||
| 114 | $angle = 0; |
||
| 115 | $width = imagesx($image); |
||
| 116 | $centerX = $width / 2; |
||
| 117 | [$left, , $right] = imageftbbox($font_size, $angle, $font, $text); |
||
| 118 | $left_offset = ($right - $left) / 2; |
||
| 119 | $x = $centerX - $left_offset; |
||
| 120 | imagettftext($image, $font_size, $angle, $x, 755, $black, $font, $text); |
||
| 121 | |||
| 122 | // Using imagepng() results in clearer text compared with imagejpeg() |
||
| 123 | header('Content-Type: image/jpg'); |
||
| 124 | header('Content-Description: File Transfer'); |
||
| 125 | header('Content-Type: application/octet-stream'); |
||
| 126 | header('Content-Transfer-Encoding: binary'); |
||
| 127 | header('Expires: 0'); |
||
| 128 | header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); |
||
| 129 | header('Pragma: public'); |
||
| 130 | ob_start(); |
||
| 131 | imagepng($image); |
||
| 132 | header('Content-Disposition: attachment; filename=vignette.jpg'); |
||
| 133 | imagedestroy($image); |
||
| 134 | } |
||
| 136 |