33use Lecturize \Followers \Exceptions \AlreadyFollowingException ;
44use Lecturize \Followers \Exceptions \CannotBeFollowedException ;
55use Lecturize \Followers \Exceptions \FollowerNotFoundException ;
6- use Lecturize \Followers \Models \Followable ;
6+ use Lecturize \Followers \Models \Follower ;
77
88use Illuminate \Database \Eloquent \Model ;
99
1010/**
11- * Class FollowableTrait
11+ * Class CanBeFollowed
1212 * @package Lecturize\Followers\Traits
1313 */
14- trait FollowableTrait
14+ trait CanBeFollowed
1515{
1616 /**
1717 * Get all followable items this model morphs to as being followed
@@ -20,11 +20,11 @@ trait FollowableTrait
2020 */
2121 public function follower ()
2222 {
23- return $ this ->morphMany (Followable ::class, 'followable ' );
23+ return $ this ->morphMany (Follower ::class, 'followable ' );
2424 }
2525
2626 /**
27- * @param $query
27+ * @param $query
2828 * @return mixed
2929 */
3030 public function scopeFollowers ( $ query )
@@ -37,29 +37,29 @@ public function scopeFollowers( $query )
3737 }
3838
3939 /**
40- * Add follower
40+ * Add a follower.
4141 *
42- * @param Model $follower
42+ * @param Model $follower
4343 * @return mixed
4444 * @throws AlreadyFollowingException
4545 * @throws CannotBeFollowedException
4646 */
4747 public function addFollower ( Model $ follower )
4848 {
4949 // check if $follower is already following this
50- if ( $ hasFollower = $ this ->hasFollower ($ follower ) !== false )
51- throw new AlreadyFollowingException ( get_class ($ follower ) .':: ' . $ follower ->id .' is already following ' . get_class ($ this ) .':: ' . $ this ->id );
50+ if ($ hasFollower = $ this ->hasFollower ($ follower ) !== false )
51+ throw new AlreadyFollowingException (get_class ($ follower ) .':: ' . $ follower ->id .' is already following ' . get_class ($ this ) .':: ' . $ this ->id );
5252
53- // check if $follower can follow (has CanFollowTrait )
54- if ( ! $ follower ->followables () )
55- throw new CannotBeFollowedException ( get_class ($ follower ) .':: ' . $ follower ->id .' cannot follow this. ' );
53+ // check if $follower can follow (has CanFollow )
54+ if (! $ follower ->followables ())
55+ throw new CannotBeFollowedException (get_class ($ follower ) .':: ' . $ follower ->id .' cannot follow this. ' );
5656
5757 $ key = $ this ->getFollowerCacheKey ();
5858
59- if ( config ('lecturize.followers.cache.enable ' , true ) )
60- \Cache:: forget ($ key );
59+ if (config ('lecturize.followers.cache.enable ' , true ))
60+ cache ()-> forget ($ key );
6161
62- return Followable ::create ([
62+ return Follower ::create ([
6363 'follower_id ' => $ follower ->id ,
6464 'follower_type ' => get_class ($ follower ),
6565 'followable_id ' => $ this ->id ,
@@ -68,61 +68,60 @@ public function addFollower( Model $follower )
6868 }
6969
7070 /**
71- * Delete follower
71+ * Delete a follower.
7272 *
73- * @param Model $follower
73+ * @param Model $follower
7474 * @return mixed
7575 * @throws FollowerNotFoundException
7676 */
7777 public function deleteFollower ( Model $ follower )
7878 {
79- if ( $ hasFollower = $ this ->hasFollower ($ follower ) === true )
80- {
79+ if ($ hasFollower = $ this ->hasFollower ($ follower ) === true ) {
8180 $ key = $ this ->getFollowerCacheKey ();
8281
83- if ( config ('lecturize.followers.cache.enable ' , true ) )
84- \Cache:: forget ($ key );
82+ if (config ('lecturize.followers.cache.enable ' , true ))
83+ cache ()-> forget ($ key );
8584
86- return Followable ::followedBy ( $ follower )
87- ->following ( $ this )
88- ->delete ();
85+ return Follower ::followedBy ( $ follower )
86+ ->following ( $ this )
87+ ->delete ();
8988 }
9089
91- throw new FollowerNotFoundException ( get_class ($ follower ) .':: ' . $ follower ->id .' is not following ' . get_class ($ this ) .':: ' . $ this ->id );
90+ throw new FollowerNotFoundException (get_class ($ follower ) .':: ' . $ follower ->id .' is not following ' . get_class ($ this ) .':: ' . $ this ->id );
9291 }
9392
9493 /**
95- * @param $follower
94+ * @param $follower
9695 * @return bool
9796 */
98- public function hasFollower ( $ follower )
97+ public function hasFollower ($ follower )
9998 {
100- $ query = Followable ::followedBy ( $ follower )
101- ->following ( $ this );
99+ $ query = Follower ::followedBy ($ follower )
100+ ->following ($ this );
102101
103102 return $ query ->count () > 0 ;
104103 }
105104
106105 /**
107- * @param bool $get_cached
106+ * @param bool $get_cached
108107 * @return mixed
109108 */
110- public function getFollowerCount ( $ get_cached = true )
109+ public function getFollowerCount ($ get_cached = true )
111110 {
112111 $ key = $ this ->getFollowerCacheKey ();
113112
114- if ( $ get_cached && config ('lecturize.followers.cache.enable ' , true ) && \Cache:: has ($ key ) )
115- return \Cache:: get ($ key );
113+ if ($ get_cached && config ('lecturize.followers.cache.enable ' , true ) && cache ()-> has ($ key ))
114+ return cache ()-> get ($ key );
116115
117116 $ count = 0 ;
118- Followable ::where ('followable_id ' , $ this ->id )
119- ->where ('followable_type ' , get_class ($ this ))
120- ->chunk (1000 , function ($ models ) use (&$ count ) {
117+ Follower ::where ('followable_id ' , $ this ->id )
118+ ->where ('followable_type ' , get_class ($ this ))
119+ ->chunk (1000 , function ($ models ) use (&$ count ) {
121120 $ count = $ count + count ($ models );
122- });
121+ });
123122
124- if ( config ('lecturize.followers.cache.enable ' , true ) )
125- \Cache:: put ($ key , $ count , config ('lecturize.followers.cache.expiry ' , 10 ));
123+ if (config ('lecturize.followers.cache.enable ' , true ))
124+ cache ()-> put ($ key , $ count , config ('lecturize.followers.cache.expiry ' , 10 ));
126125
127126 return $ count ;
128127 }
@@ -134,21 +133,20 @@ public function getFollowerCount( $get_cached = true )
134133 */
135134 public function getFollowers ( $ limit = 0 , $ type = '' )
136135 {
137- if ( $ type ) {
136+ if ($ type ) {
138137 $ followers = $ this ->follower ()->where ('follower_type ' , $ type )->get ();
139138 } else {
140139 $ followers = $ this ->follower ()->get ();
141140 }
142141
143142 $ return = [];
144- foreach ( $ followers as $ follower )
145- {
143+ foreach ($ followers as $ follower ) {
146144 $ return [] = $ follower ->follower ()->first ();
147145 }
148146
149147 $ collection = collect ($ return )->shuffle ();
150148
151- if ( $ limit == 0 )
149+ if ($ limit === 0 )
152150 return $ collection ;
153151
154152 return $ collection ->take ($ limit );
0 commit comments