/
EditGroupHandler.php
91 lines (71 loc) · 2.17 KB
/
EditGroupHandler.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?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\GroupRepository;
use Flarum\Group\GroupValidator;
use Flarum\User\Exception\PermissionDeniedException;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Arr;
class EditGroupHandler
{
use DispatchEventsTrait;
/**
* @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);
$actor->assertCan('edit', $group);
$attributes = Arr::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'];
}
if (isset($attributes['isHidden'])) {
$group->is_hidden = $attributes['isHidden'];
}
$this->events->dispatch(
new Saving($group, $actor, $data)
);
$this->validator->assertValid($group->getDirty());
$group->save();
$this->dispatchEventsFor($group, $actor);
return $group;
}
}