-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
2,244 additions
and
303 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
<?php | ||
|
||
|
||
namespace Turahe\Likeable\Console; | ||
|
||
use Turahe\Likeable\Contracts\Likeable as LikeableContract; | ||
use Turahe\Likeable\Contracts\Like as LikeContract; | ||
use Turahe\Likeable\Contracts\LikeCounter as LikeCounterContract; | ||
use Turahe\Likeable\Exceptions\ModelInvalidException; | ||
use Turahe\Likeable\Services\LikeableService as LikeableServiceContract; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Contracts\Events\Dispatcher; | ||
use Illuminate\Database\Eloquent\Relations\Relation; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
class LikeableRecountCommand extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'likeable:recount {model?} {type?}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Recount likes and dislikes for the models'; | ||
|
||
/** | ||
* Type of likes to be recounted. | ||
* | ||
* @var string|null | ||
*/ | ||
protected $likeType; | ||
|
||
/** | ||
* Likeable service. | ||
* | ||
* @var \Turahe\Likeable\Contracts\LikeableService | ||
*/ | ||
protected $service; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @param \Illuminate\Contracts\Events\Dispatcher $events | ||
* @return void | ||
* | ||
* @throws \Turahe\Likeable\Exceptions\ModelInvalidException | ||
*/ | ||
public function handle(Dispatcher $events) | ||
{ | ||
$model = $this->argument('model'); | ||
$this->likeType = $this->argument('type'); | ||
$this->service = app(LikeableServiceContract::class); | ||
|
||
if (empty($model)) { | ||
$this->recountLikesOfAllModelTypes(); | ||
} else { | ||
$this->recountLikesOfModelType($model); | ||
} | ||
} | ||
|
||
/** | ||
* Recount likes of all model types. | ||
* | ||
* @return void | ||
* | ||
* @throws \Turahe\Likeable\Exceptions\ModelInvalidException | ||
*/ | ||
protected function recountLikesOfAllModelTypes() | ||
{ | ||
$likeableTypes = app(LikeContract::class)->groupBy('likeable_type')->get(); | ||
foreach ($likeableTypes as $like) { | ||
$this->recountLikesOfModelType($like->likeable_type); | ||
} | ||
} | ||
|
||
/** | ||
* Recount likes of model type. | ||
* | ||
* @param string $modelType | ||
* @return void | ||
* | ||
* @throws \Turahe\Likeable\Exceptions\ModelInvalidException | ||
*/ | ||
protected function recountLikesOfModelType($modelType) | ||
{ | ||
$modelType = $this->normalizeModelType($modelType); | ||
|
||
$counters = $this->service->fetchLikesCounters($modelType, $this->likeType); | ||
|
||
$this->service->removeLikeCountersOfType($modelType, $this->likeType); | ||
|
||
DB::table(app(LikeCounterContract::class)->getTable())->insert($counters); | ||
|
||
$this->info('All [' . $modelType . '] records likes has been recounted.'); | ||
} | ||
|
||
/** | ||
* Normalize likeable model type. | ||
* | ||
* @param string $modelType | ||
* @return string | ||
* | ||
* @throws \Turahe\Likeable\Exceptions\ModelInvalidException | ||
*/ | ||
protected function normalizeModelType($modelType) | ||
{ | ||
$morphMap = Relation::morphMap(); | ||
|
||
if (class_exists($modelType)) { | ||
$model = new $modelType; | ||
$modelType = $model->getMorphClass(); | ||
} else { | ||
if (!isset($morphMap[$modelType])) { | ||
throw new ModelInvalidException("[$modelType] class and morph map are not found."); | ||
} | ||
|
||
$modelClass = $morphMap[$modelType]; | ||
$model = new $modelClass; | ||
} | ||
|
||
if (!$model instanceof LikeableContract) { | ||
throw new ModelInvalidException("[$modelType] not implements Likeable contract."); | ||
} | ||
|
||
return $modelType; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
|
||
namespace Turahe\Likeable\Contracts; | ||
/** | ||
* Interface Like. | ||
* | ||
* @property \Turahe\Likeable\Contracts\Likeable likeable | ||
* @property int type_id | ||
* @property int user_id | ||
* @package Turahe\Likeable\Contract | ||
*/ | ||
|
||
interface Like | ||
{ | ||
/** | ||
* Likeable model relation. | ||
* | ||
* @return \Illuminate\Database\Eloquent\Relations\MorphTo | ||
*/ | ||
public function likeable(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
|
||
namespace Turahe\Likeable\Contracts; | ||
|
||
/** | ||
* Interface LikeCounter. | ||
* | ||
* @property int type_id | ||
* @property int count | ||
* @package Turahe\Likeable\Contracts | ||
*/ | ||
interface LikeCounter | ||
{ | ||
/** | ||
* Likeable model relation. | ||
* | ||
* @return \Illuminate\Database\Eloquent\Relations\MorphTo | ||
*/ | ||
public function likeable(); | ||
|
||
} |
Oops, something went wrong.