1414trait CanFollow
1515{
1616 /**
17- * Get all followable items this model morphs to as a follower
17+ * Get all followable items this model morphs to as a follower.
1818 *
1919 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
2020 */
@@ -24,37 +24,22 @@ public function followables()
2424 }
2525
2626 /**
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.
4128 *
42- * @param Model $followable
29+ * @param mixed $followable
4330 * @return mixed
31+ *
4432 * @throws AlreadyFollowingException
4533 * @throws CannotBeFollowedException
4634 */
47- public function follow ( Model $ followable )
35+ public function follow ($ followable )
4836 {
4937 if ($ isFollower = $ this ->isFollowing ($ followable ) !== false ) {
5038 throw new AlreadyFollowingException ( get_class ($ this ) .':: ' . $ this ->id .' is already following ' . get_class ($ followable ) .':: ' . $ followable ->id );
5139 }
5240
5341 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 ());
5843
5944 return Follower::create ([
6045 'follower_id ' => $ this ->id ,
@@ -68,19 +53,17 @@ public function follow( Model $followable )
6853 }
6954
7055 /**
71- * Unfollow method
56+ * Unfollow a followable model.
7257 *
73- * @param Model $followable
58+ * @param mixed $followable
7459 * @return mixed
60+ *
7561 * @throws FollowerNotFoundException
7662 */
77- public function unfollow ( Model $ followable )
63+ public function unfollow ($ followable )
7864 {
7965 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 ());
8467
8568 return Follower::following ($ followable )
8669 ->followedBy ($ this )
@@ -91,47 +74,46 @@ public function unfollow( Model $followable )
9174 }
9275
9376 /**
94- * @param $followable
77+ * Check whether this model is following a given followable model.
78+ *
79+ * @param mixed $followable
9580 * @return bool
9681 */
97- public function isFollowing ( $ followable )
82+ public function isFollowing ($ followable )
9883 {
9984 $ query = Follower::following ($ followable )
10085 ->followedBy ($ this );
10186
102- return $ query ->count () > 0 ;
87+ return ( bool ) $ query ->count () > 0 ;
10388 }
10489
10590 /**
106- * @param bool $get_cached
107- * @return mixed
91+ * Get the following count.
92+ *
93+ * @return int
10894 */
109- public function getFollowingCount ( $ get_cached = true )
95+ public function getFollowingCount ()
11096 {
11197 $ key = $ this ->getFollowingCacheKey ();
11298
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+ });
122106
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+ });
127109 }
128110
129111 /**
130112 * @param int $limit
131113 * @param string $type
132114 * @return mixed
133115 */
134- public function getFollowing ( $ limit = 0 , $ type = '' )
116+ public function getFollowing ($ limit = 0 , $ type = '' )
135117 {
136118 if ($ type ) {
137119 $ followables = $ this ->followables ()->where ('followable_type ' , $ type )->get ();
@@ -140,9 +122,8 @@ public function getFollowing( $limit = 0, $type = '' )
140122 }
141123
142124 $ return = [];
143- foreach ($ followables as $ followable ) {
125+ foreach ($ followables as $ followable )
144126 $ return [] = $ followable ->followable ()->first ();
145- }
146127
147128 $ collection = collect ($ return )->shuffle ();
148129
@@ -153,17 +134,30 @@ public function getFollowing( $limit = 0, $type = '' )
153134 }
154135
155136 /**
137+ * Get the cache key.
138+ *
156139 * @return string
157140 */
158141 private function getFollowingCacheKey ()
159142 {
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 );
163146
164- $ key = 'followers. ' . end ( $ type ) .'. ' . $ id .'.following.count ' ;
165- $ key = md5 ( strtolower ( $ key ));
147+ return 'followers. ' . $ model .'. ' . $ this -> id .'.following.count ' ;
148+ }
166149
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+ });
168162 }
169163}
0 commit comments