Passed
Pull Request — 2.x (#586)
by Bekzat
04:25
created

HasRevisions::scopeMine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace A17\Twill\Models\Behaviors;
4
5
trait HasRevisions
6
{
7
    public function revisions()
8
    {
9
        return $this->hasMany(config('twill.namespace') . "\Models\Revisions\\" . class_basename($this) . "Revision")->orderBy('created_at', 'desc');
0 ignored issues
show
Bug introduced by
It seems like hasMany() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

9
        return $this->/** @scrutinizer ignore-call */ hasMany(config('twill.namespace') . "\Models\Revisions\\" . class_basename($this) . "Revision")->orderBy('created_at', 'desc');
Loading history...
10
    }
11
12
    public function scopeMine($query)
13
    {
14
        return $query->whereHas('revisions', function ($query) {
15
            $query->where('user_id', auth('twill_users')->user()->id);
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
16
        });
17
    }
18
19
    public function revisionsArray()
20
    {
21
        return $this->revisions->map(function ($revision) {
22
            return [
23
                'id' => $revision->id,
24
                'author' => $revision->user->name ?? 'Unknown',
25
                'datetime' => $revision->created_at->toIso8601String(),
26
            ];
27
        })->toArray();
28
    }
29
}
30