mirror of
https://github.com/flarum/core.git
synced 2025-08-06 08:27:42 +02:00
Move command classes to domain namespaces
They will probably be refactored away at a later stage (when we get rid of the command bus). Until then, this lets us remove the Flarum\Core namespace and actually feels quite clean.
This commit is contained in:
52
src/Discussion/Command/DeleteDiscussion.php
Normal file
52
src/Discussion/Command/DeleteDiscussion.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Discussion\Command;
|
||||
|
||||
use Flarum\User\User;
|
||||
|
||||
class DeleteDiscussion
|
||||
{
|
||||
/**
|
||||
* The ID of the discussion to delete.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $discussionId;
|
||||
|
||||
/**
|
||||
* The user performing the action.
|
||||
*
|
||||
* @var User
|
||||
*/
|
||||
public $actor;
|
||||
|
||||
/**
|
||||
* Any other user input associated with the action. This is unused by
|
||||
* default, but may be used by extensions.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $data;
|
||||
|
||||
/**
|
||||
* @param int $discussionId The ID of the discussion to delete.
|
||||
* @param User $actor The user performing the action.
|
||||
* @param array $data Any other user input associated with the action. This
|
||||
* is unused by default, but may be used by extensions.
|
||||
*/
|
||||
public function __construct($discussionId, User $actor, array $data = [])
|
||||
{
|
||||
$this->discussionId = $discussionId;
|
||||
$this->actor = $actor;
|
||||
$this->data = $data;
|
||||
}
|
||||
}
|
64
src/Discussion/Command/DeleteDiscussionHandler.php
Normal file
64
src/Discussion/Command/DeleteDiscussionHandler.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Discussion\Command;
|
||||
|
||||
use Flarum\Discussion\DiscussionRepository;
|
||||
use Flarum\Discussion\Event\Deleting;
|
||||
use Flarum\Foundation\DispatchEventsTrait;
|
||||
use Flarum\User\AssertPermissionTrait;
|
||||
use Flarum\User\Exception\PermissionDeniedException;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class DeleteDiscussionHandler
|
||||
{
|
||||
use DispatchEventsTrait;
|
||||
use AssertPermissionTrait;
|
||||
|
||||
/**
|
||||
* @var \Flarum\Discussion\DiscussionRepository
|
||||
*/
|
||||
protected $discussions;
|
||||
|
||||
/**
|
||||
* @param Dispatcher $events
|
||||
* @param DiscussionRepository $discussions
|
||||
*/
|
||||
public function __construct(Dispatcher $events, DiscussionRepository $discussions)
|
||||
{
|
||||
$this->events = $events;
|
||||
$this->discussions = $discussions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param DeleteDiscussion $command
|
||||
* @return \Flarum\Discussion\Discussion
|
||||
* @throws PermissionDeniedException
|
||||
*/
|
||||
public function handle(DeleteDiscussion $command)
|
||||
{
|
||||
$actor = $command->actor;
|
||||
|
||||
$discussion = $this->discussions->findOrFail($command->discussionId, $actor);
|
||||
|
||||
$this->assertCan($actor, 'delete', $discussion);
|
||||
|
||||
$this->events->fire(
|
||||
new Deleting($discussion, $actor, $command->data)
|
||||
);
|
||||
|
||||
$discussion->delete();
|
||||
|
||||
$this->dispatchEventsFor($discussion, $actor);
|
||||
|
||||
return $discussion;
|
||||
}
|
||||
}
|
50
src/Discussion/Command/EditDiscussion.php
Normal file
50
src/Discussion/Command/EditDiscussion.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Discussion\Command;
|
||||
|
||||
use Flarum\User\User;
|
||||
|
||||
class EditDiscussion
|
||||
{
|
||||
/**
|
||||
* The ID of the discussion to edit.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $discussionId;
|
||||
|
||||
/**
|
||||
* The user performing the action.
|
||||
*
|
||||
* @var \Flarum\User\User
|
||||
*/
|
||||
public $actor;
|
||||
|
||||
/**
|
||||
* The attributes to update on the discussion.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $data;
|
||||
|
||||
/**
|
||||
* @param int $discussionId The ID of the discussion to edit.
|
||||
* @param \Flarum\User\User $actor The user performing the action.
|
||||
* @param array $data The attributes to update on the discussion.
|
||||
*/
|
||||
public function __construct($discussionId, User $actor, array $data)
|
||||
{
|
||||
$this->discussionId = $discussionId;
|
||||
$this->actor = $actor;
|
||||
$this->data = $data;
|
||||
}
|
||||
}
|
89
src/Discussion/Command/EditDiscussionHandler.php
Normal file
89
src/Discussion/Command/EditDiscussionHandler.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Discussion\Command;
|
||||
|
||||
use Flarum\Discussion\DiscussionRepository;
|
||||
use Flarum\Discussion\DiscussionValidator;
|
||||
use Flarum\Discussion\Event\Saving;
|
||||
use Flarum\Foundation\DispatchEventsTrait;
|
||||
use Flarum\User\AssertPermissionTrait;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class EditDiscussionHandler
|
||||
{
|
||||
use DispatchEventsTrait;
|
||||
use AssertPermissionTrait;
|
||||
|
||||
/**
|
||||
* @var DiscussionRepository
|
||||
*/
|
||||
protected $discussions;
|
||||
|
||||
/**
|
||||
* @var DiscussionValidator
|
||||
*/
|
||||
protected $validator;
|
||||
|
||||
/**
|
||||
* @param Dispatcher $events
|
||||
* @param DiscussionRepository $discussions
|
||||
* @param DiscussionValidator $validator
|
||||
*/
|
||||
public function __construct(Dispatcher $events, DiscussionRepository $discussions, DiscussionValidator $validator)
|
||||
{
|
||||
$this->events = $events;
|
||||
$this->discussions = $discussions;
|
||||
$this->validator = $validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param EditDiscussion $command
|
||||
* @return \Flarum\Discussion\Discussion
|
||||
* @throws \Flarum\User\Exception\PermissionDeniedException
|
||||
*/
|
||||
public function handle(EditDiscussion $command)
|
||||
{
|
||||
$actor = $command->actor;
|
||||
$data = $command->data;
|
||||
$attributes = array_get($data, 'attributes', []);
|
||||
|
||||
$discussion = $this->discussions->findOrFail($command->discussionId, $actor);
|
||||
|
||||
if (isset($attributes['title'])) {
|
||||
$this->assertCan($actor, 'rename', $discussion);
|
||||
|
||||
$discussion->rename($attributes['title']);
|
||||
}
|
||||
|
||||
if (isset($attributes['isHidden'])) {
|
||||
$this->assertCan($actor, 'hide', $discussion);
|
||||
|
||||
if ($attributes['isHidden']) {
|
||||
$discussion->hide($actor);
|
||||
} else {
|
||||
$discussion->restore();
|
||||
}
|
||||
}
|
||||
|
||||
$this->events->fire(
|
||||
new Saving($discussion, $actor, $data)
|
||||
);
|
||||
|
||||
$this->validator->assertValid($discussion->getDirty());
|
||||
|
||||
$discussion->save();
|
||||
|
||||
$this->dispatchEventsFor($discussion, $actor);
|
||||
|
||||
return $discussion;
|
||||
}
|
||||
}
|
50
src/Discussion/Command/ReadDiscussion.php
Normal file
50
src/Discussion/Command/ReadDiscussion.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Discussion\Command;
|
||||
|
||||
use Flarum\User\User;
|
||||
|
||||
class ReadDiscussion
|
||||
{
|
||||
/**
|
||||
* The ID of the discussion to mark as read.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $discussionId;
|
||||
|
||||
/**
|
||||
* The user to mark the discussion as read for.
|
||||
*
|
||||
* @var User
|
||||
*/
|
||||
public $actor;
|
||||
|
||||
/**
|
||||
* The number of the post to mark as read.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $readNumber;
|
||||
|
||||
/**
|
||||
* @param int $discussionId The ID of the discussion to mark as read.
|
||||
* @param User $actor The user to mark the discussion as read for.
|
||||
* @param int $readNumber The number of the post to mark as read.
|
||||
*/
|
||||
public function __construct($discussionId, User $actor, $readNumber)
|
||||
{
|
||||
$this->discussionId = $discussionId;
|
||||
$this->actor = $actor;
|
||||
$this->readNumber = $readNumber;
|
||||
}
|
||||
}
|
66
src/Discussion/Command/ReadDiscussionHandler.php
Normal file
66
src/Discussion/Command/ReadDiscussionHandler.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Discussion\Command;
|
||||
|
||||
use Flarum\Discussion\DiscussionRepository;
|
||||
use Flarum\Discussion\Event\UserDataSaving;
|
||||
use Flarum\Foundation\DispatchEventsTrait;
|
||||
use Flarum\User\AssertPermissionTrait;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class ReadDiscussionHandler
|
||||
{
|
||||
use DispatchEventsTrait;
|
||||
use AssertPermissionTrait;
|
||||
|
||||
/**
|
||||
* @var DiscussionRepository
|
||||
*/
|
||||
protected $discussions;
|
||||
|
||||
/**
|
||||
* @param Dispatcher $events
|
||||
* @param DiscussionRepository $discussions
|
||||
*/
|
||||
public function __construct(Dispatcher $events, DiscussionRepository $discussions)
|
||||
{
|
||||
$this->events = $events;
|
||||
$this->discussions = $discussions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ReadDiscussion $command
|
||||
* @return \Flarum\Discussion\UserState
|
||||
* @throws \Flarum\User\Exception\PermissionDeniedException
|
||||
*/
|
||||
public function handle(ReadDiscussion $command)
|
||||
{
|
||||
$actor = $command->actor;
|
||||
|
||||
$this->assertRegistered($actor);
|
||||
|
||||
$discussion = $this->discussions->findOrFail($command->discussionId, $actor);
|
||||
|
||||
$state = $discussion->stateFor($actor);
|
||||
$state->read($command->readNumber);
|
||||
|
||||
$this->events->fire(
|
||||
new UserDataSaving($state)
|
||||
);
|
||||
|
||||
$state->save();
|
||||
|
||||
$this->dispatchEventsFor($state);
|
||||
|
||||
return $state;
|
||||
}
|
||||
}
|
42
src/Discussion/Command/StartDiscussion.php
Normal file
42
src/Discussion/Command/StartDiscussion.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Discussion\Command;
|
||||
|
||||
use Flarum\User\User;
|
||||
|
||||
class StartDiscussion
|
||||
{
|
||||
/**
|
||||
* The user authoring the discussion.
|
||||
*
|
||||
* @var User
|
||||
*/
|
||||
public $actor;
|
||||
|
||||
/**
|
||||
* The discussion attributes.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $data;
|
||||
|
||||
/**
|
||||
* @param User $actor The user authoring the discussion.
|
||||
* @param array $data The discussion attributes.
|
||||
*/
|
||||
public function __construct(User $actor, array $data, $ipAddress)
|
||||
{
|
||||
$this->actor = $actor;
|
||||
$this->data = $data;
|
||||
$this->ipAddress = $ipAddress;
|
||||
}
|
||||
}
|
105
src/Discussion/Command/StartDiscussionHandler.php
Normal file
105
src/Discussion/Command/StartDiscussionHandler.php
Normal file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Discussion\Command;
|
||||
|
||||
use Exception;
|
||||
use Flarum\Discussion\Discussion;
|
||||
use Flarum\Discussion\DiscussionValidator;
|
||||
use Flarum\Discussion\Event\Saving;
|
||||
use Flarum\Foundation\DispatchEventsTrait;
|
||||
use Flarum\User\AssertPermissionTrait;
|
||||
use Illuminate\Contracts\Bus\Dispatcher as BusDispatcher;
|
||||
use Illuminate\Contracts\Events\Dispatcher as EventDispatcher;
|
||||
|
||||
class StartDiscussionHandler
|
||||
{
|
||||
use DispatchEventsTrait;
|
||||
use AssertPermissionTrait;
|
||||
|
||||
/**
|
||||
* @var BusDispatcher
|
||||
*/
|
||||
protected $bus;
|
||||
|
||||
/**
|
||||
* @var \Flarum\Discussion\DiscussionValidator
|
||||
*/
|
||||
protected $validator;
|
||||
|
||||
/**
|
||||
* @param EventDispatcher $events
|
||||
* @param BusDispatcher $bus
|
||||
* @param \Flarum\Discussion\DiscussionValidator $validator
|
||||
*/
|
||||
public function __construct(EventDispatcher $events, BusDispatcher $bus, DiscussionValidator $validator)
|
||||
{
|
||||
$this->events = $events;
|
||||
$this->bus = $bus;
|
||||
$this->validator = $validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param StartDiscussion $command
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function handle(StartDiscussion $command)
|
||||
{
|
||||
$actor = $command->actor;
|
||||
$data = $command->data;
|
||||
$ipAddress = $command->ipAddress;
|
||||
|
||||
$this->assertCan($actor, 'startDiscussion');
|
||||
|
||||
// Create a new Discussion entity, persist it, and dispatch domain
|
||||
// events. Before persistence, though, fire an event to give plugins
|
||||
// an opportunity to alter the discussion entity based on data in the
|
||||
// command they may have passed through in the controller.
|
||||
$discussion = Discussion::start(
|
||||
array_get($data, 'attributes.title'),
|
||||
$actor
|
||||
);
|
||||
|
||||
$this->events->fire(
|
||||
new Saving($discussion, $actor, $data)
|
||||
);
|
||||
|
||||
$this->validator->assertValid($discussion->getAttributes());
|
||||
|
||||
$discussion->save();
|
||||
|
||||
// Now that the discussion has been created, we can add the first post.
|
||||
// We will do this by running the PostReply command.
|
||||
try {
|
||||
$post = $this->bus->dispatch(
|
||||
new PostReply($discussion->id, $actor, $data, $ipAddress)
|
||||
);
|
||||
} catch (Exception $e) {
|
||||
$discussion->delete();
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
// Before we dispatch events, refresh our discussion instance's
|
||||
// attributes as posting the reply will have changed some of them (e.g.
|
||||
// last_time.)
|
||||
$discussion->setRawAttributes($post->discussion->getAttributes(), true);
|
||||
$discussion->setStartPost($post);
|
||||
$discussion->setLastPost($post);
|
||||
|
||||
$this->dispatchEventsFor($discussion, $actor);
|
||||
|
||||
$discussion->save();
|
||||
|
||||
return $discussion;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user