Test Failed
Push — main ( 646093...e66c61 )
by Davide
16:46
created

QuoteRepository::getAll()   B

Complexity

Conditions 7
Paths 34

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 22.6944

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 7
eloc 21
c 4
b 0
f 0
nc 34
nop 2
dl 0
loc 34
rs 8.6506
ccs 6
cts 19
cp 0.3158
crap 22.6944
1
<?php
2
3
namespace App\Repositories;
4
5
use App\Models\Quote;
6
use Illuminate\Support\Facades\Config;
7
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
8
9
class QuoteRepository implements QuoteRepositoryInterface
10
{
11
12
    /**
13
     * Get all Quote 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 = Quote::orderBy('created_at', 'desc');
23
24 1
        if (!is_null($searchParameters)) {
25
            if (!empty($searchParameters['author'])) {
26
                $query->where(
27
                    'author',
28
                    'like',
29
                    '%' . $searchParameters['author'] . '%'
30
                );
31
            }
32
            if (!empty($searchParameters['description'])) {
33
                $query->where(
34
                    'description',
35
                    'like',
36
                    '%' . $searchParameters['description'] . '%'
37
                );
38
            }
39
            if (!is_null($searchParameters['is_published'])) {
40
                $query->where('is_published', $searchParameters['is_published']);
41
            }
42
            if (!is_null($searchParameters['show_where'])) {
43
                $query->where('show_where', $searchParameters['show_where']);
44 1
            }
45 1
        }
46
47
        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...
48
            $results = $query->paginate($recordsPerPage)->withQueryString();
49
        } else {
50 1
            $results = $query->get();
51
        }
52
53
        return $results;
54
    }
55
56
    /**
57
     * Get Quote term by id
58
     *
59 2
     * @param int $id
60
     * @return Quote
61 2
     */
62
    public function getById(int $id): Quote
63
    {
64
        return Quote::findOrFail($id);
65
    }
66
67
    /**
68
     * Store Quote term
69
     *
70 1
     * @param array $data
71
     * @return Quote
72 1
     */
73 1
    public function store(array $data): Quote
74
    {
75 1
        $quote = new Quote();
76
        $quote = self::assignDataAttributes($quote, $data);
0 ignored issues
show
Bug Best Practice introduced by
The method App\Repositories\QuoteRe...: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

76
        /** @scrutinizer ignore-call */ 
77
        $quote = self::assignDataAttributes($quote, $data);
Loading history...
77 1
78
        $quote->save();
79
80
        return $quote->fresh();
81
    }
82
83
    /**
84
     * Update Quote term
85
     *
86
     * @param array $data
87 1
     * @param int $id
88
     * @return Quote
89 1
     */
90 1
    public function update(array $data, int $id): Quote
91
    {
92 1
        $quote = $this->getById($id);
93
        $quote = self::assignDataAttributes($quote, $data);
0 ignored issues
show
Bug Best Practice introduced by
The method App\Repositories\QuoteRe...: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

93
        /** @scrutinizer ignore-call */ 
94
        $quote = self::assignDataAttributes($quote, $data);
Loading history...
94 1
95
        $quote->update();
96
97
        return $quote;
98
    }
99
100
    /**
101
     * Delete Quote term
102
     *
103 1
     * @param int $id
104
     * @return void
105 1
     */
106 1
    public function delete(int $id): void
107
    {
108
        Quote::destroy($id);
109
    }
110
111
    /**
112
     * Assign the attributes of the data array to the object
113
     *
114
     * @param \App\Models\Quote $quote
115
     * @param array $data
116 2
     *
117
     * @return \App\Models\Quote
118 2
     */
119 2
    public function assignDataAttributes(Quote $quote, array $data): Quote
120
    {
121
        $quote->author = $data['author'];
122 2
        $quote->description = $data['description'];
123 2
        $quote->is_published = (isset($data['is_published'])) ? 1 : 0;
124 2
        $quote->show_where = $data['show_where'];
125
126
        // Translations
127
        foreach (LaravelLocalization::getSupportedLocales() as $countryCode => $countryAvTrans) {
128 2
            if ($countryCode != Config::get('app.fallback_locale')) {
129
                $quote->setTranslation('description', $countryCode, $data['description_' . $countryCode] ?? null);
130
            }
131
        }
132
133
        return $quote;
134
    }
135
}
136