REFACTORING #1880 @20m: move action log logic into repository

This commit is contained in:
Michal Kváček
2016-11-23 01:03:37 +01:00
parent 2fffea39fa
commit 3aabb8fa7b
5 changed files with 37 additions and 41 deletions
@@ -1,10 +1,33 @@
<?php <?php
namespace App\Models\ActionLog\ModelTraits; namespace App\Cms\Repositories\ActionLog;
use App\Models\User;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
trait ActionLog class Actions
{ {
/**
* @param null $userId
*
* @return array
*/
public function getLoggedActions($userId = null)
{
$actions = $this->getLogActionsQuery(true);
$user = null;
if ($userId) {
$user = User::findOrFail($userId);
$actions->where('users.id', $userId);
}
$actions = $actions->paginate(25);
$this->translateLogMessages($actions);
return [$user, $actions];
}
/** /**
* Query factory for loading action logs with their type (therefore with name and value) * Query factory for loading action logs with their type (therefore with name and value)
* *
@@ -58,8 +81,6 @@ trait ActionLog
* Method used for translating and filling parameters into value of log. * Method used for translating and filling parameters into value of log.
* *
* @param $messages * @param $messages
*
* @return array
*/ */
public function translateLogMessages(&$messages) public function translateLogMessages(&$messages)
{ {
@@ -2,49 +2,22 @@
namespace App\Http\Controllers\Admin\ActionLog; namespace App\Http\Controllers\Admin\ActionLog;
use App\Cms\Repositories\ActionLog\Actions;
use App\Http\Controllers\Admin\Controller; use App\Http\Controllers\Admin\Controller;
use App\Models\ActionLog\ActionLog;
use App\Models\User;
use Illuminate\Support\Facades\Input; use Illuminate\Support\Facades\Input;
class Overview extends Controller class Overview extends Controller
{ {
/**
* @var ActionLog
*/
private $log;
/**
* ActionLog constructor.
*
* @param ActionLog $log
*/
public function __construct(ActionLog $log)
{
parent::__construct();
$this->log = $log;
}
/** /**
* Get overview of logged actions * Get overview of logged actions
* *
* @param Actions $repository
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/ */
public function getOverview() public function index(Actions $repository)
{ {
$user = null; list($user, $actions) = $repository->getLoggedActions(Input::get('userId'));
$userId = Input::get('userId');
$actions = $this->log->getLogActionsQuery(true);
if ($userId) {
$user = User::findOrFail($userId);
$actions->where('users.id', $userId);
}
$actions = $actions->paginate(25);
$this->log->translateLogMessages($actions);
return view('admin.actionLog.overview', ['actions' => $actions, 'user' => $user]); return view('admin.actionLog.overview', ['actions' => $actions, 'user' => $user]);
} }
@@ -7,7 +7,7 @@ use Illuminate\Http\Request;
class Dashboard extends Controller class Dashboard extends Controller
{ {
public function getIndex(Request $request) public function index(Request $request)
{ {
return redirect()->route('content.overview'); return redirect()->route('content.overview');
} }
+2 -2
View File
@@ -28,11 +28,11 @@ Route::group(['prefix' => LaravelLocalization::setLocale()], function () {
} }
// display dashboard with some usefull information // display dashboard with some usefull information
Route::get('/', 'Dashboard\Dashboard@getIndex') Route::get('/', 'Dashboard\Dashboard@index')
->name('admin.dashboard'); ->name('admin.dashboard');
// list of all actions // list of all actions
Route::get('actions', 'ActionLog\Overview@getOverview') Route::get('actions', 'ActionLog\Overview@index')
->name('log.overview'); ->name('log.overview');
// search form with results // search form with results
+4 -2
View File
@@ -9,8 +9,6 @@ use Illuminate\Database\Eloquent\Model;
*/ */
class ActionLog extends Model class ActionLog extends Model
{ {
use ModelTraits\ActionLog;
protected $table = 'action_log'; protected $table = 'action_log';
public $timestamps = false; public $timestamps = false;
protected $fillable = [ protected $fillable = [
@@ -20,4 +18,8 @@ class ActionLog extends Model
'user_agent', 'user_agent',
'parameters' 'parameters'
]; ];
protected $casts = [
'parameters' => 'array'
];
} }