TestimonialRepository::getAll()   B
last analyzed

Complexity

Conditions 7
Paths 34

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 24.8582

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 21
nc 34
nop 2
dl 0
loc 34
ccs 6
cts 21
cp 0.2857
crap 24.8582
rs 8.6506
c 1
b 0
f 0
1
<?php
2
3
namespace App\Repositories;
4
5
use App\Models\Testimonial;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\Support\Facades\Config;
8
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
9
10
class TestimonialRepository implements TestimonialRepositoryInterface
11
{
12
    /**
13
     * Get all Testimonials.
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 = Testimonial::orderBy('created_at', '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
            if (!empty($searchParameters['status'])) {
43
                $query->currentStatus($searchParameters['status']);
44
            }
45
        }
46
47 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...
48 1
            $results = $query->paginate($recordsPerPage)->withQueryString();
49
        } else {
50
            $results = $query->get();
51
        }
52
53 1
        return $results;
54
    }
55
56
    /**
57
     * Get Testimonial by id
58
     *
59
     * @param int $testimonialId
60
     * @return Testimonial
61
     */
62 2
    public function getById(int $testimonialId): Testimonial
63
    {
64 2
        return Testimonial::findOrFail($testimonialId);
65
    }
66
67
    /**
68
     * Store Testimonial
69
     *
70
     * @param array $data
71
     * @return Testimonial
72
     */
73 1
    public function store(array $data): Testimonial
74
    {
75 1
        $testimonial = new Testimonial();
76 1
        $testimonial = self::assignDataAttributes($testimonial, $data);
0 ignored issues
show
Bug Best Practice introduced by
The method App\Repositories\Testimo...: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
        $testimonial = self::assignDataAttributes($testimonial, $data);
Loading history...
77
78 1
        $testimonial->save();
79
80
        // When created by a guest from frontend set to unpublished
81 1
        $status = Auth::guest() ? 'unpublished' : 'published';
82 1
        $testimonial->setStatus($status);
83
84 1
        return $testimonial->fresh();
85
    }
86
87
    /**
88
     * Update Testimonial
89
     *
90
     * @param array $data
91
     * @param int $id
92
     * @return Testimonial
93
     */
94 1
    public function update(array $data, int $id): Testimonial
95
    {
96 1
        $testimonial = $this->getById($id);
97 1
        $testimonial = self::assignDataAttributes($testimonial, $data);
0 ignored issues
show
Bug Best Practice introduced by
The method App\Repositories\Testimo...: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

97
        /** @scrutinizer ignore-call */ 
98
        $testimonial = self::assignDataAttributes($testimonial, $data);
Loading history...
98
99 1
        $testimonial->update();
100
101 1
        $status = (isset($data['status'])) ? 'published' : 'unpublished';
102 1
        if ($testimonial->publishingStatus() != $status) {
103 1
            $testimonial->setStatus($status);
104
        }
105
106 1
        return $testimonial;
107
    }
108
109
    /**
110
     * Delete Testimonial
111
     *
112
     * @param int $id
113
     * @return void
114
     */
115 1
    public function delete(int $id): void
116
    {
117 1
        Testimonial::destroy($id);
118 1
    }
119
120
    /**
121
     * Assign the attributes of the data array to the object
122
     *
123
     * @param \App\Models\Testimonial $testimonial
124
     * @param array $data
125
     *
126
     * @return \App\Models\Testimonial
127
     */
128 2
    public function assignDataAttributes(Testimonial $testimonial, array $data): Testimonial
129
    {
130 2
        $testimonial->feedback = $data['feedback'] ?? null;
131 2
        $testimonial->feedback_short = $data['feedback_short'] ?? null;
132 2
        $testimonial->name = $data['name'] ?? null;
133 2
        $testimonial->surname = $data['surname'] ?? null;
134 2
        $testimonial->profession = $data['profession'] ?? null;
135
        $testimonial->country_id = $data['country_id'] ?? null;
0 ignored issues
show
Bug introduced by
The property country_id does not exist on App\Models\Testimonial. Did you mean country?
Loading history...
136 2
137 2
        $testimonial->publish_agreement =  isset($data['publish_agreement']) ? 1 : 0;
138
        $testimonial->personal_data_agreement =  isset($data['personal_data_agreement']) ? 1 : 0;
139
140
        //$testimonial->publish_agreement =  ($data['publish_agreement'] == 'on') ? 1 : 0;
141
        //$testimonial->personal_data_agreement =  ($data['personal_data_agreement'] == 'on') ? 1 : 0;
142
143 2
        // Translations
144 2
        foreach (LaravelLocalization::getSupportedLocales() as $countryCode => $countryAvTrans) {
145 2
            if ($countryCode != Config::get('app.fallback_locale')) {
146 2
                $testimonial->setTranslation('feedback', $countryCode, $data['feedback_' . $countryCode] ?? null);
147
                $testimonial->setTranslation('feedback_short', $countryCode, $data['feedback_short_' . $countryCode] ?? null);
148
                $testimonial->setTranslation('profession', $countryCode, $data['profession_' . $countryCode] ?? null);
149
            }
150 2
        }
151
152
        return $testimonial;
153
    }
154
}
155