InsightRepository   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 83.78%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
c 1
b 0
f 0
dl 0
loc 89
rs 10
ccs 31
cts 37
cp 0.8378
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A update() 0 8 1
A delete() 0 3 1
A store() 0 10 1
A getAll() 0 26 5
A getById() 0 3 1
A assignDataAttributes() 0 7 2
A getLatest() 0 3 1
1
<?php
2
3
namespace App\Repositories;
4
5
use App\Models\Insight;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Facades\Config;
8
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
9
10
class InsightRepository implements InsightRepositoryInterface
11
{
12
    public function getAll(
13 1
        int $recordsPerPage = null,
14
        array $searchParameters = null
15
    ) {
16
        $query = Insight::orderBy('created_at', 'desc');
17
18 1
        if (!is_null($searchParameters)) {
19
            if (!empty($searchParameters['title'])) {
20 1
                $query->where(
21
                    'title',
22
                    'like',
23
                    '%' . $searchParameters['title'] . '%'
24
                );
25
            }
26
            if (!is_null($searchParameters['is_published'])) {
27
                $query->where('is_published', $searchParameters['is_published']);
28
            }
29
        }
30 1
31 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...
32
            $results = $query->paginate($recordsPerPage)->withQueryString();
33
        } else {
34
            $results = $query->get();
35
        }
36 1
37
        return $results;
38
    }
39 2
40
    public function getById($insightId)
41 2
    {
42
        return Insight::findOrFail($insightId);
43
    }
44 1
45
    public function store(array $data)
46 1
    {
47 1
        $insight = new Insight();
48
        $insight = self::assignDataAttributes($insight, $data);
0 ignored issues
show
Bug Best Practice introduced by
The method App\Repositories\Insight...: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

48
        /** @scrutinizer ignore-call */ 
49
        $insight = self::assignDataAttributes($insight, $data);
Loading history...
49 1
50
        $insight->save();
51 1
52 1
        $insight->tags()->sync($data['tag_ids'] ?? null);
53
54 1
        return $insight->fresh();
55
    }
56
57 1
    public function update(array $data, int $id)
58
    {
59 1
        $insight = $this->getById($id);
60 1
        $insight = self::assignDataAttributes($insight, $data);
0 ignored issues
show
Bug Best Practice introduced by
The method App\Repositories\Insight...: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

60
        /** @scrutinizer ignore-call */ 
61
        $insight = self::assignDataAttributes($insight, $data);
Loading history...
61
62 1
        $insight->update();
63
64 1
        return $insight;
65
    }
66
67 1
    public function delete(int $id)
68
    {
69 1
        Insight::destroy($id);
70 1
    }
71
72
    /**
73
     * Assign the attributes of the data array to the object
74
     *
75
     * @param \App\Models\Insight $insight
76
     * @param array $data
77
     *
78
     * @return \App\Models\Insight
79
     */
80 2
    public function assignDataAttributes(Insight $insight, array $data): Insight
81
    {
82 2
        $insight->title = $data['title'] ?? null;
83 2
        $insight->body = $data['body'] ?? null;
84
        $insight->is_published = (isset($data['is_published'])) ? 1 : 0;
85
86 2
        return $insight;
87 2
    }
88 2
89 2
    /**
90
     * Get the last 5 Insights.
91
     *
92
     * @param int $numberOfInsights
93 2
     *
94
     * @return Collection
95
     */
96
    public function getLatest(int $numberOfInsights): Collection
97
    {
98
        return Insight::orderBy('created_at', 'desc')->take($numberOfInsights)->get();
99
    }
100
101
}
102