mirror of
https://github.com/flarum/framework.git
synced 2025-04-25 22:24:04 +08:00

Starting with version 5.9, the global funtions will be deprecated. * https://laravel-news.com/laravel-5-8-deprecates-string-and-array-helpers * https://github.com/laravel/framework/pull/26898
74 lines
1.7 KiB
PHP
74 lines
1.7 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\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;
|
|
use Illuminate\Support\Arr;
|
|
|
|
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(
|
|
Arr::get($data, 'attributes.nameSingular'),
|
|
Arr::get($data, 'attributes.namePlural'),
|
|
Arr::get($data, 'attributes.color'),
|
|
Arr::get($data, 'attributes.icon')
|
|
);
|
|
|
|
$this->events->dispatch(
|
|
new Saving($group, $actor, $data)
|
|
);
|
|
|
|
$this->validator->assertValid($group->getAttributes());
|
|
|
|
$group->save();
|
|
|
|
$this->dispatchEventsFor($group, $actor);
|
|
|
|
return $group;
|
|
}
|
|
}
|