start() public static method

Start a new discussion. Raises the DiscussionWasStarted event.
public static start ( string $title, User $user ) : static
$title string
$user User
return static
Example #1
0
 /**
  * @param StartDiscussion $command
  * @return mixed
  * @throws Exception
  */
 public function handle(StartDiscussion $command)
 {
     $actor = $command->actor;
     $data = $command->data;
     $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 DiscussionWillBeSaved($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));
     } 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;
 }
 /**
  * @param SplitDiscussion $command
  * @return \Flarum\Core\Discussion
  */
 public function handle(SplitDiscussion $command)
 {
     $this->assertCan($command->actor, 'split');
     $this->validator->assertValid(['start_post_id' => $command->start_post_id, 'end_post_id' => $command->end_post_id, 'title' => $command->title]);
     // load the first selected post to split.
     $startPost = $this->posts->findOrFail($command->start_post_id, $command->actor);
     // load the discussion this split action is taking place on.
     $originalDiscussion = $startPost->discussion;
     // create a new discussion for the user of the first splitted reply.
     $discussion = Discussion::start($command->title, $startPost->user);
     $discussion->setStartPost($startPost);
     if (!empty($originalDiscussion->tags)) {
         $discussion->tags()->sync($originalDiscussion->tags);
     }
     // persist the new discussion.
     $discussion->save();
     // update all posts that are split.
     $affectedPosts = $this->assignPostsToDiscussion($originalDiscussion, $discussion, $startPost->id, $command->end_post_id);
     $originalDiscussion = $this->refreshDiscussion($originalDiscussion);
     $discussion = $this->refreshDiscussion($discussion);
     $this->events->fire(new DiscussionWasSplit($command->actor, $affectedPosts, $originalDiscussion, $discussion));
     return $discussion;
 }