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

BookController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 82
c 1
b 0
f 0
dl 0
loc 123
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A store() 0 16 1
A update() 0 13 1
B exportCode() 0 79 1
A destroy() 0 7 1
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