Test Failed
Push — main ( 97a8b8...676696 )
by Davide
11:43 queued 11s
created

GlossaryRepository::getById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
3
namespace App\Repositories;
4
5
use App\Models\Glossary;
6
use Illuminate\Support\Facades\Config;
7
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
8
9
class GlossaryRepository implements GlossaryRepositoryInterface
10
{
11
12
    /**
13
     * Get all Glossary terms.
14
     *
15
     * @param int|null $recordsPerPage
16
     * @param array|null $searchParameters
17
     *
18
     * @return \Illuminate\Support\Collection|\Illuminate\Contracts\Pagination\LengthAwarePaginator
19
     */
20 1
    public function getAll(int $recordsPerPage = null, array $searchParameters = null)
21
    {
22 1
        $query = Glossary::orderBy('created_at', 'desc');
23
24 1
        if (!is_null($searchParameters)) {
25
            if (!empty($searchParameters['term'])) {
26
                $query->where(
27
                    'term',
28
                    'like',
29
                    '%' . $searchParameters['term'] . '%'
30
                );
31
            }
32
            if (!is_null($searchParameters['is_published'])) {
33
                $query->where('is_published', $searchParameters['is_published']);
34
            }
35
        }
36
37 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...
38 1
            $results = $query->paginate($recordsPerPage)->withQueryString();
39
        } else {
40
            $results = $query->get();
41
        }
42
43 1
        return $results;
44
    }
45
46
    /**
47
     * Get all Glossary terms with their variants.
48
     *
49
     * @param int|null $recordsPerPage
50
     * @param array|null $searchParameters
51
     *
52 2
     * @return \Illuminate\Support\Collection|\Illuminate\Contracts\Pagination\LengthAwarePaginator
53
     */
54 2
    public function getAllWithVariants()
55
    {
56
        return Glossary::with('variants')
57
            ->where('is_published', 1)
58
            ->get();
59
    }
60
61
    /**
62
     * Get Glossary term by id
63 1
     *
64
     * @param int $id
65 1
     * @return Glossary
66 1
     */
67
    public function getById(int $id): Glossary
68 1
    {
69
        return Glossary::findOrFail($id);
70 1
    }
71
72 1
    /**
73
     * Get glossary term by slug
74
     *
75
     * @param  string  $glossarySlug
76
     * @return Glossary
77
     */
78
    public function getBySlug(string $glossarySlug): Glossary
79
    {
80
        return Glossary::where('slug', $glossarySlug)->first();
81
    }
82 1
83
    /**
84 1
     * Store Glossary term
85 1
     *
86
     * @param array $data
87 1
     * @return Glossary
88
     */
89 1
    public function store(array $data): Glossary
90 1
    {
91 1
        $glossary = new Glossary();
92
        $glossary = self::assignDataAttributes($glossary, $data);
0 ignored issues
show
Bug Best Practice introduced by
The method App\Repositories\Glossar...: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

92
        /** @scrutinizer ignore-call */ 
93
        $glossary = self::assignDataAttributes($glossary, $data);
Loading history...
93
94 1
        $glossary->save();
95
96
        return $glossary->fresh();
97
    }
98
99
    /**
100
     * Update Glossary term
101
     *
102
     * @param array $data
103 1
     * @param int $id
104
     * @return Glossary
105 1
     */
106 1
    public function update(array $data, int $id): Glossary
107
    {
108
        $glossary = $this->getById($id);
109
        $glossary = self::assignDataAttributes($glossary, $data);
0 ignored issues
show
Bug Best Practice introduced by
The method App\Repositories\Glossar...: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

109
        /** @scrutinizer ignore-call */ 
110
        $glossary = self::assignDataAttributes($glossary, $data);
Loading history...
110
111
        $glossary->update();
112
113
        return $glossary;
114
    }
115
116 2
    /**
117
     * Delete Glossary term
118 2
     *
119 2
     * @param int $id
120 2
     * @return void
121
     */
122
    public function delete(int $id): void
123 2
    {
124 2
        Glossary::destroy($id);
125 2
    }
126 2
127 2
    /**
128
     * Assign the attributes of the data array to the object
129
     *
130
     * @param \App\Models\Glossary $glossary
131 2
     * @param array $data
132
     *
133
     * @return \App\Models\Glossary
134
     */
135
    public function assignDataAttributes(Glossary $glossary, array $data): Glossary
136
    {
137
        $glossary->term = $data['term'];
138
        $glossary->definition = $data['definition'];
139
        $glossary->body = $data['body'];
140
        $glossary->question_type = $data['question_type'];
141
        $glossary->is_published = (isset($data['is_published'])) ? 1 : 0;
142
143
        // Translations
144
        foreach (LaravelLocalization::getSupportedLocales() as $countryCode => $countryAvTrans) {
145
            if ($countryCode != Config::get('app.fallback_locale')) {
146
                $glossary->setTranslation('term', $countryCode, $data['term_' . $countryCode] ?? null);
147
                $glossary->setTranslation('definition', $countryCode, $data['definition_' . $countryCode] ?? null);
148
                $glossary->setTranslation('body', $countryCode, $data['body_' . $countryCode] ?? null);
149
            }
150
        }
151
152
        return $glossary;
153
    }
154
}
155