Completed
Push — master ( 2b5a5a...a3a238 )
by Mauro
02:35
created

TaskService::checkTask()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 2
eloc 10
nc 2
nop 1
1
<?php
2
3
namespace App\Service;
4
5
use App\Service\MessageService;
6
use App\Service\ValidationService as vs;
7
use App\Repository\TaskRepository;
8
9
/**
10
 * Tasks Service.
11
 */
12
class TaskService extends BaseService
13
{
14
    /**
15
     * Constructor of the class.
16
     *
17
     * @param object $database
18
     */
19
    public function __construct(\PDO $database)
20
    {
21
        $this->database = $database;
22
    }
23
24
    /**
25
     * Check if the task exists.
26
     *
27
     * @param int $taskId
28
     * @return object $task
29
     * @throws \Exception
30
     */
31
    public function checkTask($taskId)
32
    {
33
        $tasksRepository = new TaskRepository;
34
        $query = $tasksRepository->getTaskQuery();
35
        $statement = $this->database->prepare($query);
36
        $statement->bindParam('id', $taskId);
37
        $statement->execute();
38
        $task = $statement->fetchObject();
39
        if (!$task) {
40
            throw new \Exception(MessageService::TASK_NOT_FOUND, 404);
41
        }
42
43
        return $task;
44
    }
45
46
    /**
47
     * Get all tasks.
48
     *
49
     * @return array
50
     */
51
    public function getTasks()
52
    {
53
        $repository = new TaskRepository;
54
        $query = $repository->getTasksQuery();
55
        $statement = $this->database->prepare($query);
56
        $statement->execute();
57
58
        return $statement->fetchAll();
59
    }
60
61
    /**
62
     * Get one task by id.
63
     *
64
     * @param int $taskId
65
     * @return array
66
     */
67
    public function getTask($taskId)
68
    {
69
        $task = $this->checkTask($taskId);
70
71
        return $task;
72
    }
73
74
    /**
75
     * Search tasks by name.
76
     *
77
     * @param string $tasksName
78
     * @return array
79
     * @throws \Exception
80
     */
81 View Code Duplication
    public function searchTasks($tasksName)
82
    {
83
        $repository = new TaskRepository;
84
        $query = $repository->searchTasksQuery();
85
        $statement = $this->database->prepare($query);
86
        $query = '%' . $tasksName . '%';
87
        $statement->bindParam('query', $query);
88
        $statement->execute();
89
        $tasks = $statement->fetchAll();
90
        if (!$tasks) {
91
            throw new \Exception(MessageService::TASK_NOT_FOUND, 404);
92
        }
93
94
        return $tasks;
95
    }
96
97
    /**
98
     * Create a task.
99
     *
100
     * @param array $input
101
     * @return array
102
     * @throws \Exception
103
     */
104 View Code Duplication
    public function createTask($input)
105
    {
106
        $data = vs::validateInputOnCreateTask($input);
107
        $repository = new TaskRepository;
108
        $query = $repository->createTaskQuery();
109
        $statement = $this->database->prepare($query);
110
        $statement->bindParam('task', $data['task']);
111
        $statement->bindParam('status', $data['status']);
112
        $statement->execute();
113
        $task = $this->checkTask($this->database->lastInsertId());
114
115
        return $task;
116
    }
117
118
    /**
119
     * Update a task.
120
     *
121
     * @param array $input
122
     * @param int $taskId
123
     * @return array
124
     * @throws \Exception
125
     */
126 View Code Duplication
    public function updateTask($input, $taskId)
127
    {
128
        $task = $this->checkTask($taskId);
129
        $data = vs::validateInputOnUpdateTask($input, $task);
130
        $repository = new TaskRepository;
131
        $query = $repository->updateTaskQuery();
132
        $statement = $this->database->prepare($query);
133
        $statement->bindParam('id', $taskId);
134
        $statement->bindParam('task', $data['task']);
135
        $statement->bindParam('status', $data['status']);
136
        $statement->execute();
137
138
        return $this->checkTask($taskId);
139
    }
140
141
    /**
142
     * Delete a task.
143
     *
144
     * @param int $taskId
145
     * @return array
146
     */
147 View Code Duplication
    public function deleteTask($taskId)
1 ignored issue
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...
148
    {
149
        $this->checkTask($taskId);
150
        $repository = new TaskRepository;
151
        $query = $repository->deleteTaskQuery();
152
        $statement = $this->database->prepare($query);
153
        $statement->bindParam('id', $taskId);
154
        $statement->execute();
155
156
        return MessageService::TASK_DELETED;
157
    }
158
}
159