@@ -23,28 +23,16 @@ public function follower()
2323 return $ this ->morphMany (Follower::class, 'followable ' );
2424 }
2525
26- /**
27- * @param $query
28- * @return mixed
29- */
30- public function scopeFollowers ( $ query )
31- {
32- $ model = $ this ;
33- return $ query ->whereHas ('follower ' , function ($ q ) use ($ model ) {
34- $ q ->where ('followable_id ' , $ model ->id );
35- $ q ->where ('followable_type ' , get_class ($ model ));
36- });
37- }
38-
3926 /**
4027 * Add a follower.
4128 *
42- * @param Model $follower
29+ * @param mixed $follower
4330 * @return mixed
31+ *
4432 * @throws AlreadyFollowingException
4533 * @throws CannotBeFollowedException
4634 */
47- public function addFollower ( Model $ follower )
35+ public function addFollower ($ follower )
4836 {
4937 // check if $follower is already following this
5038 if ($ hasFollower = $ this ->hasFollower ($ follower ) !== false )
@@ -54,10 +42,7 @@ public function addFollower( Model $follower )
5442 if (! $ follower ->followables ())
5543 throw new CannotBeFollowedException (get_class ($ follower ) .':: ' . $ follower ->id .' cannot follow this. ' );
5644
57- $ key = $ this ->getFollowerCacheKey ();
58-
59- if (config ('lecturize.followers.cache.enable ' , true ))
60- cache ()->forget ($ key );
45+ cache ()->forget ($ this ->getFollowerCacheKey ());
6146
6247 return Follower::create ([
6348 'follower_id ' => $ follower ->id ,
@@ -70,68 +55,64 @@ public function addFollower( Model $follower )
7055 /**
7156 * Delete a follower.
7257 *
73- * @param Model $follower
58+ * @param mixed $follower
7459 * @return mixed
7560 * @throws FollowerNotFoundException
7661 */
77- public function deleteFollower ( Model $ follower )
62+ public function deleteFollower ($ follower )
7863 {
7964 if ($ hasFollower = $ this ->hasFollower ($ follower ) === true ) {
80- $ key = $ this ->getFollowerCacheKey ();
65+ cache ()-> forget ( $ this ->getFollowerCacheKey () );
8166
82- if (config ('lecturize.followers.cache.enable ' , true ))
83- cache ()->forget ($ key );
84-
85- return Follower::followedBy ( $ follower )
86- ->following ( $ this )
67+ return Follower::followedBy ($ follower )
68+ ->following ($ this )
8769 ->delete ();
8870 }
8971
9072 throw new FollowerNotFoundException (get_class ($ follower ) .':: ' . $ follower ->id .' is not following ' . get_class ($ this ) .':: ' . $ this ->id );
9173 }
9274
9375 /**
94- * @param $follower
76+ * Check whether this model has a given follower.
77+ *
78+ * @param mixed $follower
9579 * @return bool
9680 */
9781 public function hasFollower ($ follower )
9882 {
9983 $ query = Follower::followedBy ($ follower )
10084 ->following ($ this );
10185
102- return $ query ->count () > 0 ;
86+ return ( bool ) $ query ->count () > 0 ;
10387 }
10488
10589 /**
106- * @param bool $get_cached
107- * @return mixed
90+ * Get the follower count.
91+ *
92+ * @return int
10893 */
109- public function getFollowerCount ($ get_cached = true )
94+ public function getFollowerCount ()
11095 {
11196 $ key = $ this ->getFollowerCacheKey ();
11297
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 ('followable_id ' , $ this ->id )
118- ->where ('followable_type ' , get_class ($ this ))
119- ->chunk (1000 , function ($ models ) use (&$ count ) {
120- $ count = $ count + count ($ models );
121- });
122-
123- if (config ('lecturize.followers.cache.enable ' , true ))
124- cache ()->put ($ key , $ count , config ('lecturize.followers.cache.expiry ' , 10 ));
98+ return cache ()->remember ($ key , config ('lecturize.followers.cache.expiry ' , 10 ), function () {
99+ $ count = 0 ;
100+ Follower::where ('followable_id ' , $ this ->id )
101+ ->where ('followable_type ' , get_class ($ this ))
102+ ->chunk (1000 , function ($ models ) use (&$ count ) {
103+ $ count = $ count + count ($ models );
104+ });
125105
126- return $ count ;
106+ return $ count ;
107+ });
127108 }
128109
129110 /**
130- * @param int $limit
131- * @param string $type
111+ * @param int $limit
112+ * @param string $type
132113 * @return mixed
133114 */
134- public function getFollowers ( $ limit = 0 , $ type = '' )
115+ public function getFollowers ($ limit = 0 , $ type = '' )
135116 {
136117 if ($ type ) {
137118 $ followers = $ this ->follower ()->where ('follower_type ' , $ type )->get ();
@@ -140,9 +121,8 @@ public function getFollowers( $limit = 0, $type = '' )
140121 }
141122
142123 $ return = [];
143- foreach ($ followers as $ follower ) {
124+ foreach ($ followers as $ follower )
144125 $ return [] = $ follower ->follower ()->first ();
145- }
146126
147127 $ collection = collect ($ return )->shuffle ();
148128
@@ -153,17 +133,30 @@ public function getFollowers( $limit = 0, $type = '' )
153133 }
154134
155135 /**
136+ * Get the cache key.
137+ *
156138 * @return string
157139 */
158140 private function getFollowerCacheKey ()
159141 {
160- $ id = $ this ->id ;
161- $ class = get_class ($ this );
162- $ type = explode ('\\' , $ class );
163-
164- $ key = 'followers. ' . end ($ type ) .'. ' . $ id .'.follower.count ' ;
165- $ key = md5 (strtolower ($ key ));
142+ $ model = get_class ($ this );
143+ $ model = substr ($ model , strrpos ($ model , '\\' ) + 1 );
144+ $ model = strtolower ($ model );
166145
167- return $ key ;
146+ return ' followers. ' . $ model . ' . ' . $ this -> id . ' .follower.count ' ;
168147 }
148+
149+ /**
150+ * Scope followers.
151+ *
152+ * @param object $query
153+ * @return mixed
154+ */
155+ public function scopeFollowers ($ query )
156+ {
157+ return $ query ->whereHas ('follower ' , function ($ q ) {
158+ $ q ->where ('followable_id ' , $ this ->id );
159+ $ q ->where ('followable_type ' , get_class ($ this ));
160+ });
161+ }
169162}
0 commit comments