Passed
Push — master ( 34de29...6cda2b )
by Carlos
01:35 queued 21s
created

Followable   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 53
c 1
b 0
f 0
dl 0
loc 166
rs 10
wmc 22

12 Methods

Rating   Name   Duplication   Size   Complexity  
A toggleFollow() 0 3 2
A rejectFollowRequestFrom() 0 3 1
A followers() 0 9 1
A isFollowedBy() 0 17 3
A follow() 0 9 3
A followings() 0 9 1
A hasRequestedToFollow() 0 17 3
A needsToApproveFollowRequests() 0 3 1
A acceptFollowRequestFrom() 0 3 1
A isFollowing() 0 17 3
A unfollow() 0 3 1
A areFollowingEachOther() 0 4 2
1
<?php
2
3
/*
4
 * This file is part of the overtrue/laravel-follow
5
 *
6
 * (c) overtrue <[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 Overtrue\LaravelFollow;
13
14
use Illuminate\Database\Eloquent\Model;
15
16
/**
17
 * Trait Followable.
18
 *
19
 * @property \Illuminate\Database\Eloquent\Collection $followings
20
 * @property \Illuminate\Database\Eloquent\Collection $followers
21
 */
22
trait Followable
23
{
24
    /**
25
     * @return bool
26
     */
27
    public function needsToApproveFollowRequests()
28
    {
29
        return false;
30
    }
31
32
    /**
33
     * @param \Illuminate\Database\Eloquent\Model|int $user
34
     *
35
     * @return array
36
     */
37
    public function follow($user)
38
    {
39
        $isPending = $user->needsToApproveFollowRequests() ?: false;
40
41
        $this->followings()->attach($user, [
42
            'accepted_at' => $isPending ? null : now()
43
        ]);
44
45
        return ['pending' => $isPending];
46
    }
47
48
    /**
49
     * @param \Illuminate\Database\Eloquent\Model|int $user
50
     */
51
    public function unfollow($user)
52
    {
53
        $this->followings()->detach($user);
54
    }
55
56
    /**
57
     * @param \Illuminate\Database\Eloquent\Model|int $user
58
     *
59
     */
60
    public function toggleFollow($user)
61
    {
62
        $this->isFollowing($user) ? $this->unfollow($user) : $this->follow($user);
63
    }
64
65
    /**
66
     * @param \Illuminate\Database\Eloquent\Model|int $user
67
     */
68
    public function rejectFollowRequestFrom($user)
69
    {
70
        $this->followers()->detach($user);
71
    }
72
73
    /**
74
     * @param \Illuminate\Database\Eloquent\Model|int $user
75
     */
76
    public function acceptFollowRequestFrom($user)
77
    {
78
        $this->followers()->updateExistingPivot($user, ['accepted_at' => now()]);
79
    }
80
81
    /**
82
     * @param \Illuminate\Database\Eloquent\Model|int $user
83
     */
84
    public function hasRequestedToFollow(Model $user): bool
85
    {
86
        if ($user instanceof Model) {
0 ignored issues
show
introduced by
$user is always a sub-type of Illuminate\Database\Eloquent\Model.
Loading history...
87
            $user = $user->getKey();
88
        }
89
90
        /* @var \Illuminate\Database\Eloquent\Model $this */
91
        if ($this->relationLoaded('followings')) {
92
            return $this->followings
93
                ->whereNull('pivot.accepted_at')
0 ignored issues
show
Bug introduced by
The method whereNull() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
                ->/** @scrutinizer ignore-call */ whereNull('pivot.accepted_at')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
94
                ->contains($user);
95
        }
96
97
        return $this->followings()
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->followings...ame(), $user)->exists() could return the type Illuminate\Database\Eloquent\Builder which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
98
            ->wherePivot('accepted_at', null)
99
            ->where($this->getQualifiedKeyName(), $user)
100
            ->exists();
101
    }
102
103
    /**
104
     * @param \Illuminate\Database\Eloquent\Model|int $user
105
     *
106
     * @return bool
107
     */
108
    public function isFollowing($user)
109
    {
110
        if ($user instanceof Model) {
111
            $user = $user->getKey();
112
        }
113
114
        /* @var \Illuminate\Database\Eloquent\Model $this */
115
        if ($this->relationLoaded('followings')) {
116
            return $this->followings
117
                ->whereNotNull('pivot.accepted_at')
118
                ->contains($user);
119
        }
120
121
        return $this->followings()
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->followings...ame(), $user)->exists() also could return the type Illuminate\Database\Eloquent\Builder which is incompatible with the documented return type boolean.
Loading history...
122
            ->wherePivot('accepted_at', '!=', null)
123
            ->where($this->getQualifiedKeyName(), $user)
124
            ->exists();
125
    }
126
127
    /**
128
     * @param \Illuminate\Database\Eloquent\Model|int $user
129
     *
130
     * @return bool
131
     */
132
    public function isFollowedBy($user)
133
    {
134
        if ($user instanceof Model) {
135
            $user = $user->getKey();
136
        }
137
138
        /* @var \Illuminate\Database\Eloquent\Model $this */
139
        if ($this->relationLoaded('followers')) {
140
            return $this->followers
141
                ->whereNotNull('pivot.accepted_at')
0 ignored issues
show
Bug introduced by
The method whereNotNull() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

141
                ->/** @scrutinizer ignore-call */ whereNotNull('pivot.accepted_at')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
142
                ->contains($user);
143
        }
144
145
        return $this->followers()
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->followers(...ame(), $user)->exists() also could return the type Illuminate\Database\Eloquent\Builder which is incompatible with the documented return type boolean.
Loading history...
146
            ->wherePivot('accepted_at', '!=', null)
147
            ->where($this->getQualifiedKeyName(), $user)
148
            ->exists();
149
    }
150
151
    /**
152
     * @param \Illuminate\Database\Eloquent\Model|int $user
153
     *
154
     * @return bool
155
     */
156
    public function areFollowingEachOther($user)
157
    {
158
        /* @var \Illuminate\Database\Eloquent\Model $user*/
159
        return $this->isFollowing($user) && $this->isFollowedBy($user);
160
    }
161
162
    /**
163
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
164
     */
165
    public function followers()
166
    {
167
        /* @var \Illuminate\Database\Eloquent\Model $this */
168
        return $this->belongsToMany(
169
            __CLASS__,
170
            \config('follow.relation_table', 'user_follower'),
171
            'following_id',
172
            'follower_id'
173
        )->withPivot('accepted_at')->withTimestamps()->using(UserFollower::class);
174
    }
175
176
    /**
177
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
178
     */
179
    public function followings()
180
    {
181
        /* @var \Illuminate\Database\Eloquent\Model $this */
182
        return $this->belongsToMany(
183
            __CLASS__,
184
            \config('follow.relation_table', 'user_follower'),
185
            'follower_id',
186
            'following_id'
187
        )->withPivot('accepted_at')->withTimestamps()->using(UserFollower::class);
188
    }
189
}
190