Skip to content

Commit

Permalink
COM-749 Adds created and deleting events
Browse files Browse the repository at this point in the history
  • Loading branch information
tariqwalji committed May 21, 2021
1 parent 4a6d765 commit d3d2ac3
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
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;
}
}
5 changes: 5 additions & 0 deletions src/Command/DeleteFlagsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Flarum\Flags\Command;

use Flarum\Flags\Event\Deleting;
use Flarum\Flags\Event\FlagsWillBeDeleted;
use Flarum\Flags\Flag;
use Flarum\Post\PostRepository;
Expand Down Expand Up @@ -50,6 +51,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;
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;
}
}

0 comments on commit d3d2ac3

Please sign in to comment.