Skip to content
This repository was archived by the owner on Jan 10, 2020. It is now read-only.

Commit 2644cd0

Browse files
committed
New activity package
1 parent bf07721 commit 2644cd0

File tree

7 files changed

+274
-0
lines changed

7 files changed

+274
-0
lines changed

src/Console/Activity.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace Yab\Laracogs\Console;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Filesystem\Filesystem;
7+
use Yab\Laracogs\Traits\FileMakerTrait;
8+
9+
class Activity extends Command
10+
{
11+
use FileMakerTrait;
12+
13+
/**
14+
* The console command name.
15+
*
16+
* @var string
17+
*/
18+
protected $signature = 'laracogs:activity';
19+
20+
/**
21+
* The console command description.
22+
*
23+
* @var string
24+
*/
25+
protected $description = 'Add an activity tracker for your users';
26+
27+
/**
28+
* Execute the console command.
29+
*
30+
* @return mixed
31+
*/
32+
public function handle()
33+
{
34+
if (!file_exists(base_path('resources/views/team/create.blade.php'))) {
35+
$this->line("\n\nPlease perform the starter command:\n");
36+
$this->info("\n\nphp artisan laracogs:starter\n");
37+
$this->line("\n\nThen one you're able to run the unit tests successfully re-run this command, to bootstrap your app :)\n");
38+
} else {
39+
$fileSystem = new Filesystem();
40+
41+
$files = $fileSystem->allFiles(__DIR__.'/../Packages/Activity');
42+
$this->line("\n");
43+
foreach ($files as $file) {
44+
$this->line(str_replace(__DIR__.'/../Packages/Activity/', '', $file));
45+
}
46+
47+
$this->info("\n\nThese files will be published\n");
48+
49+
$result = $this->confirm('Are you sure you want to overwrite any files of the same name?');
50+
51+
if ($result) {
52+
$this->copyPreparedFiles(__DIR__.'/../Packages/Activity', base_path());
53+
$this->appendTheFactory();
54+
55+
$this->info("\n\n Please review the setup details for activity tracking.");
56+
$this->line("\n You need to add the activity service provider to your app config:");
57+
$this->comment("\n App\Providers\ActivityServiceProvider::class,");
58+
$this->line("\n You will also need to the activity middleware to the Kernel in the \$routeMiddleware array:");
59+
$this->comment("\n 'activity' => \App\Http\Middleware\Activity::class,");
60+
$this->line("\n Now to track all you need to do is add the middleware `activity` to your routes");
61+
$this->line("\n In order to get a user's activities use this:");
62+
$this->comment("\n app(App\Services\ActivityService::class)->getByUser(\$userId);");
63+
$this->info("\n Finished setting up activity");
64+
} else {
65+
$this->info("\n You cancelled the laracogs activity");
66+
}
67+
}
68+
}
69+
70+
public function appendTheFactory()
71+
{
72+
$activityPrepared = '
73+
/*
74+
|--------------------------------------------------------------------------
75+
| Activity Factory
76+
|--------------------------------------------------------------------------
77+
*/
78+
79+
$factory->define('.$this->getAppNamespace()."Models\Activity::class, function (Faker\Generator \$faker) {
80+
return [
81+
'id' => 1,
82+
'user_id' => 1,
83+
'description' => 'Standard User Activity',
84+
'request' => [],
85+
];
86+
});
87+
";
88+
89+
$factoryMaster = base_path('database/factories/ModelFactory.php');
90+
91+
return file_put_contents($factoryMaster, $activityPrepared, FILE_APPEND);
92+
}
93+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace {{App\}}Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
class Activity extends Facade
8+
{
9+
/**
10+
* Create the Facade
11+
*
12+
* @return string
13+
*/
14+
protected static function getFacadeAccessor() { return 'ActivityService'; }
15+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace {{App\}}Http\Middleware;
4+
5+
use Closure;
6+
use Illuminate\Contracts\Auth\Guard;
7+
8+
class Activity
9+
{
10+
/**
11+
* Handle an incoming request.
12+
*
13+
* @param \Illuminate\Http\Request $request
14+
* @param \Closure $next
15+
* @return mixed
16+
*/
17+
public function handle($request, Closure $next)
18+
{
19+
activity('Standard User Action');
20+
21+
return $next($request);
22+
}
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace {{App\}}Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Activity extends Model
8+
{
9+
public $table = "activities";
10+
11+
public $primaryKey = "id";
12+
13+
public $timestamps = true;
14+
15+
public $fillable = [
16+
'user_id',
17+
'description',
18+
'request',
19+
];
20+
21+
public static $rules = [
22+
'request' => 'required',
23+
];
24+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace {{App\}}Providers;
4+
5+
use Illuminate\Support\Facades\Blade;
6+
use Illuminate\Support\ServiceProvider;
7+
8+
class ActivityServiceProvider extends ServiceProvider
9+
{
10+
/**
11+
* Bootstrap any application services.
12+
*/
13+
public function boot()
14+
{
15+
if (function_exists('activity')) {
16+
function activity($description)
17+
{
18+
return Activity::log($description);
19+
}
20+
}
21+
}
22+
23+
/**
24+
* Register any application services.
25+
*/
26+
public function register()
27+
{
28+
$loader = \Illuminate\Foundation\AliasLoader::getInstance();
29+
30+
$loader->alias('Activity', \App\Facades\Activity::class);
31+
32+
$this->app->singleton('ActivityService', function ($app) {
33+
return app(\App\Services\ActivityService::class);
34+
});
35+
}
36+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace {{App\}}Services;
4+
5+
use {{App\}}Models\Activity;
6+
use Illuminate\Support\Facades\Schema;
7+
8+
class ActivityService
9+
{
10+
public function __construct(Activity $model)
11+
{
12+
$this->model = $model;
13+
}
14+
15+
/**
16+
* All activities
17+
*
18+
* @return Collection
19+
*/
20+
public function getByUser($userId, $paginate = null)
21+
{
22+
$query = $this->model->where('user', $userId);
23+
24+
if (!is_null($paginate)) {
25+
return $query->paginate($paginate);
26+
}
27+
28+
return $query->get();
29+
}
30+
31+
/**
32+
* Create an activity record
33+
*
34+
* @param string $description
35+
* @return Activity
36+
*/
37+
public function log($description = '')
38+
{
39+
$payload = [
40+
'user_id' => auth()->id(),
41+
'description' => $description,
42+
'request' => [
43+
'url' => request()->url(),
44+
'method' => request()->method(),
45+
'query' => request()->fullUrlWithQuery(),
46+
'secure' => request()->secure(),
47+
'client_ip' => request()->ip(),
48+
'payload' => request()->all(),
49+
],
50+
];
51+
52+
return $this->model->create($payload);
53+
}
54+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class CreateActivityTable extends Migration
7+
{
8+
/**
9+
* Run the migrations.
10+
*/
11+
public function up()
12+
{
13+
Schema::create('activities', function (Blueprint $table) {
14+
$table->increments('id');
15+
$table->integer('user_id');
16+
$table->string('description');
17+
$table->text('request');
18+
$table->timestamps();
19+
});
20+
}
21+
22+
/**
23+
* Reverse the migrations.
24+
*/
25+
public function down()
26+
{
27+
Schema::drop('activities');
28+
}
29+
}

0 commit comments

Comments
 (0)