TaskRepository   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 115
Duplicated Lines 16.52 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 19
loc 115
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A checkAndGetTask() 0 14 2
A getAllTasks() 0 8 1
A getAll() 9 9 1
A search() 0 14 3
A getSearchTasksQuery() 0 13 3
A create() 0 15 1
A update() 0 17 1
A delete() 10 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Repository;
6
7
use App\Exception\TaskException;
8
9
class TaskRepository extends BaseRepository
10
{
11
    public function __construct(\PDO $database)
12
    {
13
        $this->database = $database;
14
    }
15
16
    public function checkAndGetTask(int $taskId, int $userId)
17
    {
18
        $query = 'SELECT * FROM `tasks` WHERE `id` = :id AND `userId` = :userId';
19
        $statement = $this->getDb()->prepare($query);
20
        $statement->bindParam('id', $taskId);
21
        $statement->bindParam('userId', $userId);
22
        $statement->execute();
23
        $task = $statement->fetchObject();
24
        if (empty($task)) {
25
            throw new TaskException('Task not found.', 404);
26
        }
27
28
        return $task;
29
    }
30
31
    public function getAllTasks(): array
32
    {
33
        $query = 'SELECT * FROM `tasks` ORDER BY `id`';
34
        $statement = $this->getDb()->prepare($query);
35
        $statement->execute();
36
37
        return $statement->fetchAll();
38
    }
39
40 View Code Duplication
    public function getAll(int $userId): array
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...
41
    {
42
        $query = 'SELECT * FROM `tasks` WHERE `userId` = :userId ORDER BY `id`';
43
        $statement = $this->getDb()->prepare($query);
44
        $statement->bindParam('userId', $userId);
45
        $statement->execute();
46
47
        return $statement->fetchAll();
48
    }
49
50
    public function search($tasksName, int $userId, $status): array
51
    {
52
        $query = $this->getSearchTasksQuery($status);
53
        $name = '%' . $tasksName . '%';
54
        $statement = $this->getDb()->prepare($query);
55
        $statement->bindParam('name', $name);
56
        $statement->bindParam('userId', $userId);
57
        if ($status === 0 || $status === 1) {
58
            $statement->bindParam('status', $status);
59
        }
60
        $statement->execute();
61
62
        return $statement->fetchAll();
63
    }
64
65
    private function getSearchTasksQuery($status)
66
    {
67
        $statusQuery = '';
68
        if ($status === 0 || $status === 1) {
69
            $statusQuery = 'AND `status` = :status';
70
        }
71
72
        return "
73
            SELECT * FROM `tasks`
74
            WHERE `name` LIKE :name AND `userId` = :userId $statusQuery
75
            ORDER BY `id`
76
        ";
77
    }
78
79
    public function create($task)
80
    {
81
        $query = '
82
            INSERT INTO `tasks` (`name`, `description`, `status`, `userId`)
83
            VALUES (:name, :description, :status, :userId)
84
        ';
85
        $statement = $this->getDb()->prepare($query);
86
        $statement->bindParam('name', $task->name);
87
        $statement->bindParam('description', $task->description);
88
        $statement->bindParam('status', $task->status);
89
        $statement->bindParam('userId', $task->userId);
90
        $statement->execute();
91
92
        return $this->checkAndGetTask((int) $this->database->lastInsertId(), (int) $task->userId);
93
    }
94
95
    public function update($task)
96
    {
97
        $query = '
98
            UPDATE `tasks`
99
            SET `name` = :name, `description` = :description, `status` = :status
100
            WHERE `id` = :id AND `userId` = :userId
101
        ';
102
        $statement = $this->getDb()->prepare($query);
103
        $statement->bindParam('id', $task->id);
104
        $statement->bindParam('name', $task->name);
105
        $statement->bindParam('description', $task->description);
106
        $statement->bindParam('status', $task->status);
107
        $statement->bindParam('userId', $task->userId);
108
        $statement->execute();
109
110
        return $this->checkAndGetTask((int) $task->id, (int) $task->userId);
111
    }
112
113 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...
114
    {
115
        $query = 'DELETE FROM `tasks` WHERE `id` = :id AND `userId` = :userId';
116
        $statement = $this->getDb()->prepare($query);
117
        $statement->bindParam('id', $taskId);
118
        $statement->bindParam('userId', $userId);
119
        $statement->execute();
120
121
        return 'The task was deleted.';
122
    }
123
}
124