UpdateGroup.php 1.58 KB
Newer Older
Jacob Priddy's avatar
Jacob Priddy committed
1
2
3
4
5
<?php

namespace Source\UseCases\Groups\UpdateGroup;

use Source\Entities\Group;
Jacob Priddy's avatar
Jacob Priddy committed
6
use Source\Authorization\Permissions;
Jacob Priddy's avatar
Jacob Priddy committed
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
use Source\Gateways\Groups\GroupsRepository;
use Source\Exceptions\EntityNotFoundException;

class UpdateGroup implements UpdateGroupUseCase
{
    /**
     * @var \Source\Gateways\Groups\GroupsRepository
     */
    protected GroupsRepository $groups;

    /**
     * @param \Source\Gateways\Groups\GroupsRepository $groups
     */
    public function __construct(GroupsRepository $groups)
    {
        $this->groups = $groups;
    }

    /**
     * @inheritDoc
     */
    public function update(string $groupId, array $attributes, Presenter $presenter): void
    {
        $group = $this->groups->get($groupId);

        if (!$group) {
            throw new EntityNotFoundException();
        }

Jacob Priddy's avatar
Jacob Priddy committed
36
37
38
39
40
41
42
43
44
45
        $reflection = new \ReflectionClass(Permissions::class);


        if (in_array($group->getTitle(), $reflection->getConstants(), true)) {
            // Cannot modify default permission group title
            $groupTitle = $group->getTitle();
        } else {
            $groupTitle = $attributes['title'];
        }

Jacob Priddy's avatar
Jacob Priddy committed
46
47
        $newGroup = new Group(
            $group->getId(),
Jacob Priddy's avatar
Jacob Priddy committed
48
            $groupTitle,
Jacob Priddy's avatar
Jacob Priddy committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
            $attributes['description']
        );

        $response = new ResponseModel();
        $returnedGroup = $this->groups->update($group->getId(), $newGroup);

        if (!$returnedGroup) {
            $response->setError('Unable to update group.');
        } else {
            $response->setGroup($returnedGroup);
        }

        $presenter->present($response);
    }
}