CanFollow::isFollowing()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
3
/*
4
 * This file is part of questocat/laravel-rally package.
5
 *
6
 * (c) questocat <[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 Questocat\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