DeleteUser.php 1.18 KB
Newer Older
Jacob Priddy's avatar
Jacob Priddy committed
1
2
3
4
<?php

namespace Source\UseCases\Users\DeleteUser;

Jacob Priddy's avatar
Jacob Priddy committed
5
use Source\Authorization\Authorizer;
Jacob Priddy's avatar
Jacob Priddy committed
6
use Source\Gateways\Users\UsersRepository;
Jacob Priddy's avatar
Jacob Priddy committed
7
use Source\Exceptions\DeleteFailedException;
Jacob Priddy's avatar
Jacob Priddy committed
8
use Source\Exceptions\EntityNotFoundException;
Jacob Priddy's avatar
Jacob Priddy committed
9

Jacob Priddy's avatar
Jacob Priddy committed
10
11
class DeleteUser implements DeleteUserUseCase
{
Jacob Priddy's avatar
Jacob Priddy committed
12
13
14
15
16
    /**
     * @var UsersRepository
     */
    protected UsersRepository $usersRepository;

Jacob Priddy's avatar
Jacob Priddy committed
17
18
19
20
21
22
    /**
     * @var \Source\Authorization\Authorizer
     */
    protected Authorizer $authorizer;

    public function __construct(Authorizer $authorizer, UsersRepository $usersRepository)
Jacob Priddy's avatar
Jacob Priddy committed
23
    {
Jacob Priddy's avatar
Jacob Priddy committed
24
        $this->authorizer = $authorizer;
Jacob Priddy's avatar
Jacob Priddy committed
25
26
27
28
29
30
        $this->usersRepository = $usersRepository;
    }

    /**
     * @inheritDoc
     */
Jacob Priddy's avatar
Jacob Priddy committed
31
32
    public function delete(string $userId, Presenter $presenter): void
    {
Jacob Priddy's avatar
Jacob Priddy committed
33
34
        $this->authorizer->protectAdminRights($userId);

Jacob Priddy's avatar
Jacob Priddy committed
35
36
37
38
39
40
        $user = $this->usersRepository->get($userId);

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

Jacob Priddy's avatar
Jacob Priddy committed
41
42
43
44
45
46
47
48
49
        if (!$this->usersRepository->delete($userId)) {
            throw new DeleteFailedException('Unable to delete user.');
        }

        $response = new ResponseModel('User deleted.');

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