Skip to content

Commit

Permalink
Add Created and Deleting events (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland authored Jan 25, 2021
1 parent 4a6d765 commit da2efc1
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/Command/CreateFlagHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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\Events\Dispatcher;
use Illuminate\Support\Arr;
use Symfony\Component\Translation\TranslatorInterface;
use Tobscure\JsonApi\Exception\InvalidParameterException;
Expand All @@ -36,15 +38,23 @@ class CreateFlagHandler
*/
protected $settings;

/**
* @var Dispatcher
*/
protected $events;

/**
* @param PostRepository $posts
* @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->translator = $translator;
$this->settings = $settings;
$this->events = $events;
}

/**
Expand Down Expand Up @@ -93,6 +103,8 @@ public function handle(CreateFlag $command)

$flag->save();

$this->events->dispatch(new Created($flag, $actor, $data));

return $flag;
}
}
8 changes: 7 additions & 1 deletion src/Command/DeleteFlagsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@

namespace Flarum\Flags\Command;

use Flarum\Flags\Event\Deleting;
use Flarum\Flags\Event\FlagsWillBeDeleted;
use Flarum\Flags\Flag;
use Flarum\Post\PostRepository;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Events\Dispatcher;

class DeleteFlagsHandler
{
Expand Down Expand Up @@ -48,8 +49,13 @@ 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));
}

$post->flags()->delete();

return $post;
Expand Down
43 changes: 43 additions & 0 deletions src/Event/Created.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
43 changes: 43 additions & 0 deletions src/Event/Deleting.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
4 changes: 4 additions & 0 deletions src/Event/FlagsWillBeDeleted.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand Down

0 comments on commit da2efc1

Please sign in to comment.