-
-
Notifications
You must be signed in to change notification settings - Fork 828
/
CreateGroupHandler.php
57 lines (44 loc) · 1.35 KB
/
CreateGroupHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
/*
* This file is part of Flarum.
*
* For detailed 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 Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Arr;
class CreateGroupHandler
{
use DispatchEventsTrait;
public function __construct(
protected Dispatcher $events,
protected GroupValidator $validator
) {
}
public function handle(CreateGroup $command): Group
{
$actor = $command->actor;
$data = $command->data;
$actor->assertRegistered();
$actor->assertCan('createGroup');
$group = Group::build(
Arr::get($data, 'attributes.nameSingular'),
Arr::get($data, 'attributes.namePlural'),
Arr::get($data, 'attributes.color'),
Arr::get($data, 'attributes.icon'),
Arr::get($data, 'attributes.isHidden', false)
);
$this->events->dispatch(
new Saving($group, $actor, $data)
);
$this->validator->assertValid($group->getAttributes());
$group->save();
$this->dispatchEventsFor($group, $actor);
return $group;
}
}