Passed
Push — master ( 0b75f0...6b8772 )
by Fernando
17:35
created

Base::getTaskFromDb()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Service\Task;
6
7
use App\Exception\TaskException;
8
use App\Repository\TaskRepository;
9
use App\Service\BaseService;
10
use App\Service\RedisService;
11
use App\Service\LoggerService;
12
use Respect\Validation\Validator as v;
13
14
abstract class Base extends BaseService
15
{
16
    private const REDIS_KEY = 'task:%s:user:%s';
17
18
    /** @var TaskRepository */
19
    protected $taskRepository;
20
21
    /** @var RedisService */
22
    protected $redisService;
23
24
    /** @var LoggerService */
25
    protected $loggerService;
26
27 19
    public function __construct(
28
        TaskRepository $taskRepository,
29
        RedisService $redisService,
30
        LoggerService $loggerService
31
    ) {
32 19
        $this->taskRepository = $taskRepository;
33 19
        $this->redisService = $redisService;
34 19
        $this->loggerService = $loggerService;
35 19
    }
36
37 4
    protected static function validateTaskName(string $name): string
38
    {
39 4
        if (!v::length(1, 100)->validate($name)) {
40 1
            throw new TaskException('Invalid name.', 400);
41
        }
42
43 3
        return $name;
44
    }
45
46 2
    protected static function validateTaskStatus(int $status): int
47
    {
48 2
        if (!v::numeric()->between(0, 1)->validate($status)) {
49 1
            throw new TaskException('Invalid status', 400);
50
        }
51
52 1
        return $status;
53
    }
54
55 3
    protected function getTaskFromCache(int $taskId, int $userId): object
56
    {
57 3
        $redisKey = sprintf(self::REDIS_KEY, $taskId, $userId);
58 3
        $key = $this->redisService->generateKey($redisKey);
59 3
        if ($this->redisService->exists($key)) {
60 2
            $task = $this->redisService->get($key);
61
        } else {
62 1
            $task = $this->getTaskFromDb($taskId, $userId)->getData();
63
            $this->redisService->setex($key, $task);
64
        }
65
66 2
        return $task;
67
    }
68
69 7
    protected function getTaskFromDb(int $taskId, int $userId): \App\Entity\Task
70
    {
71 7
        return $this->taskRepository->checkAndGetTask($taskId, $userId);
72
    }
73
74 2
    protected function saveInCache(int $taskId, int $userId, object $task): void
75
    {
76 2
        $redisKey = sprintf(self::REDIS_KEY, $taskId, $userId);
77 2
        $key = $this->redisService->generateKey($redisKey);
78 2
        $this->redisService->setex($key, $task);
79 2
    }
80
81 1
    protected function deleteFromCache(int $taskId, int $userId): void
82
    {
83 1
        $redisKey = sprintf(self::REDIS_KEY, $taskId, $userId);
84 1
        $key = $this->redisService->generateKey($redisKey);
85 1
        $this->redisService->del([$key]);
86 1
    }
87
}
88