Passed
Push — master ( 1438ee...6641ad )
by Quentin
06:42 queued 12s
created

HasRevisions::getRevisionModel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 10
ccs 0
cts 0
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace A17\Twill\Models\Behaviors;
4
5
trait HasRevisions
6
{
7 31
    public function revisions()
8
    {
9 31
        return $this->hasMany($this->getRevisionModel())->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($this->getRevisionModel())->orderBy('created_at', 'desc');
Loading history...
10
    }
11
12 31
    public function scopeMine($query)
13
    {
14
        return $query->whereHas('revisions', function ($query) {
15 31
            $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 31
        });
17
    }
18
19 8
    public function revisionsArray()
20
    {
21
        return $this->revisions->map(function ($revision) {
22
            return [
23 8
                'id' => $revision->id,
24 8
                'author' => $revision->user->name ?? 'Unknown',
25 8
                'datetime' => $revision->created_at->toIso8601String(),
26
            ];
27 8
        })->toArray();
28
    }
29
30
    protected function getRevisionModel()
31
    {
32
        $revision = config('twill.namespace') . "\Models\Revisions\\" . class_basename($this) . "Revision";
33
34
        if (@class_exists($revision))
35
        {
36
            return $revision;
37
        }
38
39
        return $this->getCapsuleRevisionClass(class_basename($this));
0 ignored issues
show
Bug introduced by
It seems like getCapsuleRevisionClass() 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

39
        return $this->/** @scrutinizer ignore-call */ getCapsuleRevisionClass(class_basename($this));
Loading history...
40
    }
41
}
42