Passed
Push — master ( 26caeb...0e28f7 )
by Thomas
11:18
created

BookController::exportCode()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 79
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 62
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 79
rs 8.829

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\Controllers;
4
5
use App\Models\Book;
6
use App\Models\Student;
7
use Carbon\Carbon;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\DB;
10
11
class BookController extends Controller
12
{
13
    public function store(Request $request)
14
    {
15
        $request->validate([
16
            'book_id' => 'required',
17
            'student_id' => 'required',
18
        ]);
19
20
        $student = Student::find($request->student_id);
0 ignored issues
show
Bug introduced by
The property student_id does not seem to exist on Illuminate\Http\Request.
Loading history...
21
22
        $student->books()->attach(Book::find($request->book_id), [
0 ignored issues
show
Bug introduced by
The property book_id does not seem to exist on Illuminate\Http\Request.
Loading history...
Bug introduced by
The method books() does not exist on Illuminate\Database\Eloquent\Collection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

22
        $student->/** @scrutinizer ignore-call */ 
23
                  books()->attach(Book::find($request->book_id), [

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
23
            'code' => $request->code,
0 ignored issues
show
Bug introduced by
The property code does not seem to exist on Illuminate\Http\Request.
Loading history...
24
            'status_id' => $request->status_id,
0 ignored issues
show
Bug introduced by
The property status_id does not seem to exist on Illuminate\Http\Request.
Loading history...
25
            'expiry_date' => $request->expiry_date,
0 ignored issues
show
Bug introduced by
The property expiry_date does not seem to exist on Illuminate\Http\Request.
Loading history...
26
        ]);
27
28
        return redirect()->back();
29
    }
30
31
    public function update(Request $request)
32
    {
33
        $request->validate([
34
            'book_student_id' => 'required',
35
        ]);
36
37
        DB::table('book_student')->where('id', $request->book_student_id)->update([
0 ignored issues
show
Bug introduced by
The property book_student_id does not seem to exist on Illuminate\Http\Request.
Loading history...
38
            'status_id' => $request->status,
0 ignored issues
show
Bug introduced by
The property status does not seem to exist on Illuminate\Http\Request.
Loading history...
39
            'code' => $request->code,
0 ignored issues
show
Bug introduced by
The property code does not seem to exist on Illuminate\Http\Request.
Loading history...
40
            'expiry_date' => $request->expiry_date,
0 ignored issues
show
Bug introduced by
The property expiry_date does not seem to exist on Illuminate\Http\Request.
Loading history...
41
        ]);
42
43
        return Student::find(DB::table('book_student')->where('id', $request->book_student_id)->first()->student_id)->books;
0 ignored issues
show
Bug introduced by
The property books does not seem to exist on Illuminate\Database\Eloquent\Collection.
Loading history...
44
    }
45
46
    public function destroy(Request $request)
47
    {
48
        $student_id = DB::table('book_student')->where('id', $request->book_student_id)->first()->student_id;
0 ignored issues
show
Bug introduced by
The property book_student_id does not seem to exist on Illuminate\Http\Request.
Loading history...
49
50
        DB::table('book_student')->where('id', $request->book_student_id)->delete();
51
52
        return Student::find($student_id)->books;
0 ignored issues
show
Bug introduced by
The property books does not seem to exist on Illuminate\Database\Eloquent\Collection.
Loading history...
53
    }
54
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);
0 ignored issues
show
Bug introduced by
The property book_student_id does not seem to exist on Illuminate\Http\Request.
Loading history...
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;
0 ignored issues
show
Bug introduced by
The property firstname does not seem to exist on Illuminate\Database\Eloquent\Collection.
Loading history...
Bug introduced by
The property lastname does not seem to exist on Illuminate\Database\Eloquent\Collection.
Loading history...
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
    }
135
}
136