Test Failed
Push — master ( addd19...5a71e5 )
by Zhengchao
03:17
created

CanFollow   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 60%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 5
c 6
b 0
f 0
lcom 1
cbo 2
dl 0
loc 66
ccs 6
cts 10
cp 0.6
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A follow() 0 4 1
A unfollow() 0 4 1
A isFollowing() 0 4 1
A toggleFollow() 0 4 1
A following() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of emanci/laravel-rally package.
5
 *
6
 * (c) emanci <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Emanci\Rally\Traits;
13
14
trait CanFollow
15
{
16
    /**
17
     * Follow a followable entity.
18
     *
19
     * @param int|array|\Illuminate\Database\Eloquent\Model $followable
20
     * @param string                                        $className
21
     *
22
     * @return array
23
     */
24 7
    public function follow($followable, $className = __CLASS__)
25
    {
26 7
        return $this->following($className)->sync($followable, false);
27
    }
28
29
    /**
30
     * Unfollow a followable entity.
31
     *
32
     * @param int|array|\Illuminate\Database\Eloquent\Model $followable
33
     * @param string                                        $className
34
     *
35
     * @return bool|null
36
     */
37
    public function unfollow($followable, $className = __CLASS__)
38
    {
39
        return $this->following($className)->detach($followable);
40
    }
41
42
    /**
43
     * Check if entity is following given entity.
44
     *
45
     * @param int|\Illuminate\Database\Eloquent\Model $followable
46
     * @param string                                  $className
47
     *
48
     * @return bool
49
     */
50
    public function isFollowing($followable, $className = __CLASS__)
51
    {
52
        return $this->following($className)->get()->contains($followable);
53
    }
54
55
    /**
56
     * Toggle follow a followable or followables.
57
     *
58
     * @param int|array|\Illuminate\Database\Eloquent\Model $followable
59
     * @param string                                        $className
60
     *
61
     * @return array
62
     */
63 1
    public function toggleFollow($followable, $className = __CLASS__)
64
    {
65 1
        return $this->following($className)->toggle($followable);
66
    }
67
68
    /**
69
     * Return entity following.
70
     *
71
     * @param string $className
72
     *
73
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
74
     */
75 8
    public function following($className = __CLASS__)
76
    {
77 8
        return $this->morphedByMany($className, config('rally.followable_prefix'), config('rally.followers_table'), config('rally.follower_prefix').'_id', config('rally.followable_prefix').'_id')->withTimestamps();
0 ignored issues
show
Bug introduced by
It seems like morphedByMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
78
    }
79
}
80