Completed
Push — master ( 8dc5cd...a72e46 )
by Mauro
02:22
created

TaskRepository::searchTasks()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 14
loc 14
rs 9.4285
cc 2
eloc 10
nc 2
nop 1
1
<?php
2
3
namespace App\Repository;
4
5
use App\Service\MessageService;
6
7
/**
8
 * Tasks Repository.
9
 */
10
class TaskRepository extends BaseRepository
11
{
12
    /**
13
     * @param object $database
14
     */
15
    public function __construct(\PDO $database = null)
16
    {
17
        $this->database = $database;
18
    }
19
20
    /**
21
     * Check if the task exists.
22
     *
23
     * @param int $taskId
24
     * @return array $task
25
     * @throws \Exception
26
     */
27
    public function checkTask($taskId)
28
    {
29
        $query = $this->getTaskQuery();
30
        $statement = $this->database->prepare($query);
31
        $statement->bindParam('id', $taskId);
32
        $statement->execute();
33
        $task = $statement->fetchObject();
34
        if (empty($task)) {
35
            throw new \Exception(MessageService::TASK_NOT_FOUND, 404);
36
        }
37
38
        return $task;
39
    }
40
41
    /**
42
     * Get all tasks.
43
     *
44
     * @return array
45
     */
46
    public function getTasks()
47
    {
48
        $query = $this->getTasksQuery();
49
        $statement = $this->database->prepare($query);
50
        $statement->execute();
51
52
        return $statement->fetchAll();
53
    }
54
55
    /**
56
     * Search tasks by name.
57
     *
58
     * @param string $tasksName
59
     * @return array
60
     * @throws \Exception
61
     */
62 View Code Duplication
    public function searchTasks($tasksName)
63
    {
64
        $query = $this->searchTasksQuery();
65
        $statement = $this->database->prepare($query);
66
        $query = '%' . $tasksName . '%';
67
        $statement->bindParam('query', $query);
68
        $statement->execute();
69
        $tasks = $statement->fetchAll();
70
        if (!$tasks) {
71
            throw new \Exception(MessageService::TASK_NOT_FOUND, 404);
72
        }
73
74
        return $tasks;
75
    }
76
77
    /**
78
     * Get Task Sql Query.
79
     *
80
     * @return string
81
     */
82
    public function getTaskQuery()
83
    {
84
        return 'SELECT * FROM tasks WHERE id=:id';
85
    }
86
87
    public function getTasksQuery()
88
    {
89
        return 'SELECT * FROM tasks ORDER BY task';
90
    }
91
92
    public function searchTasksQuery()
93
    {
94
        return 'SELECT * FROM tasks WHERE UPPER(task) LIKE :query ORDER BY task';
95
    }
96
97
    public function createTaskQuery()
98
    {
99
        return 'INSERT INTO tasks (task, status) VALUES (:task, :status)';
100
    }
101
102
    public function updateTaskQuery()
103
    {
104
        return 'UPDATE tasks SET task=:task, status=:status WHERE id=:id';
105
    }
106
107
    public function deleteTaskQuery()
108
    {
109
        return 'DELETE FROM tasks WHERE id=:id';
110
    }
111
}
112