-
Notifications
You must be signed in to change notification settings - Fork 3
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
116 changed files
with
4,484 additions
and
13,962 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
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,151 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use Carbon\Carbon; | ||
use Closure; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Indigo\Contracts\Counter; | ||
use Indigo\Contracts\Viewable; | ||
|
||
/** | ||
* Class SaveCounter | ||
* @package App\Console\Commands | ||
*/ | ||
class SaveCounter extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'counter:save {viewable* : The array of Viewable}'; | ||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Save posts view count into database and flush cache'; | ||
/** | ||
* @var \Indigo\Contracts\Counter | ||
*/ | ||
private $counter; | ||
/** | ||
* @var array | ||
*/ | ||
private $viewableMaps = []; | ||
|
||
/** | ||
* Create a new command instance. | ||
* | ||
* @param \Indigo\Contracts\Counter $counter | ||
*/ | ||
public function __construct(Counter $counter) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->counter = $counter; | ||
} | ||
|
||
/** | ||
* Execute the console command. | ||
*/ | ||
public function handle() | ||
{ | ||
$viewableArray = $this->argument('viewable'); | ||
|
||
// Validate arguments | ||
$this->validate($viewableArray); | ||
|
||
$this->iterateViewable(function ($viewable) { | ||
$stats = $this->getStats($viewable); | ||
|
||
$this->updateCount($viewable, (array)$stats); | ||
|
||
$this->resetStats($viewable); | ||
}); | ||
} | ||
|
||
/** | ||
* @param $viewableArray | ||
*/ | ||
private function validate($viewableArray) | ||
{ | ||
foreach ($viewableArray as $className) { | ||
$instance = resolve($className); | ||
if (!$instance instanceof Viewable || !$instance instanceof Model) { | ||
$this->error("{$className} is not implement with " . Viewable::class); | ||
exit; | ||
} | ||
$this->setViewableMaps($className, $instance); | ||
} | ||
} | ||
|
||
/** | ||
* @param $name | ||
* @param $instance | ||
* @return $this | ||
*/ | ||
private function setViewableMaps($name, $instance) | ||
{ | ||
$this->viewableMaps[$name] = $instance; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param \Closure $callback | ||
*/ | ||
private function iterateViewable(Closure $callback) | ||
{ | ||
foreach ($this->viewableMaps as $instance) { | ||
$callback($instance); | ||
} | ||
} | ||
|
||
/** | ||
* @param $key | ||
* @return mixed | ||
*/ | ||
private function getStats($key) | ||
{ | ||
return $this->counter->getAll($key); | ||
} | ||
|
||
/** | ||
* @param \Indigo\Contracts\Viewable|\Illuminate\Database\Eloquent\Model $viewable | ||
* @param array $data | ||
*/ | ||
private function updateCount($viewable, array $data = []) | ||
{ | ||
$tableData = []; | ||
|
||
foreach ($data as $identifier => $increment) { | ||
$viewable->newQuery()->where($viewable->getKeyName(), $identifier)->increment($viewable->getCountField(), | ||
$increment); | ||
array_push($tableData, [$identifier, $increment]); | ||
} | ||
|
||
$this->success(get_class($viewable), $tableData); | ||
} | ||
|
||
/** | ||
* @param $className | ||
* @param $tableData | ||
*/ | ||
private function success($className, $tableData) | ||
{ | ||
$this->info("Save count of {$className} successfully at " . Carbon::now()->toDateTimeString()); | ||
$this->table(['ID', 'Increment'], $tableData); | ||
} | ||
|
||
/** | ||
* @param $viewable | ||
* @return bool | ||
*/ | ||
private function resetStats($viewable) | ||
{ | ||
return $this->counter->resetAll($viewable); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.