NoteRepository::checkAndGetNote()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Repository;
6
7
use App\Exception\NoteException;
8
9
class NoteRepository extends BaseRepository
10
{
11
    public function __construct(\PDO $database)
12
    {
13
        $this->database = $database;
14
    }
15
16
    public function checkAndGetNote(int $noteId)
17
    {
18
        $query = 'SELECT * FROM `notes` WHERE `id` = :id';
19
        $statement = $this->database->prepare($query);
20
        $statement->bindParam(':id', $noteId);
21
        $statement->execute();
22
        $note = $statement->fetchObject();
23
        if (empty($note)) {
24
            throw new NoteException('Note not found.', 404);
25
        }
26
27
        return $note;
28
    }
29
30
    public function getNotes(): array
31
    {
32
        $query = 'SELECT * FROM `notes` ORDER BY `id`';
33
        $statement = $this->database->prepare($query);
34
        $statement->execute();
35
36
        return $statement->fetchAll();
37
    }
38
39
    public function searchNotes(string $strNotes): array
40
    {
41
        $query = 'SELECT * FROM `notes` WHERE `name` LIKE :name OR `description` LIKE :description ORDER BY `id`';
42
        $name = '%' . $strNotes . '%';
43
        $description = '%' . $strNotes . '%';
44
        $statement = $this->database->prepare($query);
45
        $statement->bindParam('name', $name);
46
        $statement->bindParam('description', $description);
47
        $statement->execute();
48
        $notes = $statement->fetchAll();
49
        if (!$notes) {
50
            throw new NoteException('No notes with that name or description were found.', 404);
51
        }
52
53
        return $notes;
54
    }
55
56
    public function createNote($data)
57
    {
58
        $query = 'INSERT INTO `notes` (`name`, `description`) VALUES (:name, :description)';
59
        $statement = $this->database->prepare($query);
60
        $statement->bindParam(':name', $data->name);
61
        $statement->bindParam(':description', $data->description);
62
        $statement->execute();
63
64
        return $this->checkAndGetNote((int) $this->database->lastInsertId());
65
    }
66
67 View Code Duplication
    public function updateNote($note)
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...
68
    {
69
        $query = 'UPDATE `notes` SET `name` = :name, `description` = :description WHERE `id` = :id';
70
        $statement = $this->database->prepare($query);
71
        $statement->bindParam(':id', $note->id);
72
        $statement->bindParam(':name', $note->name);
73
        $statement->bindParam(':description', $note->description);
74
        $statement->execute();
75
76
        return $this->checkAndGetNote((int) $note->id);
77
    }
78
79
    public function deleteNote(int $noteId)
80
    {
81
        $query = 'DELETE FROM `notes` WHERE `id` = :id';
82
        $statement = $this->database->prepare($query);
83
        $statement->bindParam(':id', $noteId);
84
        $statement->execute();
85
    }
86
}
87