Add Created and Deleting events (#35)

This commit is contained in:
Ian Morland 2021-01-25 13:49:27 +00:00 committed by GitHub
parent 9f065ee973
commit 705ea7f2bc
5 changed files with 110 additions and 2 deletions

View File

@ -9,12 +9,14 @@
namespace Flarum\Flags\Command; namespace Flarum\Flags\Command;
use Flarum\Flags\Event\Created;
use Flarum\Flags\Flag; use Flarum\Flags\Flag;
use Flarum\Foundation\ValidationException; use Flarum\Foundation\ValidationException;
use Flarum\Post\CommentPost; use Flarum\Post\CommentPost;
use Flarum\Post\PostRepository; use Flarum\Post\PostRepository;
use Flarum\Settings\SettingsRepositoryInterface; use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\Exception\PermissionDeniedException; use Flarum\User\Exception\PermissionDeniedException;
use Illuminate\Events\Dispatcher;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Symfony\Component\Translation\TranslatorInterface; use Symfony\Component\Translation\TranslatorInterface;
use Tobscure\JsonApi\Exception\InvalidParameterException; use Tobscure\JsonApi\Exception\InvalidParameterException;
@ -36,15 +38,23 @@ class CreateFlagHandler
*/ */
protected $settings; protected $settings;
/**
* @var Dispatcher
*/
protected $events;
/** /**
* @param PostRepository $posts * @param PostRepository $posts
* @param TranslatorInterface $translator * @param TranslatorInterface $translator
* @param SettingsRepositoryInterface $settings
* @param Dispatcher $events
*/ */
public function __construct(PostRepository $posts, TranslatorInterface $translator, SettingsRepositoryInterface $settings) public function __construct(PostRepository $posts, TranslatorInterface $translator, SettingsRepositoryInterface $settings, Dispatcher $events)
{ {
$this->posts = $posts; $this->posts = $posts;
$this->translator = $translator; $this->translator = $translator;
$this->settings = $settings; $this->settings = $settings;
$this->events = $events;
} }
/** /**
@ -93,6 +103,8 @@ class CreateFlagHandler
$flag->save(); $flag->save();
$this->events->dispatch(new Created($flag, $actor, $data));
return $flag; return $flag;
} }
} }

View File

@ -9,10 +9,11 @@
namespace Flarum\Flags\Command; namespace Flarum\Flags\Command;
use Flarum\Flags\Event\Deleting;
use Flarum\Flags\Event\FlagsWillBeDeleted; use Flarum\Flags\Event\FlagsWillBeDeleted;
use Flarum\Flags\Flag; use Flarum\Flags\Flag;
use Flarum\Post\PostRepository; use Flarum\Post\PostRepository;
use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Events\Dispatcher;
class DeleteFlagsHandler class DeleteFlagsHandler
{ {
@ -48,8 +49,13 @@ class DeleteFlagsHandler
$actor->assertCan('viewFlags', $post->discussion); $actor->assertCan('viewFlags', $post->discussion);
// remove beta 17
$this->events->dispatch(new FlagsWillBeDeleted($post, $actor, $command->data)); $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(); $post->flags()->delete();
return $post; return $post;

View File

@ -0,0 +1,43 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Flags\Event;
use Flarum\Flags\Flag;
use Flarum\User\User;
class Created
{
/**
* @var Flag
*/
public $flag;
/**
* @var User
*/
public $actor;
/**
* @var array
*/
public $data;
/**
* @param Flag $flag
* @param User $actor
* @param array $data
*/
public function __construct(Flag $flag, User $actor, array $data = [])
{
$this->flag = $flag;
$this->actor = $actor;
$this->data = $data;
}
}

View File

@ -0,0 +1,43 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Flags\Event;
use Flarum\Flags\Flag;
use Flarum\User\User;
class Deleting
{
/**
* @var Flag
*/
public $flag;
/**
* @var User
*/
public $actor;
/**
* @var array
*/
public $data;
/**
* @param Flag $flag
* @param User $actor
* @param array $data
*/
public function __construct(Flag $flag, User $actor, array $data = [])
{
$this->flag = $flag;
$this->actor = $actor;
$this->data = $data;
}
}

View File

@ -12,6 +12,10 @@ namespace Flarum\Flags\Event;
use Flarum\Post\Post; use Flarum\Post\Post;
use Flarum\User\User; 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 class FlagsWillBeDeleted
{ {
/** /**