Issues (26)

src/Service/Task/TaskService.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Service\Task;
6
7
use App\Entity\Task;
8
9
final class TaskService extends Base
10
{
11
    /**
12
     * @return array<string>
13
     */
14
    public function getTasksByPage(
15
        int $userId,
16
        int $page,
17
        int $perPage,
18
        ?string $name,
19
        ?string $description,
20
        ?string $status
21
    ): array {
22
        if ($page < 1) {
23
            $page = 1;
24
        }
25
        if ($perPage < 1) {
26
            $perPage = self::DEFAULT_PER_PAGE_PAGINATION;
27
        }
28
29
        return $this->getTaskRepository()->getTasksByPage(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getTaskRep... $description, $status) returns the type array<string,array|array<string,double|integer>> which is incompatible with the documented return type string[].
Loading history...
30
            $userId,
31
            $page,
32
            $perPage,
33
            $name,
34
            $description,
35
            $status
36
        );
37
    }
38
39
    /**
40
     * @return array<string>
41
     */
42
    public function getAllTasks(): array
43
    {
44
        return $this->getTaskRepository()->getAllTasks();
45
    }
46
47
    /**
48
     * @return array<string>
49
     */
50
    public function getAllByUser(int $userId): array
51
    {
52
        return $this->taskRepository->getTasksByUserId($userId);
53
    }
54
55
    public function getOne(int $taskId, int $userId): object
56
    {
57
        if (self::isRedisEnabled() === true) {
58
            $task = $this->getTaskFromCache($taskId, $userId);
59
        } else {
60
            $task = $this->getTaskFromDb($taskId, $userId)->toJson();
61
        }
62
63
        return $task;
64
    }
65
66
    /**
67
     * @param array<string> $input
68
     */
69
    public function create(array $input): object
70
    {
71
        $data = json_decode((string) json_encode($input), false);
72
        if (!isset($data->name)) {
73
            throw new \App\Exception\TaskException('The field "name" is required.', 400);
74
        }
75
        $task = new Task();
76
        $task->updateName(self::validateTaskName($data->name));
77
        $description = isset($data->description) ? $data->description : null;
78
        $task->updateDescription($description);
79
        $status = 0;
80
        if (isset($data->status)) {
81
            $status = self::validateTaskStatus($data->status);
82
        }
83
        $task->updateStatus($status);
84
        $task->updateUserId((int) $data->decoded->sub);
85
        /** @var Task $task */
86
        $response = $this->getTaskRepository()->create($task);
87
        if (self::isRedisEnabled() === true) {
88
            $this->saveInCache(
89
                $response->getId(),
90
                $response->getUserId(),
91
                $response->toJson()
92
            );
93
        }
94
        if (self::isLoggerEnabled() === true) {
95
            $this->loggerService->setInfo('The task with the ID ' . $response->getId() . ' has created successfully.');
96
        }
97
98
        return $response->toJson();
99
    }
100
101
    /**
102
     * @param array<string> $input
103
     */
104
    public function update(array $input, int $taskId): object
105
    {
106
        $data = $this->validateTask($input, $taskId);
107
        /** @var Task $task */
108
        $task = $this->getTaskRepository()->update($data);
109
        if (self::isRedisEnabled() === true) {
110
            $this->saveInCache(
111
                $task->getId(),
112
                $data->getUserId(),
113
                $task->toJson()
114
            );
115
        }
116
        if (self::isLoggerEnabled() === true) {
117
            $this->loggerService->setInfo('The task with the ID ' . $task->getId() . ' has updated successfully.');
118
        }
119
120
        return $task->toJson();
121
    }
122
123
    public function delete(int $taskId, int $userId): void
124
    {
125
        $this->getTaskFromDb($taskId, $userId);
126
        $this->getTaskRepository()->delete($taskId, $userId);
127
        if (self::isRedisEnabled() === true) {
128
            $this->deleteFromCache($taskId, $userId);
129
        }
130
        if (self::isLoggerEnabled() === true) {
131
            $this->loggerService->setInfo('The task with the ID ' . $taskId . ' from the User with the Id ' . $userId . ' has deleted successfully.');
132
        }
133
    }
134
135
    private function validateTask(array $input, int $taskId): Task
136
    {
137
        $task = $this->getTaskFromDb($taskId, (int) $input['decoded']->sub);
138
        $data = json_decode((string) json_encode($input), false);
139
        if (!isset($data->name) && !isset($data->status)) {
140
            throw new \App\Exception\TaskException('Enter the data to update the task.', 400);
141
        }
142
        if (isset($data->name)) {
143
            $task->updateName(self::validateTaskName($data->name));
144
        }
145
        if (isset($data->description)) {
146
            $task->updateDescription($data->description);
147
        }
148
        if (isset($data->status)) {
149
            $task->updateStatus(self::validateTaskStatus($data->status));
150
        }
151
        $userId = null;
152
        if (isset($data->decoded) && isset($data->decoded->sub)) {
153
            $userId = (int) $data->decoded->sub;
154
        }
155
        $task->updateUserId($userId);
156
157
        return $task;
158
    }
159
160
    public function search(string $tasksName, int $userId, ?string $status): array
161
    {
162
        if ($status !== null) {
163
            $status = (int) $status;
164
        }
165
166
        return $this->taskRepository->search($tasksName, $userId, $status);
167
    }
168
}
169