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

HasRevisions   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 35
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A scopeMine() 0 4 1
A revisionsArray() 0 9 1
A revisions() 0 3 1
A getRevisionModel() 0 10 2
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