mirror of
https://github.com/flarum/core.git
synced 2025-07-23 01:31:40 +02:00
Massive refactor
- Use contextual namespaces within Flarum\Core - Clean up and docblock everything - Refactor Activity/Notification blueprint stuff - Refactor Formatter stuff - Refactor Search stuff - Upgrade to JSON-API 1.0 - Removed “addedPosts” and “removedPosts” relationships from discussion API. This was used for adding/removing event posts after renaming a discussion etc. Instead we should make an additional request to get all new posts Todo: - Fix Extenders and extensions - Get rid of repository interfaces - Fix other bugs I’ve inevitably introduced
This commit is contained in:
75
src/Core/Discussions/Commands/StartDiscussionHandler.php
Normal file
75
src/Core/Discussions/Commands/StartDiscussionHandler.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php namespace Flarum\Core\Discussions\Commands;
|
||||
|
||||
use Flarum\Core\Discussions\Events\DiscussionWillBeSaved;
|
||||
use Flarum\Core\Forum;
|
||||
use Illuminate\Contracts\Bus\Dispatcher;
|
||||
use Flarum\Core\Discussions\Discussion;
|
||||
use Flarum\Core\Posts\Commands\PostReply;
|
||||
use Flarum\Core\Support\DispatchesEvents;
|
||||
|
||||
class StartDiscussionHandler
|
||||
{
|
||||
use DispatchesEvents;
|
||||
|
||||
/**
|
||||
* @var Dispatcher
|
||||
*/
|
||||
protected $bus;
|
||||
|
||||
/**
|
||||
* @var Forum
|
||||
*/
|
||||
protected $forum;
|
||||
|
||||
/**
|
||||
* @param Dispatcher $bus
|
||||
*/
|
||||
public function __construct(Dispatcher $bus, Forum $forum)
|
||||
{
|
||||
$this->bus = $bus;
|
||||
$this->forum = $forum;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param StartDiscussion $command
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle(StartDiscussion $command)
|
||||
{
|
||||
$actor = $command->actor;
|
||||
$data = $command->data;
|
||||
|
||||
$this->forum->assertCan($actor, 'startDiscussion');
|
||||
|
||||
// Create a new Discussion entity, persist it, and dispatch domain
|
||||
// events. Before persistance, 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
|
||||
);
|
||||
|
||||
event(new DiscussionWillBeSaved($discussion, $actor, $data));
|
||||
|
||||
$discussion->save();
|
||||
|
||||
// Now that the discussion has been created, we can add the first post.
|
||||
// We will do this by running the PostReply command.
|
||||
$post = $this->bus->dispatch(
|
||||
new PostReply($discussion->id, $actor, $data)
|
||||
);
|
||||
|
||||
// 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);
|
||||
|
||||
$this->dispatchEventsFor($discussion);
|
||||
|
||||
$discussion->save();
|
||||
|
||||
return $discussion;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user