Skip to content

Commit

Permalink
Minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderPoellmann committed Aug 19, 2017
1 parent c9ea323 commit 79850d8
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 58 deletions.
4 changes: 4 additions & 0 deletions src/Contracts/CanBeFollowedInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php namespace Lecturize\Followers\Contracts;

/**
* Interface CanBeFollowedInterface
* @package Lecturize\Followers\Contracts
*/
interface CanBeFollowedInterface
{
//
Expand Down
4 changes: 4 additions & 0 deletions src/Contracts/CanFollowInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php namespace Lecturize\Followers\Contracts;

/**
* Interface CanFollowInterface
* @package Lecturize\Followers\Contracts
*/
interface CanFollowInterface
{
//
Expand Down
4 changes: 4 additions & 0 deletions src/Exceptions/AlreadyFollowingException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php namespace Lecturize\Followers\Exceptions;

/**
* Class AlreadyFollowingException
* @package Lecturize\Followers\Exceptions
*/
class AlreadyFollowingException extends \Exception
{
//
Expand Down
4 changes: 4 additions & 0 deletions src/Exceptions/CannotBeFollowedException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php namespace Lecturize\Followers\Exceptions;

/**
* Class CannotBeFollowedException
* @package Lecturize\Followers\Exceptions
*/
class CannotBeFollowedException extends \Exception
{
//
Expand Down
4 changes: 4 additions & 0 deletions src/Exceptions/FollowerNotFoundException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php namespace Lecturize\Followers\Exceptions;

/**
* Class FollowerNotFoundException
* @package Lecturize\Followers\Exceptions
*/
class FollowerNotFoundException extends \Exception
{
//
Expand Down
5 changes: 3 additions & 2 deletions src/Traits/CanBeFollowed.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
trait CanBeFollowed
{
/**
* Get all followable items this model morphs to as being followed
* Get all followable items this model morphs to as being followed.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
Expand All @@ -28,7 +28,7 @@ public function follower()
*
* @param mixed $follower
* @return mixed
*
+
* @throws AlreadyFollowingException
* @throws CannotBeFollowedException
*/
Expand Down Expand Up @@ -57,6 +57,7 @@ public function addFollower($follower)
*
* @param mixed $follower
* @return mixed
*
* @throws FollowerNotFoundException
*/
public function deleteFollower($follower)
Expand Down
106 changes: 50 additions & 56 deletions src/Traits/CanFollow.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
trait CanFollow
{
/**
* Get all followable items this model morphs to as a follower
* Get all followable items this model morphs to as a follower.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
Expand All @@ -24,37 +24,22 @@ public function followables()
}

/**
* @param $query
* @return mixed
*/
public function scopeFollows($query)
{
$model = $this;
return $query->whereHas('followables', function($q) use($model) {
$q->where('follower_id', $model->id);
$q->where('follower_type', get_class($model));
});
}

/**
* Follow method
* Follow a followable model.
*
* @param Model $followable
* @param mixed $followable
* @return mixed
*
* @throws AlreadyFollowingException
* @throws CannotBeFollowedException
*/
public function follow( Model $followable )
public function follow($followable)
{
if ($isFollower = $this->isFollowing($followable) !== false) {
throw new AlreadyFollowingException( get_class($this) .'::'. $this->id .' is already following '. get_class($followable) .'::'. $followable->id );
}

if ($followable->follower()) {
$key = $this->getFollowingCacheKey();

if (config('lecturize.followers.cache.enable', true))
cache()->forget($key);
cache()->forget($this->getFollowingCacheKey());

return Follower::create([
'follower_id' => $this->id,
Expand All @@ -68,19 +53,17 @@ public function follow( Model $followable )
}

/**
* Unfollow method
* Unfollow a followable model.
*
* @param Model $followable
* @param mixed $followable
* @return mixed
*
* @throws FollowerNotFoundException
*/
public function unfollow( Model $followable )
public function unfollow($followable)
{
if ($isFollower = $this->isFollowing($followable) === true) {
$key = $this->getFollowingCacheKey();

if (config('lecturize.followers.cache.enable', true))
cache()->forget($key);
cache()->forget($this->getFollowingCacheKey());

return Follower::following($followable)
->followedBy($this)
Expand All @@ -91,47 +74,46 @@ public function unfollow( Model $followable )
}

/**
* @param $followable
* Check whether this model is following a given followable model.
*
* @param mixed $followable
* @return bool
*/
public function isFollowing( $followable )
public function isFollowing($followable)
{
$query = Follower::following($followable)
->followedBy($this);

return $query->count() > 0;
return (bool) $query->count() > 0;
}

/**
* @param bool $get_cached
* @return mixed
* Get the following count.
*
* @return int
*/
public function getFollowingCount( $get_cached = true )
public function getFollowingCount()
{
$key = $this->getFollowingCacheKey();

if ($get_cached && config('lecturize.followers.cache.enable', true) && cache()->has($key))
return cache()->get($key);

$count = 0;
Follower::where('follower_id', $this->id)
->where('follower_type', get_class($this))
->chunk(1000, function ($models) use (&$count) {
$count = $count + count($models);
});
return cache()->remember($key, config('lecturize.followers.cache.expiry', 10), function() {
$count = 0;
Follower::where('follower_id', $this->id)
->where('follower_type', get_class($this))
->chunk(1000, function ($models) use (&$count) {
$count = $count + count($models);
});

if (config('lecturize.followers.cache.enable', true))
cache()->put($key, $count, config('lecturize.followers.cache.expiry', 10));

return $count;
return $count;
});
}

/**
* @param int $limit
* @param string $type
* @return mixed
*/
public function getFollowing( $limit = 0, $type = '' )
public function getFollowing($limit = 0, $type = '')
{
if ($type) {
$followables = $this->followables()->where('followable_type', $type)->get();
Expand All @@ -140,9 +122,8 @@ public function getFollowing( $limit = 0, $type = '' )
}

$return = [];
foreach ($followables as $followable) {
foreach ($followables as $followable)
$return[] = $followable->followable()->first();
}

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

Expand All @@ -153,17 +134,30 @@ public function getFollowing( $limit = 0, $type = '' )
}

/**
* Get the cache key.
*
* @return string
*/
private function getFollowingCacheKey()
{
$id = $this->id;
$class = get_class($this);
$type = explode('\\', $class);
$model = get_class($this);
$model = substr($model, strrpos($model, '\\') + 1);
$model = strtolower($model);

$key = 'followers.'. end($type) .'.'. $id .'.following.count';
$key = md5(strtolower($key));
return 'followers.'. $model .'.'. $this->id .'.following.count';
}

return $key;
/**
* Scope follows.
*
* @param object $query
* @return mixed
*/
public function scopeFollows($query)
{
return $query->whereHas('followables', function($q) {
$q->where('follower_id', $this->id);
$q->where('follower_type', get_class($this));
});
}
}

0 comments on commit 79850d8

Please sign in to comment.