Test Setup Failed
Push — master ( 948172...201d68 )
by Fernando
05:02
created

Update::validateUserData()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 11
c 0
b 0
f 0
nc 5
nop 2
dl 0
loc 17
ccs 3
cts 3
cp 1
crap 6
rs 9.2222
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Service\User;
6
7
use App\Entity\User;
8
9
final class Update extends Base
10
{
11 3
    /**
12
     * @param array<string> $input
13 3
     */
14 3
    public function update(array $input, int $userId): object
15 3
    {
16 1
        $data = $this->validateUserData($input, $userId);
17
        /** @var User $user */
18 2
        $user = $this->userRepository->update($data);
19 2
20
        if (self::isRedisEnabled() === true) {
21 1
            $this->saveInCache($user->getId(), $user->toJson());
22 1
        }
23
        if (self::isLoggerEnabled() === true) {
24 1
            $this->loggerService->setInfo('The user with the ID ' . $user->getId() . ' has updated successfully.');
25 1
        }
26
27 1
        return $user->toJson();
28
    }
29 1
30 1
    /**
31 1
     * @param array<string> $input
32
     */
33 1
    private function validateUserData(array $input, int $userId): User
34 1
    {
35
        $user = $this->getUserFromDb($userId);
36
        $data = json_decode((string) json_encode($input), false);
37 1
        if (!isset($data->name) && !isset($data->email)) {
38
            throw new \App\Exception\UserException('Enter the data to update the user.', 400);
39
        }
40
        if (isset($data->name)) {
41
            $user->updateName(self::validateUserName($data->name));
42
        }
43
        if (isset($data->email) && $data->email !== $user->getEmail()) {
44
            $this->userRepository->checkUserByEmail($data->email);
45
            $user->updateEmail(self::validateEmail($data->email));
46
        }
47
        $user->updateUpdatedAt(date('Y-m-d H:i:s'));
48
49
        return $user;
50
    }
51
}
52