1
0
mirror of https://github.com/flarum/core.git synced 2025-10-20 03:06:07 +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:
Franz Liedke
2017-06-24 15:21:07 +02:00
parent 95dc7e71f4
commit 5b0d0d9f0f
57 changed files with 115 additions and 119 deletions

View File

@@ -0,0 +1,41 @@
<?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\Group\Command;
use Flarum\User\User;
class CreateGroup
{
/**
* The user performing the action.
*
* @var User
*/
public $actor;
/**
* The attributes of the new group.
*
* @var array
*/
public $data;
/**
* @param User $actor The user performing the action.
* @param array $data The attributes of the new group.
*/
public function __construct(User $actor, array $data)
{
$this->actor = $actor;
$this->data = $data;
}
}

View File

@@ -0,0 +1,72 @@
<?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\Group\Command;
use Flarum\Foundation\DispatchEventsTrait;
use Flarum\Group\Event\Saving;
use Flarum\Group\Group;
use Flarum\Group\GroupValidator;
use Flarum\User\AssertPermissionTrait;
use Illuminate\Contracts\Events\Dispatcher;
class CreateGroupHandler
{
use DispatchEventsTrait;
use AssertPermissionTrait;
/**
* @var \Flarum\Group\GroupValidator
*/
protected $validator;
/**
* @param Dispatcher $events
* @param \Flarum\Group\GroupValidator $validator
*/
public function __construct(Dispatcher $events, GroupValidator $validator)
{
$this->events = $events;
$this->validator = $validator;
}
/**
* @param CreateGroup $command
* @return \Flarum\Group\Group
* @throws \Flarum\User\Exception\PermissionDeniedException
*/
public function handle(CreateGroup $command)
{
$actor = $command->actor;
$data = $command->data;
$this->assertCan($actor, 'createGroup');
$group = Group::build(
array_get($data, 'attributes.nameSingular'),
array_get($data, 'attributes.namePlural'),
array_get($data, 'attributes.color'),
array_get($data, 'attributes.icon')
);
$this->events->fire(
new Saving($group, $actor, $data)
);
$this->validator->assertValid($group->getAttributes());
$group->save();
$this->dispatchEventsFor($group, $actor);
return $group;
}
}

View 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\Group\Command;
use Flarum\User\User;
class DeleteGroup
{
/**
* The ID of the group to delete.
*
* @var int
*/
public $groupId;
/**
* The user performing the action.
*
* @var User
*/
public $actor;
/**
* Any other group input associated with the action. This is unused by
* default, but may be used by extensions.
*
* @var array
*/
public $data;
/**
* @param int $groupId The ID of the group to delete.
* @param User $actor The user performing the action.
* @param array $data Any other group input associated with the action. This
* is unused by default, but may be used by extensions.
*/
public function __construct($groupId, User $actor, array $data = [])
{
$this->groupId = $groupId;
$this->actor = $actor;
$this->data = $data;
}
}

View File

@@ -0,0 +1,63 @@
<?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\Group\Command;
use Flarum\Foundation\DispatchEventsTrait;
use Flarum\Group\Event\Deleting;
use Flarum\Group\GroupRepository;
use Flarum\User\AssertPermissionTrait;
use Flarum\User\Exception\PermissionDeniedException;
use Illuminate\Contracts\Events\Dispatcher;
class DeleteGroupHandler
{
use DispatchEventsTrait;
use AssertPermissionTrait;
/**
* @var GroupRepository
*/
protected $groups;
/**
* @param GroupRepository $groups
*/
public function __construct(Dispatcher $events, GroupRepository $groups)
{
$this->groups = $groups;
$this->events = $events;
}
/**
* @param DeleteGroup $command
* @return \Flarum\Group\Group
* @throws PermissionDeniedException
*/
public function handle(DeleteGroup $command)
{
$actor = $command->actor;
$group = $this->groups->findOrFail($command->groupId, $actor);
$this->assertCan($actor, 'delete', $group);
$this->events->fire(
new Deleting($group, $actor, $command->data)
);
$group->delete();
$this->dispatchEventsFor($group, $actor);
return $group;
}
}

View 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\Group\Command;
use Flarum\User\User;
class EditGroup
{
/**
* The ID of the group to edit.
*
* @var int
*/
public $groupId;
/**
* The user performing the action.
*
* @var User
*/
public $actor;
/**
* The attributes to update on the post.
*
* @var array
*/
public $data;
/**
* @param int $groupId The ID of the group to edit.
* @param User $actor The user performing the action.
* @param array $data The attributes to update on the post.
*/
public function __construct($groupId, User $actor, array $data)
{
$this->groupId = $groupId;
$this->actor = $actor;
$this->data = $data;
}
}

View File

@@ -0,0 +1,90 @@
<?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\Group\Command;
use Flarum\Foundation\DispatchEventsTrait;
use Flarum\Group\Event\Saving;
use Flarum\Group\Group;
use Flarum\Group\GroupRepository;
use Flarum\Group\GroupValidator;
use Flarum\User\AssertPermissionTrait;
use Flarum\User\Exception\PermissionDeniedException;
use Illuminate\Contracts\Events\Dispatcher;
class EditGroupHandler
{
use DispatchEventsTrait;
use AssertPermissionTrait;
/**
* @var \Flarum\Group\GroupRepository
*/
protected $groups;
/**
* @var GroupValidator
*/
protected $validator;
/**
* @param Dispatcher $events
* @param GroupRepository $groups
* @param GroupValidator $validator
*/
public function __construct(Dispatcher $events, GroupRepository $groups, GroupValidator $validator)
{
$this->events = $events;
$this->groups = $groups;
$this->validator = $validator;
}
/**
* @param EditGroup $command
* @return Group
* @throws PermissionDeniedException
*/
public function handle(EditGroup $command)
{
$actor = $command->actor;
$data = $command->data;
$group = $this->groups->findOrFail($command->groupId, $actor);
$this->assertCan($actor, 'edit', $group);
$attributes = array_get($data, 'attributes', []);
if (isset($attributes['nameSingular']) && isset($attributes['namePlural'])) {
$group->rename($attributes['nameSingular'], $attributes['namePlural']);
}
if (isset($attributes['color'])) {
$group->color = $attributes['color'];
}
if (isset($attributes['icon'])) {
$group->icon = $attributes['icon'];
}
$this->events->fire(
new Saving($group, $actor, $data)
);
$this->validator->assertValid($group->getDirty());
$group->save();
$this->dispatchEventsFor($group, $actor);
return $group;
}
}