UpdateUser.php 2.4 KB
Newer Older
1
2
3
4
<?php

namespace Source\UseCases\Users\UpdateUser;

Jacob Priddy's avatar
Jacob Priddy committed
5
6
7
use Exception;
use Carbon\Carbon;
use Source\Entities\User;
Jacob Priddy's avatar
Jacob Priddy committed
8
9
use Source\Entities\Doorcode;
use Source\Entities\Password;
Jacob Priddy's avatar
Jacob Priddy committed
10
use Source\Authorization\Authorizer;
Jacob Priddy's avatar
Jacob Priddy committed
11
12
13
use Source\Gateways\Users\UsersRepository;
use Source\Exceptions\EntityNotFoundException;

Jacob Priddy's avatar
Jacob Priddy committed
14
15
class UpdateUser implements UpdateUserUseCase
{
Jacob Priddy's avatar
Jacob Priddy committed
16
17
18
19
20
    /**
     * @var UsersRepository
     */
    protected UsersRepository $usersRepository;

Jacob Priddy's avatar
Jacob Priddy committed
21
22
23
24
25
    /**
     * @var \Source\Authorization\Authorizer
     */
    protected Authorizer $authorizer;

Jacob Priddy's avatar
Jacob Priddy committed
26
    /**
Jacob Priddy's avatar
Jacob Priddy committed
27
28
     * @param \Source\Authorization\Authorizer       $authorizer
     * @param \Source\Gateways\Users\UsersRepository $usersRepository
Jacob Priddy's avatar
Jacob Priddy committed
29
     */
Jacob Priddy's avatar
Jacob Priddy committed
30
    public function __construct(Authorizer $authorizer, UsersRepository $usersRepository)
Jacob Priddy's avatar
Jacob Priddy committed
31
    {
Jacob Priddy's avatar
Jacob Priddy committed
32
        $this->authorizer = $authorizer;
Jacob Priddy's avatar
Jacob Priddy committed
33
34
35
36
37
38
39
        $this->usersRepository = $usersRepository;
    }

    /**
     * @inheritDoc
     * @throws Exception
     */
Jacob Priddy's avatar
Jacob Priddy committed
40
41
    public function update(string $userId, array $attributes, Presenter $presenter): void
    {
Jacob Priddy's avatar
Jacob Priddy committed
42
43
        $this->authorizer->protectAdminRights($userId);

Jacob Priddy's avatar
Jacob Priddy committed
44
        $user = $this->usersRepository->get($userId);
Jacob Priddy's avatar
Jacob Priddy committed
45

Jacob Priddy's avatar
Jacob Priddy committed
46
47
48
49
        if (!$user) {
            throw new EntityNotFoundException();
        }

Jacob Priddy's avatar
Jacob Priddy committed
50
51
52
        $expires = (array_key_exists('expires_at', $attributes) && $attributes['expires_at'])
            ? new Carbon($attributes['expires_at'])
            : $user->getExpiresAt();
Jacob Priddy's avatar
Jacob Priddy committed
53
54
55
56

        // updatedAt will get overwritten
        $newUser = new User(
            $user->getId(),
57
58
59
60
61
            $attributes['first_name'] ?? $user->getFirstName(),
            $attributes['last_name'] ?? $user->getLastName(),
            $attributes['display_name'] ?? $user->getDisplayName(),
            $attributes['email'] ?? $user->getEmail(),
            $attributes['emplid'] ?? $user->getEmplid(),
Jacob Priddy's avatar
Jacob Priddy committed
62
63
            Password::hash($attributes['password'] ?? null) ?? $user->getPassword(),
            Doorcode::hash($attributes['salt'], $attributes['doorcode'] ?? null) ?? $user->getDoorcode(),
Jacob Priddy's avatar
Jacob Priddy committed
64
65
            $expires,
            $user->getCreatedAt(),
Jacob Priddy's avatar
Jacob Priddy committed
66
            $user->getUpdatedAt()
Jacob Priddy's avatar
Jacob Priddy committed
67
68
69
70
71
72
73
        );

        $response = new ResponseModel();
        $returnedUser = $this->usersRepository->update($user->getId(), $newUser);

        if (!$returnedUser) {
            $response->setError('Unable to update user.');
Jacob Priddy's avatar
Jacob Priddy committed
74
        } else {
Jacob Priddy's avatar
Jacob Priddy committed
75
76
77
78
            $response->setUser($returnedUser);
        }

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