NoteListFinderRepository   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 52
dl 0
loc 119
ccs 56
cts 56
cp 1
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A findAllNotesExceptMainWithUserByClientId() 0 28 2
A __construct() 0 4 1
A findAllNotesExceptMainByUserId() 0 18 2
A findMostRecentNotes() 0 27 2
1
<?php
2
3
namespace App\Module\Note\List\Repository;
4
5
use App\Infrastructure\Database\Hydrator;
6
use App\Infrastructure\Database\QueryFactory;
0 ignored issues
show
Bug introduced by
The type App\Infrastructure\Database\QueryFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use App\Module\Note\List\Data\NoteResultData;
8
9
class NoteListFinderRepository
10
{
11
    /** Fields for queries that populate @see NoteResultData */
12
    public array $noteResultFields = [
13
        'id' => 'note.id',
14
        'client_id' => 'note.client_id',
15
        'message' => 'note.message',
16
        'hidden' => 'note.hidden',
17
        'updated_at' => 'note.updated_at',
18
        'created_at' => 'note.created_at',
19
        'deleted_at' => 'note.deleted_at',
20
        'user_id' => 'note.user_id',
21
    ];
22
23 13
    public function __construct(
24
        private readonly QueryFactory $queryFactory,
25
        private readonly Hydrator $hydrator,
26
    ) {
27 13
    }
28
29
    /**
30
     * Return all notes which are linked to the given user except the main note.
31
     *
32
     * @param int $userId
33
     *
34
     * @return NoteResultData[]
35
     */
36 1
    public function findAllNotesExceptMainByUserId(int $userId): array
37
    {
38 1
        $query = $this->queryFactory->selectQuery()->from('note');
39
40 1
        $concatName = $query->func()->concat(['user.first_name' => 'identifier', ' ', 'user.last_name' => 'identifier']);
41
42 1
        $query->select(array_merge($this->noteResultFields, ['user_full_name' => $concatName]))
43 1
            ->join(['table' => 'user', 'conditions' => 'note.user_id = user.id'])
44 1
            ->andWhere([
45
                // Not unsafe as it's not an expression and thus escaped by querybuilder
46 1
                'note.user_id' => $userId,
47 1
                'note.is_main' => 0,
48 1
                'note.deleted_at IS' => null,
49 1
            ]);
50 1
        $resultRows = $query->execute()->fetchAll('assoc') ?: [];
51
52
        // Convert to list of Note objects with associated User info
53 1
        return $this->hydrator->hydrate($resultRows, NoteResultData::class);
54
    }
55
56
    /**
57
     * Return all notes which are linked to the given client
58
     * from most recent to oldest EXCEPT for the main note.
59
     *
60
     * @param int $clientId
61
     *
62
     * @return NoteResultData[]
63
     */
64 6
    public function findAllNotesExceptMainWithUserByClientId(int $clientId): array
65
    {
66 6
        $query = $this->queryFactory->selectQuery()->from('note');
67
68 6
        $concatName = $query->func()->concat([
69 6
            $query->func()->coalesce(['user.first_name' => 'identifier', '']),
70 6
            ' ',
71 6
            $query->func()->coalesce(['user.last_name' => 'identifier', '']),
72 6
        ]);
73
74 6
        $query->select(array_merge($this->noteResultFields, ['user_full_name' => $concatName]))
75 6
            ->join(['user' => ['table' => 'user', 'type' => 'LEFT', 'conditions' => 'note.user_id = user.id']])
76 6
            ->join(['client' => ['table' => 'client', 'type' => 'LEFT', 'conditions' => 'note.client_id = client.id']])
77 6
            ->where(
78 6
                [
79
                    // Not unsafe as it's not an expression and thus escaped by querybuilder
80 6
                    'note.client_id' => $clientId,
81 6
                    'note.is_main' => 0,
82 6
                    'OR' => [
83 6
                        'note.deleted_at IS' => null,
84 6
                        'note.deleted_at' => $query->identifier('client.deleted_at'),
85 6
                    ],
86 6
                ]
87 6
            )->orderByDesc('note.created_at');
88 6
        $resultRows = $query->execute()->fetchAll('assoc') ?: [];
89
90
        // Convert to list of Note objects with associated User info
91 6
        return $this->hydrator->hydrate($resultRows, NoteResultData::class);
92
    }
93
94
    /**
95
     * Returns given amount of notes ordered by most recent.
96
     *
97
     * @param int $notesAmount
98
     *
99
     * @return NoteResultData[]
100
     */
101 1
    public function findMostRecentNotes(int $notesAmount): array
102
    {
103 1
        $query = $this->queryFactory->selectQuery()->from('note');
104
105 1
        $concatUserName = $query->func()->concat(
106
            // Cake interprets the string literally with "literal", so IFNULL() and column are interpreted as raw sql
107 1
            ['IFNULL(user.first_name, "")' => 'literal', ' ', 'IFNULL(user.last_name, "")' => 'identifier']
108 1
        );
109 1
        $concatClientName = $query->func()->concat(
110 1
            ['IFNULL(client.first_name, "")' => 'literal', ' ', 'IFNULL(client.last_name, "")' => 'identifier']
111 1
        );
112
113 1
        $query->select(
114 1
            array_merge($this->noteResultFields, [
115 1
                'user_full_name' => $concatUserName,
116 1
                'client_full_name' => $concatClientName,
117 1
            ])
118 1
        )
119 1
            ->join(['table' => 'user', 'conditions' => 'note.user_id = user.id'])
120 1
            ->leftJoin('client', ['note.client_id = client.id'])
121 1
            ->andWhere(['note.deleted_at IS' => null, 'note.is_main' => 0])
122 1
            ->orderBy(['note.updated_at' => 'DESC', 'note.created_at' => 'DESC'])->limit($notesAmount);
123
124 1
        $resultRows = $query->execute()->fetchAll('assoc') ?: [];
125
126
        // Convert to list of Note objects with associated User info
127 1
        return $this->hydrator->hydrate($resultRows, NoteResultData::class);
128
    }
129
}
130