14
14
trait CanFollow
15
15
{
16
16
/**
17
- * Get all followable items this model morphs to as a follower
17
+ * Get all followable items this model morphs to as a follower.
18
18
*
19
19
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
20
20
*/
@@ -24,37 +24,22 @@ public function followables()
24
24
}
25
25
26
26
/**
27
- * @param $query
28
- * @return mixed
29
- */
30
- public function scopeFollows ($ query )
31
- {
32
- $ model = $ this ;
33
- return $ query ->whereHas ('followables ' , function ($ q ) use ($ model ) {
34
- $ q ->where ('follower_id ' , $ model ->id );
35
- $ q ->where ('follower_type ' , get_class ($ model ));
36
- });
37
- }
38
-
39
- /**
40
- * Follow method
27
+ * Follow a followable model.
41
28
*
42
- * @param Model $followable
29
+ * @param mixed $followable
43
30
* @return mixed
31
+ *
44
32
* @throws AlreadyFollowingException
45
33
* @throws CannotBeFollowedException
46
34
*/
47
- public function follow ( Model $ followable )
35
+ public function follow ($ followable )
48
36
{
49
37
if ($ isFollower = $ this ->isFollowing ($ followable ) !== false ) {
50
38
throw new AlreadyFollowingException ( get_class ($ this ) .':: ' . $ this ->id .' is already following ' . get_class ($ followable ) .':: ' . $ followable ->id );
51
39
}
52
40
53
41
if ($ followable ->follower ()) {
54
- $ key = $ this ->getFollowingCacheKey ();
55
-
56
- if (config ('lecturize.followers.cache.enable ' , true ))
57
- cache ()->forget ($ key );
42
+ cache ()->forget ($ this ->getFollowingCacheKey ());
58
43
59
44
return Follower::create ([
60
45
'follower_id ' => $ this ->id ,
@@ -68,19 +53,17 @@ public function follow( Model $followable )
68
53
}
69
54
70
55
/**
71
- * Unfollow method
56
+ * Unfollow a followable model.
72
57
*
73
- * @param Model $followable
58
+ * @param mixed $followable
74
59
* @return mixed
60
+ *
75
61
* @throws FollowerNotFoundException
76
62
*/
77
- public function unfollow ( Model $ followable )
63
+ public function unfollow ($ followable )
78
64
{
79
65
if ($ isFollower = $ this ->isFollowing ($ followable ) === true ) {
80
- $ key = $ this ->getFollowingCacheKey ();
81
-
82
- if (config ('lecturize.followers.cache.enable ' , true ))
83
- cache ()->forget ($ key );
66
+ cache ()->forget ($ this ->getFollowingCacheKey ());
84
67
85
68
return Follower::following ($ followable )
86
69
->followedBy ($ this )
@@ -91,47 +74,46 @@ public function unfollow( Model $followable )
91
74
}
92
75
93
76
/**
94
- * @param $followable
77
+ * Check whether this model is following a given followable model.
78
+ *
79
+ * @param mixed $followable
95
80
* @return bool
96
81
*/
97
- public function isFollowing ( $ followable )
82
+ public function isFollowing ($ followable )
98
83
{
99
84
$ query = Follower::following ($ followable )
100
85
->followedBy ($ this );
101
86
102
- return $ query ->count () > 0 ;
87
+ return ( bool ) $ query ->count () > 0 ;
103
88
}
104
89
105
90
/**
106
- * @param bool $get_cached
107
- * @return mixed
91
+ * Get the following count.
92
+ *
93
+ * @return int
108
94
*/
109
- public function getFollowingCount ( $ get_cached = true )
95
+ public function getFollowingCount ()
110
96
{
111
97
$ key = $ this ->getFollowingCacheKey ();
112
98
113
- if ($ get_cached && config ('lecturize.followers.cache.enable ' , true ) && cache ()->has ($ key ))
114
- return cache ()->get ($ key );
115
-
116
- $ count = 0 ;
117
- Follower::where ('follower_id ' , $ this ->id )
118
- ->where ('follower_type ' , get_class ($ this ))
119
- ->chunk (1000 , function ($ models ) use (&$ count ) {
120
- $ count = $ count + count ($ models );
121
- });
99
+ return cache ()->remember ($ key , config ('lecturize.followers.cache.expiry ' , 10 ), function () {
100
+ $ count = 0 ;
101
+ Follower::where ('follower_id ' , $ this ->id )
102
+ ->where ('follower_type ' , get_class ($ this ))
103
+ ->chunk (1000 , function ($ models ) use (&$ count ) {
104
+ $ count = $ count + count ($ models );
105
+ });
122
106
123
- if (config ('lecturize.followers.cache.enable ' , true ))
124
- cache ()->put ($ key , $ count , config ('lecturize.followers.cache.expiry ' , 10 ));
125
-
126
- return $ count ;
107
+ return $ count ;
108
+ });
127
109
}
128
110
129
111
/**
130
112
* @param int $limit
131
113
* @param string $type
132
114
* @return mixed
133
115
*/
134
- public function getFollowing ( $ limit = 0 , $ type = '' )
116
+ public function getFollowing ($ limit = 0 , $ type = '' )
135
117
{
136
118
if ($ type ) {
137
119
$ followables = $ this ->followables ()->where ('followable_type ' , $ type )->get ();
@@ -140,9 +122,8 @@ public function getFollowing( $limit = 0, $type = '' )
140
122
}
141
123
142
124
$ return = [];
143
- foreach ($ followables as $ followable ) {
125
+ foreach ($ followables as $ followable )
144
126
$ return [] = $ followable ->followable ()->first ();
145
- }
146
127
147
128
$ collection = collect ($ return )->shuffle ();
148
129
@@ -153,17 +134,30 @@ public function getFollowing( $limit = 0, $type = '' )
153
134
}
154
135
155
136
/**
137
+ * Get the cache key.
138
+ *
156
139
* @return string
157
140
*/
158
141
private function getFollowingCacheKey ()
159
142
{
160
- $ id = $ this -> id ;
161
- $ class = get_class ( $ this );
162
- $ type = explode ( '\\' , $ class );
143
+ $ model = get_class ( $ this ) ;
144
+ $ model = substr ( $ model , strrpos ( $ model , '\\' ) + 1 );
145
+ $ model = strtolower ( $ model );
163
146
164
- $ key = 'followers. ' . end ( $ type ) .'. ' . $ id .'.following.count ' ;
165
- $ key = md5 ( strtolower ( $ key ));
147
+ return 'followers. ' . $ model .'. ' . $ this -> id .'.following.count ' ;
148
+ }
166
149
167
- return $ key ;
150
+ /**
151
+ * Scope follows.
152
+ *
153
+ * @param object $query
154
+ * @return mixed
155
+ */
156
+ public function scopeFollows ($ query )
157
+ {
158
+ return $ query ->whereHas ('followables ' , function ($ q ) {
159
+ $ q ->where ('follower_id ' , $ this ->id );
160
+ $ q ->where ('follower_type ' , get_class ($ this ));
161
+ });
168
162
}
169
163
}
0 commit comments