Skip to content

Commit 57ef8c7

Browse files
Rename traits
1 parent daed833 commit 57ef8c7

File tree

2 files changed

+77
-83
lines changed

2 files changed

+77
-83
lines changed
Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
use Lecturize\Followers\Exceptions\AlreadyFollowingException;
44
use Lecturize\Followers\Exceptions\CannotBeFollowedException;
55
use Lecturize\Followers\Exceptions\FollowerNotFoundException;
6-
use Lecturize\Followers\Models\Followable;
6+
use Lecturize\Followers\Models\Follower;
77

88
use 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);
Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
use Lecturize\Followers\Exceptions\AlreadyFollowingException;
44
use Lecturize\Followers\Exceptions\CannotBeFollowedException;
55
use Lecturize\Followers\Exceptions\FollowerNotFoundException;
6-
use Lecturize\Followers\Models\Followable;
6+
use Lecturize\Followers\Models\Follower;
77

88
use Illuminate\Database\Eloquent\Model;
99

1010
/**
11-
* Class CanFollowTrait
11+
* Class CanFollow
1212
* @package Lecturize\Followers\Traits
1313
*/
14-
trait CanFollowTrait
14+
trait CanFollow
1515
{
1616
/**
1717
* Get all followable items this model morphs to as a follower
@@ -20,14 +20,14 @@ trait CanFollowTrait
2020
*/
2121
public function followables()
2222
{
23-
return $this->morphMany(Followable::class, 'follower');
23+
return $this->morphMany(Follower::class, 'follower');
2424
}
2525

2626
/**
27-
* @param $query
27+
* @param $query
2828
* @return mixed
2929
*/
30-
public function scopeFollows( $query )
30+
public function scopeFollows($query)
3131
{
3232
$model = $this;
3333
return $query->whereHas('followables', function($q) use($model) {
@@ -39,68 +39,65 @@ public function scopeFollows( $query )
3939
/**
4040
* Follow method
4141
*
42-
* @param Model $followable
42+
* @param Model $followable
4343
* @return mixed
4444
* @throws AlreadyFollowingException
4545
* @throws CannotBeFollowedException
4646
*/
4747
public function follow( Model $followable )
4848
{
49-
if ( $isFollower = $this->isFollowing($followable) !== false )
50-
{
49+
if ($isFollower = $this->isFollowing($followable) !== false) {
5150
throw new AlreadyFollowingException( get_class($this) .'::'. $this->id .' is already following '. get_class($followable) .'::'. $followable->id );
5251
}
5352

54-
if ( $followable->follower() )
55-
{
53+
if ($followable->follower()) {
5654
$key = $this->getFollowingCacheKey();
5755

58-
if ( config('lecturize.followers.cache.enable', true) )
59-
\Cache::forget($key);
56+
if (config('lecturize.followers.cache.enable', true))
57+
cache()->forget($key);
6058

61-
return Followable::create([
59+
return Follower::create([
6260
'follower_id' => $this->id,
6361
'follower_type' => get_class($this),
6462
'followable_id' => $followable->id,
6563
'followable_type' => get_class($followable),
6664
]);
6765
}
6866

69-
throw new CannotBeFollowedException( get_class($followable) .'::'. $followable->id .' cannot be followed.' );
67+
throw new CannotBeFollowedException(get_class($followable) .'::'. $followable->id .' cannot be followed.');
7068
}
7169

7270
/**
7371
* Unfollow method
7472
*
75-
* @param Model $followable
73+
* @param Model $followable
7674
* @return mixed
7775
* @throws FollowerNotFoundException
7876
*/
7977
public function unfollow( Model $followable )
8078
{
81-
if ( $isFollower = $this->isFollowing($followable) === true )
82-
{
79+
if ($isFollower = $this->isFollowing($followable) === true) {
8380
$key = $this->getFollowingCacheKey();
8481

85-
if ( config('lecturize.followers.cache.enable', true) )
86-
\Cache::forget($key);
82+
if (config('lecturize.followers.cache.enable', true))
83+
cache()->forget($key);
8784

88-
return Followable::following( $followable )
89-
->followedBy( $this )
90-
->delete();
85+
return Follower::following($followable)
86+
->followedBy($this)
87+
->delete();
9188
}
9289

93-
throw new FollowerNotFoundException( get_class($this) .'::'. $this->id .' is not following '. get_class($followable) .'::'. $followable->id );
90+
throw new FollowerNotFoundException(get_class($this) .'::'. $this->id .' is not following '. get_class($followable) .'::'. $followable->id);
9491
}
9592

9693
/**
97-
* @param $followable
94+
* @param $followable
9895
* @return bool
9996
*/
10097
public function isFollowing( $followable )
10198
{
102-
$query = Followable::following( $followable )
103-
->followedBy( $this );
99+
$query = Follower::following($followable)
100+
->followedBy($this);
104101

105102
return $query->count() > 0;
106103
}
@@ -113,44 +110,43 @@ public function getFollowingCount( $get_cached = true )
113110
{
114111
$key = $this->getFollowingCacheKey();
115112

116-
if ( $get_cached && config('lecturize.followers.cache.enable', true) && \Cache::has($key) )
117-
return \Cache::get($key);
113+
if ($get_cached && config('lecturize.followers.cache.enable', true) && cache()->has($key))
114+
return cache()->get($key);
118115

119116
$count = 0;
120-
Followable::where('follower_id', $this->id)
121-
->where('follower_type', get_class($this))
122-
->chunk(1000, function ($models) use (&$count) {
117+
Follower::where('follower_id', $this->id)
118+
->where('follower_type', get_class($this))
119+
->chunk(1000, function ($models) use (&$count) {
123120
$count = $count + count($models);
124121
});
125122

126-
if ( config('lecturize.followers.cache.enable', true) )
127-
\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));
128125

129126
return $count;
130127
}
131128

132129
/**
133-
* @param int $limit
134-
* @param string $type
130+
* @param int $limit
131+
* @param string $type
135132
* @return mixed
136133
*/
137134
public function getFollowing( $limit = 0, $type = '' )
138135
{
139-
if ( $type ) {
136+
if ($type) {
140137
$followables = $this->followables()->where('followable_type', $type)->get();
141138
} else {
142139
$followables = $this->followables()->get();
143140
}
144141

145142
$return = [];
146-
foreach ( $followables as $followable )
147-
{
143+
foreach ($followables as $followable) {
148144
$return[] = $followable->followable()->first();
149145
}
150146

151147
$collection = collect($return)->shuffle();
152148

153-
if ( $limit == 0 )
149+
if ($limit == 0)
154150
return $collection;
155151

156152
return $collection->take($limit);

0 commit comments

Comments
 (0)