TaskService::update()   B
last analyzed

Complexity

Conditions 7
Paths 17

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.6026
c 0
b 0
f 0
cc 7
nc 17
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Service;
6
7
use App\Exception\TaskException;
8
use App\Repository\TaskRepository;
9
10
class TaskService extends BaseService
11
{
12
    const REDIS_KEY = 'task:%s:user:%s';
13
14
    protected $taskRepository;
15
16
    protected $redisService;
17
18
    public function __construct(TaskRepository $taskRepository, RedisService $redisService)
19
    {
20
        $this->taskRepository = $taskRepository;
21
        $this->redisService = $redisService;
22
    }
23
24
    protected function getTaskRepository(): TaskRepository
25
    {
26
        return $this->taskRepository;
27
    }
28
29
    protected function getTaskFromDb(int $taskId, int $userId)
30
    {
31
        return $this->getTaskRepository()->checkAndGetTask($taskId, $userId);
32
    }
33
34
    public function getAllTasks(): array
35
    {
36
        return $this->getTaskRepository()->getAllTasks();
37
    }
38
39
    public function getAll(int $userId): array
40
    {
41
        return $this->getTaskRepository()->getAll($userId);
42
    }
43
44 View Code Duplication
    public function getOne(int $taskId, int $userId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    {
46
        if (self::isRedisEnabled() === true) {
47
            $task = $this->getTaskFromCache($taskId, $userId);
48
        } else {
49
            $task = $this->getTaskFromDb($taskId, $userId);
50
        }
51
52
        return $task;
53
    }
54
55 View Code Duplication
    public function getTaskFromCache(int $taskId, int $userId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57
        $redisKey = sprintf(self::REDIS_KEY, $taskId, $userId);
58
        $key = $this->redisService->generateKey($redisKey);
59
        if ($this->redisService->exists($key)) {
60
            $task = $this->redisService->get($key);
61
        } else {
62
            $task = $this->getTaskFromDb($taskId, $userId);
63
            $this->redisService->setex($key, $task);
64
        }
65
66
        return $task;
67
    }
68
69
    public function search($tasksName, int $userId, $status): array
70
    {
71
        if ($status !== null) {
72
            $status = (int) $status;
73
        }
74
75
        return $this->getTaskRepository()->search($tasksName, $userId, $status);
76
    }
77
78
    public function saveInCache($taskId, $userId, $tasks)
79
    {
80
        $redisKey = sprintf(self::REDIS_KEY, $taskId, $userId);
81
        $key = $this->redisService->generateKey($redisKey);
82
        $this->redisService->setex($key, $tasks);
83
    }
84
85
    public function deleteFromCache($taskId, $userId)
86
    {
87
        $redisKey = sprintf(self::REDIS_KEY, $taskId, $userId);
88
        $key = $this->redisService->generateKey($redisKey);
89
        $this->redisService->del($key);
90
    }
91
92
    public function create(array $input)
93
    {
94
        $task = new \stdClass();
95
        $data = json_decode(json_encode($input), false);
96
        if (empty($data->name)) {
97
            throw new TaskException('The field "name" is required.', 400);
98
        }
99
        $task->name = self::validateTaskName($data->name);
100
        $task->description = null;
101
        if (isset($data->description)) {
102
            $task->description = $data->description;
103
        }
104
        $task->status = 0;
105
        if (isset($data->status)) {
106
            $task->status = self::validateTaskStatus($data->status);
107
        }
108
        $task->userId = $data->decoded->sub;
109
        $tasks = $this->getTaskRepository()->create($task);
110
        if (self::isRedisEnabled() === true) {
111
            $this->saveInCache($tasks->id, $task->userId, $tasks);
112
        }
113
114
        return $tasks;
115
    }
116
117
    public function update(array $input, int $taskId)
118
    {
119
        $task = $this->getTaskFromDb($taskId, (int) $input['decoded']->sub);
120
        $data = json_decode(json_encode($input), false);
121
        if (!isset($data->name) && !isset($data->status)) {
122
            throw new TaskException('Enter the data to update the task.', 400);
123
        }
124
        if (isset($data->name)) {
125
            $task->name = self::validateTaskName($data->name);
126
        }
127
        if (isset($data->description)) {
128
            $task->description = $data->description;
129
        }
130
        if (isset($data->status)) {
131
            $task->status = self::validateTaskStatus($data->status);
132
        }
133
        $task->userId = $data->decoded->sub;
134
        $tasks = $this->getTaskRepository()->update($task);
135
        if (self::isRedisEnabled() === true) {
136
            $this->saveInCache($tasks->id, $task->userId, $tasks);
137
        }
138
139
        return $tasks;
140
    }
141
142 View Code Duplication
    public function delete(int $taskId, int $userId): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
143
    {
144
        $this->getTaskFromDb($taskId, $userId);
145
        $data = $this->getTaskRepository()->delete($taskId, $userId);
146
        if (self::isRedisEnabled() === true) {
147
            $this->deleteFromCache($taskId, $userId);
148
        }
149
150
        return $data;
151
    }
152
}
153