Skip to content

Commit 79850d8

Browse files
Minor tweaks
1 parent c9ea323 commit 79850d8

7 files changed

+73
-58
lines changed

src/Contracts/CanBeFollowedInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<?php namespace Lecturize\Followers\Contracts;
22

3+
/**
4+
* Interface CanBeFollowedInterface
5+
* @package Lecturize\Followers\Contracts
6+
*/
37
interface CanBeFollowedInterface
48
{
59
//

src/Contracts/CanFollowInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<?php namespace Lecturize\Followers\Contracts;
22

3+
/**
4+
* Interface CanFollowInterface
5+
* @package Lecturize\Followers\Contracts
6+
*/
37
interface CanFollowInterface
48
{
59
//

src/Exceptions/AlreadyFollowingException.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<?php namespace Lecturize\Followers\Exceptions;
22

3+
/**
4+
* Class AlreadyFollowingException
5+
* @package Lecturize\Followers\Exceptions
6+
*/
37
class AlreadyFollowingException extends \Exception
48
{
59
//

src/Exceptions/CannotBeFollowedException.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<?php namespace Lecturize\Followers\Exceptions;
22

3+
/**
4+
* Class CannotBeFollowedException
5+
* @package Lecturize\Followers\Exceptions
6+
*/
37
class CannotBeFollowedException extends \Exception
48
{
59
//

src/Exceptions/FollowerNotFoundException.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<?php namespace Lecturize\Followers\Exceptions;
22

3+
/**
4+
* Class FollowerNotFoundException
5+
* @package Lecturize\Followers\Exceptions
6+
*/
37
class FollowerNotFoundException extends \Exception
48
{
59
//

src/Traits/CanBeFollowed.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
trait CanBeFollowed
1515
{
1616
/**
17-
* Get all followable items this model morphs to as being followed
17+
* Get all followable items this model morphs to as being followed.
1818
*
1919
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
2020
*/
@@ -28,7 +28,7 @@ public function follower()
2828
*
2929
* @param mixed $follower
3030
* @return mixed
31-
*
31+
+
3232
* @throws AlreadyFollowingException
3333
* @throws CannotBeFollowedException
3434
*/
@@ -57,6 +57,7 @@ public function addFollower($follower)
5757
*
5858
* @param mixed $follower
5959
* @return mixed
60+
*
6061
* @throws FollowerNotFoundException
6162
*/
6263
public function deleteFollower($follower)

src/Traits/CanFollow.php

Lines changed: 50 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
trait 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

Comments
 (0)