Creating and triggering event hooks when tags are creating and updating

parent ef6948a0
<?php
namespace Modules\Tag\Events;
use Modules\Core\Abstracts\AbstractEntityHook;
use Modules\Core\Contracts\EntityIsChanging;
class TagIsCreating extends AbstractEntityHook implements EntityIsChanging
{
}
<?php
namespace Modules\Tag\Events;
use Modules\Core\Abstracts\AbstractEntityHook;
use Modules\Core\Contracts\EntityIsChanging;
use Modules\Tag\Entities\Tag;
class TagIsUpdating extends AbstractEntityHook implements EntityIsChanging
{
/**
* @var Tag
*/
private $tag;
public function __construct(Tag $tag, $attributes)
{
$this->tag = $tag;
parent::__construct($attributes);
}
/**
* @return Tag
*/
public function getTag()
{
return $this->tag;
}
}
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
namespace Modules\Tag\Repositories\Eloquent; namespace Modules\Tag\Repositories\Eloquent;
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository; use Modules\Core\Repositories\Eloquent\EloquentBaseRepository;
use Modules\Tag\Events\TagIsCreating;
use Modules\Tag\Events\TagIsUpdating;
use Modules\Tag\Events\TagWasCreated; use Modules\Tag\Events\TagWasCreated;
use Modules\Tag\Events\TagWasUpdated; use Modules\Tag\Events\TagWasUpdated;
use Modules\Tag\Repositories\TagRepository; use Modules\Tag\Repositories\TagRepository;
...@@ -21,7 +23,8 @@ class EloquentTagRepository extends EloquentBaseRepository implements TagReposit ...@@ -21,7 +23,8 @@ class EloquentTagRepository extends EloquentBaseRepository implements TagReposit
public function create($data) public function create($data)
{ {
$tag = $this->model->create($data); event($event = new TagIsCreating($data));
$tag = $this->model->create($event->getAttributes());
event(new TagWasCreated($tag)); event(new TagWasCreated($tag));
...@@ -30,7 +33,8 @@ class EloquentTagRepository extends EloquentBaseRepository implements TagReposit ...@@ -30,7 +33,8 @@ class EloquentTagRepository extends EloquentBaseRepository implements TagReposit
public function update($tag, $data) public function update($tag, $data)
{ {
$tag->update($data); event($event = new TagIsUpdating($tag, $data));
$tag->update($event->getAttributes());
event(new TagWasUpdated($tag)); event(new TagWasUpdated($tag));
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
namespace Modules\Tag\Tests\Integration; namespace Modules\Tag\Tests\Integration;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Modules\Tag\Events\TagIsCreating;
use Modules\Tag\Events\TagIsUpdating;
use Modules\Tag\Events\TagWasCreated; use Modules\Tag\Events\TagWasCreated;
use Modules\Tag\Events\TagWasUpdated; use Modules\Tag\Events\TagWasUpdated;
use Modules\Tag\Repositories\TagRepository; use Modules\Tag\Repositories\TagRepository;
...@@ -68,6 +70,42 @@ class EloquentTagRepositoryTest extends BaseTestCase ...@@ -68,6 +70,42 @@ class EloquentTagRepositoryTest extends BaseTestCase
}); });
} }
/** @test */
public function it_triggers_event_when_tag_is_creating()
{
Event::fake();
$tag = $this->tag->create([
'namespace' => 'asgardcms/media',
'en' => [
'slug' => 'media-tag',
'name' => 'media tag',
],
]);
Event::assertDispatched(TagIsCreating::class, function ($e) use ($tag) {
return $e->getAttribute('namespace') === $tag->namespace;
});
}
/** @test */
public function it_can_change_data_when_it_is_creating_event()
{
Event::listen(TagIsCreating::class, function (TagIsCreating $event) {
$event->setAttributes(['en' => ['name' => 'MEDIA TAG']]);
});
$tag = $this->tag->create([
'namespace' => 'asgardcms/media',
'en' => [
'slug' => 'media-tag',
'name' => 'media tag',
],
]);
$this->assertEquals('MEDIA TAG', $tag->translate('en')->name);
}
/** @test */ /** @test */
public function it_triggers_event_when_tag_was_updated() public function it_triggers_event_when_tag_was_updated()
{ {
...@@ -86,4 +124,23 @@ class EloquentTagRepositoryTest extends BaseTestCase ...@@ -86,4 +124,23 @@ class EloquentTagRepositoryTest extends BaseTestCase
return $e->tag->id === $tag->id; return $e->tag->id === $tag->id;
}); });
} }
/** @test */
public function it_can_change_data_when_it_is_updating_event()
{
Event::listen(TagIsUpdating::class, function (TagIsUpdating $event) {
$event->setAttributes(['en' => ['name' => 'MEDIA TAG']]);
});
$tag = $this->tag->create([
'namespace' => 'asgardcms/media',
'en' => [
'slug' => 'media-tag',
'name' => 'media tag',
],
]);
$this->tag->update($tag, []);
$this->assertEquals('MEDIA TAG', $tag->translate('en')->name);
}
} }
url: https://github.com/AsgardCms/Platform url: https://github.com/AsgardCms/Platform
versions: versions:
"2.5.0":
added:
- Create and trigger events when tags are created and updated
- Trigger event before a tag is created (<code>TagIsCreating</code>) allow data to be changed
- Trigger event before a tag is updated (<code>TagIsUpdating</code>) allow data to be changed
"2.1.0": "2.1.0":
changed: changed:
- Fixed tags not being removed probably on update & delete - Fixed tags not being removed probably on update & delete
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment