From ae8167637b15832c3473314f3473e841cb95f3bc Mon Sep 17 00:00:00 2001 From: Ian Morland Date: Wed, 20 Jan 2021 00:52:12 +0000 Subject: [PATCH 1/6] Add Created and Deleting events, deprecare FlagsWillBeDeleted event --- src/Command/CreateFlagHandler.php | 12 ++++++++- src/Command/DeleteFlagsHandler.php | 6 +++++ src/Event/Created.php | 43 ++++++++++++++++++++++++++++++ src/Event/Deleting.php | 43 ++++++++++++++++++++++++++++++ src/Event/FlagsWillBeDeleted.php | 4 +++ 5 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 src/Event/Created.php create mode 100644 src/Event/Deleting.php diff --git a/src/Command/CreateFlagHandler.php b/src/Command/CreateFlagHandler.php index 9be4363..00d48ae 100644 --- a/src/Command/CreateFlagHandler.php +++ b/src/Command/CreateFlagHandler.php @@ -9,12 +9,14 @@ namespace Flarum\Flags\Command; +use Flarum\Flags\Event\Created; use Flarum\Flags\Flag; use Flarum\Foundation\ValidationException; use Flarum\Post\CommentPost; use Flarum\Post\PostRepository; use Flarum\Settings\SettingsRepositoryInterface; use Flarum\User\Exception\PermissionDeniedException; +use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Support\Arr; use Symfony\Component\Translation\TranslatorInterface; use Tobscure\JsonApi\Exception\InvalidParameterException; @@ -36,15 +38,21 @@ class CreateFlagHandler */ protected $settings; + /** + * @var Dispatcher + */ + protected $events; + /** * @param PostRepository $posts * @param TranslatorInterface $translator */ - public function __construct(PostRepository $posts, TranslatorInterface $translator, SettingsRepositoryInterface $settings) + public function __construct(PostRepository $posts, TranslatorInterface $translator, SettingsRepositoryInterface $settings, Dispatcher $events) { $this->posts = $posts; $this->translator = $translator; $this->settings = $settings; + $this->events = $events; } /** @@ -93,6 +101,8 @@ public function handle(CreateFlag $command) $flag->save(); + $this->events->dispatch(new Created($flag, $actor, $command->data)); + return $flag; } } diff --git a/src/Command/DeleteFlagsHandler.php b/src/Command/DeleteFlagsHandler.php index 06a4057..5ad950f 100644 --- a/src/Command/DeleteFlagsHandler.php +++ b/src/Command/DeleteFlagsHandler.php @@ -9,6 +9,8 @@ namespace Flarum\Flags\Command; +use Flarum\Flags\Event\Deleted; +use Flarum\Flags\Event\Deleting; use Flarum\Flags\Event\FlagsWillBeDeleted; use Flarum\Flags\Flag; use Flarum\Post\PostRepository; @@ -50,6 +52,10 @@ public function handle(DeleteFlags $command) $this->events->dispatch(new FlagsWillBeDeleted($post, $actor, $command->data)); + foreach ($post->flags() as $flag) { + $this->events->dispatch(new Deleting($flag, $actor, $command->data)); + } + $post->flags()->delete(); return $post; diff --git a/src/Event/Created.php b/src/Event/Created.php new file mode 100644 index 0000000..8c29498 --- /dev/null +++ b/src/Event/Created.php @@ -0,0 +1,43 @@ +flag = $flag; + $this->actor = $actor; + $this->data = $data; + } +} diff --git a/src/Event/Deleting.php b/src/Event/Deleting.php new file mode 100644 index 0000000..ae7e585 --- /dev/null +++ b/src/Event/Deleting.php @@ -0,0 +1,43 @@ +flag = $flag; + $this->actor = $actor; + $this->data = $data; + } +} diff --git a/src/Event/FlagsWillBeDeleted.php b/src/Event/FlagsWillBeDeleted.php index 489a76e..127140f 100644 --- a/src/Event/FlagsWillBeDeleted.php +++ b/src/Event/FlagsWillBeDeleted.php @@ -12,6 +12,10 @@ use Flarum\Post\Post; use Flarum\User\User; +/** + * @deprecated 0.1.0-beta.16, remove 0.1.0-beta.17 + * Listen for Flarum\Flags\Event\Deleting instead + */ class FlagsWillBeDeleted { /** From de6cee1b9fff5069733dcacc19b06b0309abe72a Mon Sep 17 00:00:00 2001 From: Ian Morland Date: Wed, 20 Jan 2021 00:55:55 +0000 Subject: [PATCH 2/6] Tidy up --- src/Command/CreateFlagHandler.php | 2 ++ src/Command/DeleteFlagsHandler.php | 3 +-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Command/CreateFlagHandler.php b/src/Command/CreateFlagHandler.php index 00d48ae..0b3b5ee 100644 --- a/src/Command/CreateFlagHandler.php +++ b/src/Command/CreateFlagHandler.php @@ -46,6 +46,8 @@ class CreateFlagHandler /** * @param PostRepository $posts * @param TranslatorInterface $translator + * @param SettingsRepositoryInterface $settings + * @param Dispatcher $events */ public function __construct(PostRepository $posts, TranslatorInterface $translator, SettingsRepositoryInterface $settings, Dispatcher $events) { diff --git a/src/Command/DeleteFlagsHandler.php b/src/Command/DeleteFlagsHandler.php index 5ad950f..3d1ff05 100644 --- a/src/Command/DeleteFlagsHandler.php +++ b/src/Command/DeleteFlagsHandler.php @@ -9,7 +9,6 @@ namespace Flarum\Flags\Command; -use Flarum\Flags\Event\Deleted; use Flarum\Flags\Event\Deleting; use Flarum\Flags\Event\FlagsWillBeDeleted; use Flarum\Flags\Flag; @@ -55,7 +54,7 @@ public function handle(DeleteFlags $command) foreach ($post->flags() as $flag) { $this->events->dispatch(new Deleting($flag, $actor, $command->data)); } - + $post->flags()->delete(); return $post; From bab12d1af97382f4bf5be427a93dd188c1fb1367 Mon Sep 17 00:00:00 2001 From: Ian Morland Date: Thu, 21 Jan 2021 20:42:49 +0000 Subject: [PATCH 3/6] Cleanup and make it work --- src/Command/CreateFlagHandler.php | 4 ++-- src/Command/DeleteFlagsHandler.php | 7 ++++--- src/Event/Created.php | 9 +-------- src/Event/Deleting.php | 9 +-------- 4 files changed, 8 insertions(+), 21 deletions(-) diff --git a/src/Command/CreateFlagHandler.php b/src/Command/CreateFlagHandler.php index 0b3b5ee..6fa88d3 100644 --- a/src/Command/CreateFlagHandler.php +++ b/src/Command/CreateFlagHandler.php @@ -16,7 +16,7 @@ use Flarum\Post\PostRepository; use Flarum\Settings\SettingsRepositoryInterface; use Flarum\User\Exception\PermissionDeniedException; -use Illuminate\Contracts\Events\Dispatcher; +use Illuminate\Events\Dispatcher; use Illuminate\Support\Arr; use Symfony\Component\Translation\TranslatorInterface; use Tobscure\JsonApi\Exception\InvalidParameterException; @@ -103,7 +103,7 @@ public function handle(CreateFlag $command) $flag->save(); - $this->events->dispatch(new Created($flag, $actor, $command->data)); + $this->events->dispatch(new Created($flag, $actor)); return $flag; } diff --git a/src/Command/DeleteFlagsHandler.php b/src/Command/DeleteFlagsHandler.php index 3d1ff05..3b65ce0 100644 --- a/src/Command/DeleteFlagsHandler.php +++ b/src/Command/DeleteFlagsHandler.php @@ -13,7 +13,7 @@ use Flarum\Flags\Event\FlagsWillBeDeleted; use Flarum\Flags\Flag; use Flarum\Post\PostRepository; -use Illuminate\Contracts\Events\Dispatcher; +use Illuminate\Events\Dispatcher; class DeleteFlagsHandler { @@ -49,10 +49,11 @@ public function handle(DeleteFlags $command) $actor->assertCan('viewFlags', $post->discussion); + // remove beta 17 $this->events->dispatch(new FlagsWillBeDeleted($post, $actor, $command->data)); - foreach ($post->flags() as $flag) { - $this->events->dispatch(new Deleting($flag, $actor, $command->data)); + foreach ($post->flags as $flag) { + $this->events->dispatch(new Deleting($flag, $actor)); } $post->flags()->delete(); diff --git a/src/Event/Created.php b/src/Event/Created.php index 8c29498..d2d6028 100644 --- a/src/Event/Created.php +++ b/src/Event/Created.php @@ -24,20 +24,13 @@ class Created */ public $actor; - /** - * @var array - */ - public $data; - /** * @param Flag $flag * @param User $actor - * @param array $data */ - public function __construct(Flag $flag, User $actor, array $data = []) + public function __construct(Flag $flag, User $actor) { $this->flag = $flag; $this->actor = $actor; - $this->data = $data; } } diff --git a/src/Event/Deleting.php b/src/Event/Deleting.php index ae7e585..52afec9 100644 --- a/src/Event/Deleting.php +++ b/src/Event/Deleting.php @@ -24,20 +24,13 @@ class Deleting */ public $actor; - /** - * @var array - */ - public $data; - /** * @param Flag $flag * @param User $actor - * @param array $data */ - public function __construct(Flag $flag, User $actor, array $data = []) + public function __construct(Flag $flag, User $actor) { $this->flag = $flag; $this->actor = $actor; - $this->data = $data; } } From ffaf547a1a5d10101e62ee76e1fb8bfd238cb477 Mon Sep 17 00:00:00 2001 From: Ian Morland Date: Thu, 21 Jan 2021 20:51:40 +0000 Subject: [PATCH 4/6] styleci --- src/Event/FlagsWillBeDeleted.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Event/FlagsWillBeDeleted.php b/src/Event/FlagsWillBeDeleted.php index 127140f..9a52658 100644 --- a/src/Event/FlagsWillBeDeleted.php +++ b/src/Event/FlagsWillBeDeleted.php @@ -13,7 +13,7 @@ use Flarum\User\User; /** - * @deprecated 0.1.0-beta.16, remove 0.1.0-beta.17 + * @deprecated 0.1.0-beta.16, remove 0.1.0-beta.17 * Listen for Flarum\Flags\Event\Deleting instead */ class FlagsWillBeDeleted From f90ed1864ef46dedea9dd81696f6d98e1f48cc0e Mon Sep 17 00:00:00 2001 From: Ian Morland Date: Thu, 21 Jan 2021 20:52:16 +0000 Subject: [PATCH 5/6] styleci again --- src/Event/FlagsWillBeDeleted.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Event/FlagsWillBeDeleted.php b/src/Event/FlagsWillBeDeleted.php index 9a52658..aea4441 100644 --- a/src/Event/FlagsWillBeDeleted.php +++ b/src/Event/FlagsWillBeDeleted.php @@ -12,7 +12,7 @@ use Flarum\Post\Post; use Flarum\User\User; -/** +/** * @deprecated 0.1.0-beta.16, remove 0.1.0-beta.17 * Listen for Flarum\Flags\Event\Deleting instead */ From c3a296aa7daefd2f48ddb96fe6d62a63f9c0b5aa Mon Sep 17 00:00:00 2001 From: Ian Morland Date: Sat, 23 Jan 2021 23:25:56 +0000 Subject: [PATCH 6/6] Add back to events --- src/Command/CreateFlagHandler.php | 2 +- src/Command/DeleteFlagsHandler.php | 2 +- src/Event/Created.php | 9 ++++++++- src/Event/Deleting.php | 9 ++++++++- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Command/CreateFlagHandler.php b/src/Command/CreateFlagHandler.php index 6fa88d3..10a4fce 100644 --- a/src/Command/CreateFlagHandler.php +++ b/src/Command/CreateFlagHandler.php @@ -103,7 +103,7 @@ public function handle(CreateFlag $command) $flag->save(); - $this->events->dispatch(new Created($flag, $actor)); + $this->events->dispatch(new Created($flag, $actor, $data)); return $flag; } diff --git a/src/Command/DeleteFlagsHandler.php b/src/Command/DeleteFlagsHandler.php index 3b65ce0..d38f3e0 100644 --- a/src/Command/DeleteFlagsHandler.php +++ b/src/Command/DeleteFlagsHandler.php @@ -53,7 +53,7 @@ public function handle(DeleteFlags $command) $this->events->dispatch(new FlagsWillBeDeleted($post, $actor, $command->data)); foreach ($post->flags as $flag) { - $this->events->dispatch(new Deleting($flag, $actor)); + $this->events->dispatch(new Deleting($flag, $actor, $command->data)); } $post->flags()->delete(); diff --git a/src/Event/Created.php b/src/Event/Created.php index d2d6028..8c29498 100644 --- a/src/Event/Created.php +++ b/src/Event/Created.php @@ -24,13 +24,20 @@ class Created */ public $actor; + /** + * @var array + */ + public $data; + /** * @param Flag $flag * @param User $actor + * @param array $data */ - public function __construct(Flag $flag, User $actor) + public function __construct(Flag $flag, User $actor, array $data = []) { $this->flag = $flag; $this->actor = $actor; + $this->data = $data; } } diff --git a/src/Event/Deleting.php b/src/Event/Deleting.php index 52afec9..ae7e585 100644 --- a/src/Event/Deleting.php +++ b/src/Event/Deleting.php @@ -24,13 +24,20 @@ class Deleting */ public $actor; + /** + * @var array + */ + public $data; + /** * @param Flag $flag * @param User $actor + * @param array $data */ - public function __construct(Flag $flag, User $actor) + public function __construct(Flag $flag, User $actor, array $data = []) { $this->flag = $flag; $this->actor = $actor; + $this->data = $data; } }