Test Setup Failed
Push — main ( 1f6960...c530ab )
by Davide
01:22 queued 12s
created

TeacherRepository::getById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Repositories;
4
5
use App\Models\Teacher;
6
use Carbon\Carbon;
7
use Illuminate\Support\Facades\Auth;
8
9
class TeacherRepository implements TeacherRepositoryInterface
10
{
11
12
    /**
13
     * Get all Teachers.
14
     *
15
     * @param int|null $recordsPerPage
16
     * @param array|null $searchParameters
17
     *
18
     * @return \App\Models\Teacher[]|\Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Database\Eloquent\Collection
19
     */
20 1
    public function getAll(int $recordsPerPage = null, array $searchParameters = null)
21
    {
22 1
        $query = Teacher::orderBy('name', 'desc');
23
24 1
        if (!is_null($searchParameters)) {
25
            if (!empty($searchParameters['name'])) {
26
                $query->where(
27
                    'name',
28
                    'like',
29
                    '%' . $searchParameters['name'] . '%'
30
                );
31
            }
32
            if (!empty($searchParameters['surname'])) {
33
                $query->where(
34
                    'surname',
35
                    'like',
36
                    '%' . $searchParameters['surname'] . '%'
37
                );
38
            }
39
            if (!empty($searchParameters['countryId'])) {
40
                $query->where('country_id', $searchParameters['countryId']);
41
            }
42
        }
43
44 1
        if ($recordsPerPage) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $recordsPerPage of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
45 1
            $results = $query->paginate($recordsPerPage)->withQueryString();
46
        } else {
47
            $results = $query->get();
48
        }
49
50 1
        return $results;
51
    }
52
53
    /**
54
     * Get Teacher by id
55
     *
56
     * @param int $id
57
     *
58
     * @return Teacher
59
     */
60 2
    public function getById(int $id): Teacher
61
    {
62 2
        return Teacher::findOrFail($id);
63
    }
64
65
    /**
66
     * Get Teacher by slug
67
     *
68
     * @param  string  $teacherSlug
69
     * @return Teacher
70
     */
71
    public function getBySlug(string $teacherSlug): Teacher
72 1
    {
73
        return Teacher::where('slug', $teacherSlug)->first();
74 1
    }
75 1
76
    /**
77
     * Store Teacher
78 1
     *
79
     * @param array $data
80 1
     *
81
     * @return Teacher
82 1
     */
83
    public function store(array $data): Teacher
84
    {
85
        $teacher = new Teacher();
86
        $teacher = self::assignDataAttributes($teacher, $data);
0 ignored issues
show
Bug Best Practice introduced by
The method App\Repositories\Teacher...:assignDataAttributes() is not static, but was called statically. ( Ignorable by Annotation )

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

86
        /** @scrutinizer ignore-call */ 
87
        $teacher = self::assignDataAttributes($teacher, $data);
Loading history...
87
88
        // Creator - Logged user id or 1 for factories
89
        $teacher->user_id = !is_null(Auth::id()) ? Auth::id() : 1;
0 ignored issues
show
Bug introduced by
The property user_id does not exist on App\Models\Teacher. Did you mean user?
Loading history...
90
91
        $teacher->save();
92
93 1
        return $teacher->fresh();
94
    }
95 1
96 1
    /**
97
     * Update Teacher
98 1
     *
99
     * @param array $data
100 1
     * @param int $id
101
     *
102
     * @return Teacher
103
     */
104
    public function update(array $data, int $id): Teacher
105
    {
106
        $teacher = $this->getById($id);
107
        $teacher = self::assignDataAttributes($teacher, $data);
0 ignored issues
show
Bug Best Practice introduced by
The method App\Repositories\Teacher...:assignDataAttributes() is not static, but was called statically. ( Ignorable by Annotation )

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

107
        /** @scrutinizer ignore-call */ 
108
        $teacher = self::assignDataAttributes($teacher, $data);
Loading history...
108
109 1
        $teacher->update();
110
111 1
        return $teacher;
112 1
    }
113
114
    /**
115
     * Delete Teacher
116
     *
117
     * @param int $id
118
     * @return void
119
     */
120
    public function delete(int $id): void
121
    {
122 2
        Teacher::destroy($id);
123
    }
124 2
125 2
    /**
126 2
     * Assign the attributes of the data array to the object
127 2
     *
128 2
     * @param \App\Models\Teacher $teacher
129 2
     * @param array $data
130 2
     *
131 2
     * @return \App\Models\Teacher
132 2
     */
133
    public function assignDataAttributes(Teacher $teacher, array $data): Teacher
134 2
    {
135
        $teacher->country_id = $data['country_id'] ?? null;
0 ignored issues
show
Bug introduced by
The property country_id does not exist on App\Models\Teacher. Did you mean country?
Loading history...
136
        $teacher->name = $data['name'];
137
        $teacher->surname = $data['surname'] ?? null;
138
        $teacher->bio = $data['bio'] ?? null;
139
        $teacher->year_starting_practice = $data['year_starting_practice'] ?? null;
140
        $teacher->year_starting_teach = $data['year_starting_teach'] ?? null;
141
        $teacher->significant_teachers = $data['significant_teachers'] ?? null;
142
        $teacher->website = $data['website'] ?? null;
143
        $teacher->facebook = $data['facebook'] ?? null;
144
145
        return $teacher;
146
    }
147
}
148