CanLikeBehavior   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 53
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A likes() 0 3 1
A like() 0 15 2
A toggleLike() 0 3 2
A unlike() 0 16 2
A hasLiked() 0 6 2
1
<?php
2
3
/*
4
 * This file is part of the sebastian-kennedy/laravel-like.
5
 *
6
 * (c) SebastianKennedy <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled.
9
 */
10
11
namespace SebastianKennedy\LaravelLike\Behaviors;
12
13
use Illuminate\Database\Eloquent\Model;
14
15
trait CanLikeBehavior
16
{
17 3
    public function toggleLike(Model $model)
18
    {
19 3
        return $this->hasLiked($model) ? $this->unlike($model) : $this->like($model);
20
    }
21
22 12
    public function unlike(Model $model)
23
    {
24 12
        $relation = $model->likes()
25 12
            ->where(config('like.morph_many_id'), $model->getKey())
26 12
            ->where(config('like.morph_many_type'), $model->getMorphClass())
27 12
            ->where(config('like.foreign_key'), $this->getKey())
0 ignored issues
show
Bug introduced by
It seems like getKey() 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

27
            ->where(config('like.foreign_key'), $this->/** @scrutinizer ignore-call */ getKey())
Loading history...
28 12
            ->first();
29
30 12
        if ($relation) {
31 12
            $result = $relation->delete();
32 12
            $this->refresh();
0 ignored issues
show
Bug introduced by
It seems like refresh() 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

32
            $this->/** @scrutinizer ignore-call */ 
33
                   refresh();
Loading history...
33
34 12
            return $result;
35
        }
36
37 3
        return null;
38
    }
39
40 36
    public function like(Model $model)
41
    {
42 36
        if (!$this->hasLiked($model)) {
43 36
            $like = app(config('like.model'));
44 36
            $like->{config('like.foreign_key')} = $this->getKey();
45 36
            $like->{config('like.morph_many_id')} = $model->getKey();
46 36
            $like->{config('like.morph_many_type')} = $model->getMorphClass();
47
48 36
            $like = $this->likes()->save($like);
49 36
            $this->refresh();
50
51 36
            return $like;
52
        }
53
54 3
        return null;
55
    }
56
57 36
    public function hasLiked(Model $model)
58
    {
59 36
        return ($this->relationLoaded('likes') ? $this->likes : $this->likes())
0 ignored issues
show
Bug introduced by
It seems like relationLoaded() 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

59
        return ($this->/** @scrutinizer ignore-call */ relationLoaded('likes') ? $this->likes : $this->likes())
Loading history...
60 36
                ->where(config('like.morph_many_id'), $model->getKey())
61 36
                ->where(config('like.morph_many_type'), $model->getMorphClass())
62 36
                ->count() > 0;
63
    }
64
65 36
    public function likes()
66
    {
67 36
        return $this->hasMany(config('like.model'), config('like.foreign_key'), $this->getKeyName());
0 ignored issues
show
Bug introduced by
It seems like getKeyName() 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

67
        return $this->hasMany(config('like.model'), config('like.foreign_key'), $this->/** @scrutinizer ignore-call */ getKeyName());
Loading history...
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

67
        return $this->/** @scrutinizer ignore-call */ hasMany(config('like.model'), config('like.foreign_key'), $this->getKeyName());
Loading history...
68
    }
69
}
70