Skip to content

Commit

Permalink
refactor custom activitymodel
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Aug 11, 2016
1 parent 818bfa9 commit a47aeb2
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 61 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

All Notable changes to `spatie/laravel-activitylog` will be documented in this file

## 1.4.0 - 2016-08-11
- Added support for a custom model

## 1.4.0 - 2016-08-10
- Added support for soft deletes

Expand Down
5 changes: 2 additions & 3 deletions config/laravel-activitylog.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@
'subject_returns_soft_deleted_models' => false,

/**
* You can use your own Activity-model. However, it is required to
* extend the Spatie\Activitylog\Models\Activity model.
* Example: \App\Activity::class
* Specify the activity model to be used here. The only requirement is that
* it shoud extend the Spatie\Activitylog\Models\Activity model.
*/
'activity_model' => \Spatie\Activitylog\Models\Activity::class,
];
30 changes: 18 additions & 12 deletions src/ActivityLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Illuminate\Contracts\Config\Repository;
use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\Exceptions\CouldNotLogActivity;
use Spatie\Activitylog\Exceptions\ModelMismatchException;
use Spatie\Activitylog\Exceptions\InvalidConfiguration;
use Spatie\Activitylog\Models\Activity;

class ActivityLogger
Expand Down Expand Up @@ -34,17 +34,7 @@ public function __construct(AuthManager $auth, Repository $config)

$this->properties = collect();

$model = config('laravel-activitylog.activity_model');

if ($model == null) {
throw new ModelMismatchException('Model not set in laravel-activitylog.php');
}

if ((!($model instanceof Activity) && !($model == Activity::class)) && (!(is_subclass_of($model, Activity::class)))) {
throw new ModelMismatchException("Model `{$model}` is not extending \\Spatie\\Activitylog\\Models\\Activity");
}

$this->activityModel = config('laravel-activitylog.activity_model') ?? Activity::class;
$this->activityModel = $this->determineActivityModel();

$authDriver = $config['laravel-activitylog']['default_auth_driver'] ?? $auth->getDefaultDriver();

Expand Down Expand Up @@ -190,4 +180,20 @@ public function getActivityModel() : string
{
return $this->activityModel;
}

/**
* @return \Illuminate\Database\Eloquent\Model
*
* @throws \Spatie\Activitylog\Exceptions\InvalidConfiguration
*/
protected function determineActivityModel()
{
$activityModel = config('laravel-activitylog.activity_model') ?? Activity::class;

if (!is_a($activityModel, Activity::class, true)) {
throw InvalidConfiguration::modelIsNotValid($activityModel);
}

return $activityModel;
}
}
14 changes: 14 additions & 0 deletions src/Exceptions/InvalidConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Spatie\Activitylog\Exceptions;

use Exception;
use Spatie\Activitylog\Models\Activity;

class InvalidConfiguration extends Exception
{
public static function modelIsNotValid(string $className)
{
return new static("The given model class `$className` does not extend `".Activity::class.'`');
}
}
9 changes: 0 additions & 9 deletions src/Exceptions/ModelMismatchException.php

This file was deleted.

52 changes: 17 additions & 35 deletions tests/CustomActivityModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Spatie\Activitylog\Test;

use Spatie\Activitylog\Exceptions\ModelMismatchException;
use Spatie\Activitylog\Test\Models\MyActivity;
use Spatie\Activitylog\Test\Models\TestActivityModel;
use Spatie\Activitylog\Exceptions\InvalidConfiguration;
use Spatie\Activitylog\Test\Models\CustomActivityModel;
use Spatie\Activitylog\Test\Models\InvalidActivityModel;

class CustomActivityModelTest extends TestCase
{
Expand All @@ -22,50 +22,32 @@ public function setUp()
});
}

/**
* @test
*/
public function it_can_log_an_activity()
{
$this->app['config']->set('laravel-activitylog.activity_model', MyActivity::class);
$activity = activity()->log($this->activityDescription);
$this->assertEquals($this->activityDescription, $this->getLastActivity()->description);
$this->assertEquals('Spatie\\Activitylog\\Test\\Models\\MyActivity', $activity->getActivityModel());
}

/** @test */
public function it_provides_a_scope_to_get_activities_from_a_specific_log()
public function it_can_log_activity_using_a_custom_model()
{
$activityInLog3 = MyActivity::inLog('log3')->get();
$this->app['config']->set('laravel-activitylog.activity_model', CustomActivityModel::class);

$this->assertCount(1, $activityInLog3);
$activity = activity()->log($this->activityDescription);

$this->assertEquals('log3', $activityInLog3->first()->log_name);
$this->assertEquals($this->activityDescription, $this->getLastActivity()->description);
$this->assertEquals(CustomActivityModel::class, $activity->getActivityModel());
}

/**
* @test
*/
public function it_throws_an_exception_when_model_config_is_null()
/** @test */
public function it_does_not_throw_an_exception_when_model_config_is_null()
{
$this->app['config']->set('laravel-activitylog.activity_model', null);
try {
activity()->log($this->activityDescription);
$this->fail('Exception not being thrown');
} catch (ModelMismatchException $e) {
$this->assertEquals('Model not set in laravel-activitylog.php', $e->getMessage());
}

activity()->log($this->activityDescription);
}

/** @test */
public function it_throws_an_exception_when_model_doesnt_extend_package_model()
{
$this->app['config']->set('laravel-activitylog.activity_model', TestActivityModel::class);
try {
activity()->log($this->activityDescription);
$this->fail('Exception not being thrown');
} catch (ModelMismatchException $e) {
$this->assertEquals('Model `Spatie\\Activitylog\\Test\\Models\\TestActivityModel` is not extending \\Spatie\\Activitylog\\Models\\Activity', $e->getMessage());
}
$this->app['config']->set('laravel-activitylog.activity_model', InvalidActivityModel::class);

$this->expectException(InvalidConfiguration::class);

activity()->log($this->activityDescription);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

use Spatie\Activitylog\Models\Activity;

class MyActivity extends Activity
class CustomActivityModel extends Activity
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

namespace Spatie\Activitylog\Test\Models;

class TestActivityModel
class InvalidActivityModel
{
}

0 comments on commit a47aeb2

Please sign in to comment.