|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Service\User; |
|
6
|
|
|
|
|
7
|
|
|
use App\Exception\UserException; |
|
8
|
|
|
use App\Repository\UserRepository; |
|
9
|
|
|
use App\Service\BaseService; |
|
10
|
|
|
use App\Service\LoggerService; |
|
11
|
|
|
use App\Service\RedisService; |
|
12
|
|
|
use Respect\Validation\Validator as v; |
|
13
|
|
|
|
|
14
|
|
|
abstract class Base extends BaseService |
|
15
|
|
|
{ |
|
16
|
|
|
private const REDIS_KEY = 'user:%s'; |
|
17
|
|
|
|
|
18
|
|
|
/** @var UserRepository */ |
|
19
|
|
|
protected $userRepository; |
|
20
|
|
|
|
|
21
|
|
|
/** @var RedisService */ |
|
22
|
|
|
protected $redisService; |
|
23
|
|
|
|
|
24
|
|
|
/** @var LoggerService */ |
|
25
|
|
|
protected $loggerService; |
|
26
|
|
|
|
|
27
|
23 |
|
public function __construct( |
|
28
|
|
|
UserRepository $userRepository, |
|
29
|
|
|
RedisService $redisService, |
|
30
|
|
|
LoggerService $loggerService |
|
31
|
|
|
) { |
|
32
|
23 |
|
$this->userRepository = $userRepository; |
|
33
|
23 |
|
$this->redisService = $redisService; |
|
34
|
23 |
|
$this->loggerService = $loggerService; |
|
35
|
23 |
|
} |
|
36
|
|
|
|
|
37
|
5 |
|
protected static function validateUserName(string $name): string |
|
38
|
|
|
{ |
|
39
|
5 |
|
if (!v::alnum('ÁÉÍÓÚÑáéíóúñ.')->length(1, 100)->validate($name)) { |
|
40
|
1 |
|
throw new UserException('Invalid name.', 400); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
4 |
|
return $name; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
4 |
|
protected static function validateEmail(string $emailValue): string |
|
47
|
|
|
{ |
|
48
|
4 |
|
$email = filter_var($emailValue, FILTER_SANITIZE_EMAIL); |
|
49
|
4 |
|
if (!v::email()->validate($email)) { |
|
50
|
1 |
|
throw new UserException('Invalid email', 400); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
3 |
|
return (string) $email; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
3 |
|
protected function getUserFromCache(int $userId): object |
|
57
|
|
|
{ |
|
58
|
3 |
|
$redisKey = sprintf(self::REDIS_KEY, $userId); |
|
59
|
3 |
|
$key = $this->redisService->generateKey($redisKey); |
|
60
|
3 |
|
if ($this->redisService->exists($key)) { |
|
61
|
2 |
|
$data = $this->redisService->get($key); |
|
62
|
2 |
|
$user = json_decode((string) json_encode($data), false); |
|
63
|
|
|
} else { |
|
64
|
1 |
|
$user = $this->getUserFromDb($userId)->getData(); |
|
65
|
|
|
$this->redisService->setex($key, $user); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
2 |
|
return $user; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
5 |
|
protected function getUserFromDb(int $userId): \App\Entity\User |
|
72
|
|
|
{ |
|
73
|
5 |
|
return $this->userRepository->checkAndGetUser($userId); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
2 |
|
protected function saveInCache(int $id, object $user): void |
|
77
|
|
|
{ |
|
78
|
2 |
|
$redisKey = sprintf(self::REDIS_KEY, $id); |
|
79
|
2 |
|
$key = $this->redisService->generateKey($redisKey); |
|
80
|
2 |
|
$this->redisService->setex($key, $user); |
|
81
|
2 |
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
protected function deleteFromCache(int $userId): void |
|
84
|
|
|
{ |
|
85
|
1 |
|
$redisKey = sprintf(self::REDIS_KEY, $userId); |
|
86
|
1 |
|
$key = $this->redisService->generateKey($redisKey); |
|
87
|
1 |
|
$this->redisService->del([$key]); |
|
88
|
1 |
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|