Passed
Pull Request — 2.x (#729)
by Antonio Carlos
06:06
created

HasRevisions::getRevisionModel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace A17\Twill\Models\Behaviors;
4
5
trait HasRevisions
6
{
7 35
    public function revisions()
8
    {
9 35
        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 33
    public function scopeMine($query)
13
    {
14 33
        return $query->whereHas('revisions', function ($query) {
15 33
            $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 33
        });
17
    }
18
19 8
    public function revisionsArray()
20
    {
21 8
        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 35
    protected function getRevisionModel()
31
    {
32 35
        $revision = config('twill.namespace') . "\Models\Revisions\\" . class_basename($this) . "Revision";
33
34 35
        if (@class_exists($revision))
35
        {
36 31
            return $revision;
37
        }
38
39 4
        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