1
0
mirror of https://github.com/flarum/core.git synced 2025-07-29 04:30:56 +02:00
Files
php-flarum/src/Core/Command/EditGroupHandler.php
Toby Zerner c08b62af80 Refactor translation and validation
We now use Symfony's Translation component. Yay! We get more powerful pluralisation and better a fallback mechanism. Will want to implement the caching mechanism at some point too. The API is replicated in JavaScript, which could definitely use some testing.

Validators have been refactored so that they are decoupled from models completely (i.e. they simply validate arrays of user input). Language packs should include Laravel's validation messages.

ref #267
2015-10-15 22:30:45 +10:30

90 lines
2.2 KiB
PHP

<?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\Core\Command;
use Flarum\Core\Access\AssertPermissionTrait;
use Flarum\Core\Exception\PermissionDeniedException;
use Flarum\Core\Group;
use Flarum\Core\Repository\GroupRepository;
use Flarum\Core\Validator\GroupValidator;
use Flarum\Event\GroupWillBeSaved;
use Flarum\Core\Support\DispatchEventsTrait;
use Illuminate\Contracts\Events\Dispatcher;
class EditGroupHandler
{
use DispatchEventsTrait;
use AssertPermissionTrait;
/**
* @var 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 GroupWillBeSaved($group, $actor, $data)
);
$this->validator->assertValid($group->getDirty());
$group->save();
$this->dispatchEventsFor($group, $actor);
return $group;
}
}