diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d3ed2e9..dd500e16 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/config/laravel-activitylog.php b/config/laravel-activitylog.php index 0599d98c..72e25ce0 100644 --- a/config/laravel-activitylog.php +++ b/config/laravel-activitylog.php @@ -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, ]; diff --git a/src/ActivityLogger.php b/src/ActivityLogger.php index 7618b58c..3435a100 100644 --- a/src/ActivityLogger.php +++ b/src/ActivityLogger.php @@ -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 @@ -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(); @@ -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; + } } diff --git a/src/Exceptions/InvalidConfiguration.php b/src/Exceptions/InvalidConfiguration.php new file mode 100644 index 00000000..9d1e6345 --- /dev/null +++ b/src/Exceptions/InvalidConfiguration.php @@ -0,0 +1,14 @@ +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); } } diff --git a/tests/Models/MyActivity.php b/tests/Models/CustomActivityModel.php similarity index 68% rename from tests/Models/MyActivity.php rename to tests/Models/CustomActivityModel.php index 1ef6013c..ac094d1e 100644 --- a/tests/Models/MyActivity.php +++ b/tests/Models/CustomActivityModel.php @@ -4,6 +4,6 @@ use Spatie\Activitylog\Models\Activity; -class MyActivity extends Activity +class CustomActivityModel extends Activity { } diff --git a/tests/Models/TestActivityModel.php b/tests/Models/InvalidActivityModel.php similarity index 66% rename from tests/Models/TestActivityModel.php rename to tests/Models/InvalidActivityModel.php index 11e6399b..43392c27 100644 --- a/tests/Models/TestActivityModel.php +++ b/tests/Models/InvalidActivityModel.php @@ -2,6 +2,6 @@ namespace Spatie\Activitylog\Test\Models; -class TestActivityModel +class InvalidActivityModel { }