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

Create::validateUserData()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

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