Passed
Push — dev ( 6fa4a9...182017 )
by Janko
16:15 queued 15s
created

PrivateMessageRepository::getAmountSince()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 1
dl 0
loc 21
ccs 0
cts 14
cp 0
crap 2
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Orm\Repository;
6
7
use Doctrine\ORM\EntityRepository;
8
use Override;
0 ignored issues
show
Bug introduced by
The type Override 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...
9
use Stu\Module\Game\Component\GameComponentEnum;
10
use Stu\Module\Message\Lib\PrivateMessageFolderTypeEnum;
11
use Stu\Orm\Entity\PrivateMessage;
12
use Stu\Orm\Entity\PrivateMessageFolder;
13
use Stu\Orm\Entity\PrivateMessageFolderInterface;
14
use Stu\Orm\Entity\PrivateMessageInterface;
15
use Stu\Orm\Entity\UserInterface;
16
17
/**
18
 * @extends EntityRepository<PrivateMessage>
19
 */
20
final class PrivateMessageRepository extends EntityRepository implements PrivateMessageRepositoryInterface
21
{
22
    #[Override]
23
    public function prototype(): PrivateMessageInterface
24
    {
25
        return new PrivateMessage();
26
    }
27
28
    #[Override]
29
    public function save(PrivateMessageInterface $post, bool $doFlush = false): void
30
    {
31
        $em = $this->getEntityManager();
32
33
        $em->persist($post);
34
35
        if ($doFlush) {
36
            $em->flush();
37
        }
38
    }
39
40
    #[Override]
41
    public function getOrderedCorrepondence(
42
        int $userId,
43
        int $otherUserId,
44
        array $specialIds,
45
        int $limit
46
    ): array {
47
        return $this->getEntityManager()
48
            ->createQuery(
49
                sprintf(
50
                    'SELECT pm FROM %s pm
51
                    JOIN %s pmf
52
                    WITH pm.cat_id = pmf.id
53
                    WHERE ( (pm.send_user = :userId
54
                                AND pm.recip_user = :otherUserId)
55
                            OR
56
                            (pm.send_user = :otherUserId
57
                                AND pm.recip_user = :userId
58
                                AND pm.deleted IS NULL))
59
                    AND pmf.special in (:specialIds)
60
                    ORDER BY pm.date DESC',
61
                    PrivateMessage::class,
62
                    PrivateMessageFolder::class
63
                )
64
            )
65
            ->setParameters([
66
                'userId' => $userId,
67
                'otherUserId' => $otherUserId,
68
                'specialIds' => $specialIds
69
            ])
70
            ->setMaxResults($limit)
71
            ->getResult();
72
    }
73
74
    #[Override]
75
    public function getBySender(UserInterface $user): array
76
    {
77
        return $this->findBy(
78
            ['send_user' => $user->getId()]
79
        );
80
    }
81
82
    #[Override]
83
    public function getByReceiver(UserInterface $user): array
84
    {
85
        return $this->findBy(
86
            ['recip_user' => $user->getId()]
87
        );
88
    }
89
90 1
    #[Override]
91
    public function getByUserAndFolder(
92
        int $userId,
93
        int $folderId,
94
        int $offset,
95
        int $limit
96
    ): array {
97 1
        return $this->findBy(
98 1
            ['recip_user' => $userId, 'cat_id' => $folderId, 'deleted' => null],
99 1
            ['date' => 'desc', 'id' => 'desc'],
100 1
            $limit,
101 1
            $offset
102 1
        );
103
    }
104
105 4
    #[Override]
106
    public function getAmountByFolder(PrivateMessageFolderInterface $privateMessageFolder): int
107
    {
108 4
        return $this->count([
109 4
            'category' => $privateMessageFolder,
110 4
            'deleted' => null
111 4
        ]);
112
    }
113
114 8
    #[Override]
115
    public function getNewAmountByFolder(PrivateMessageFolderInterface $privateMessageFolder): int
116
    {
117 8
        return $this->count([
118 8
            'category' => $privateMessageFolder,
119 8
            'new' => 1,
120 8
            'deleted' => null
121 8
        ]);
122
    }
123
124 1
    public function getNewAmountByFolderAndSender(PrivateMessageFolderInterface $privateMessageFolder, UserInterface $sender): int
125
    {
126 1
        return $this->count([
127 1
            'category' => $privateMessageFolder,
128 1
            'sendingUser' => $sender,
129 1
            'new' => 1,
130 1
            'deleted' => null
131 1
        ]);
132
    }
133
134
    #[Override]
135
    public function setDeleteTimestampByFolder(int $folderId, int $timestamp): void
136
    {
137
        $this->getEntityManager()->createQuery(
138
            sprintf(
139
                'UPDATE %s pm SET pm.deleted = :timestamp WHERE pm.cat_id = :folderId',
140
                PrivateMessage::class
141
            )
142
        )->setParameters([
143
            'folderId' => $folderId,
144
            'timestamp' => $timestamp
145
        ])->execute();
146
    }
147
148 200
    #[Override]
149
    public function hasRecentMessage(UserInterface $user): bool
150
    {
151 200
        return (int)$this->getEntityManager()->createQuery(
152 200
            sprintf(
153 200
                'SELECT count(pm.id)
154
                    FROM %s pm
155
                    WHERE pm.receivingUser = :user
156
                    AND pm.new = :true
157 200
                    AND pm.date > :threshold',
158 200
                PrivateMessage::class
159 200
            )
160 200
        )
161 200
            ->setParameters([
162 200
                'user' => $user,
163 200
                'threshold' => time() - GameComponentEnum::PM->getRefreshIntervalInSeconds(),
164 200
                'true' => true
165 200
            ])
166 200
            ->getSingleScalarResult() > 0;
167
    }
168
169 1
    #[Override]
170
    public function getConversations(UserInterface $user): array
171
    {
172 1
        return $this->getEntityManager()->createQuery(
173 1
            sprintf(
174 1
                'SELECT pm FROM %1$s pm
175
                JOIN %2$s pmf
176
                WITH pm.cat_id = pmf.id
177
                LEFT JOIN %1$s inbox
178
                WITH pm.inbox_pm_id = inbox.id
179
                LEFT JOIN %2$s pmfinbox
180
                WITH inbox.cat_id = pmfinbox.id
181
                WHERE pmf.special in (:main, :out)
182
                AND (pmfinbox.special is null or pmfinbox.special = :main)
183
                AND pm.receivingUser = :user
184
                AND pm.deleted IS NULL
185
                AND  NOT EXISTS (SELECT pm2.id FROM %1$s pm2
186
                                    WHERE pm2.send_user = pm.send_user
187
                                    AND pm2.cat_id = :out
188
                                    AND pm2.recip_user = pm.recip_user 
189
                                    AND pm2.date < pm.date)
190 1
                ORDER BY pm.id DESC',
191 1
                PrivateMessage::class,
192 1
                PrivateMessageFolder::class
193 1
            )
194 1
        )
195 1
            ->setParameters([
196 1
                'user' => $user,
197 1
                'main' => PrivateMessageFolderTypeEnum::SPECIAL_MAIN,
198 1
                'out' => PrivateMessageFolderTypeEnum::SPECIAL_PMOUT
199 1
            ])
200 1
            ->getResult();
201
    }
202
203
    #[Override]
204
    public function getAmountSince(int $timestamp): int
205
    {
206
207
        return (int)$this->getEntityManager()->createQuery(
208
            sprintf(
209
                'SELECT count(pm.id)
210
                    FROM %s pm
211
                    JOIN %s pmf
212
                    WITH pm.category = pmf
213
                    WHERE pm.date > :threshold
214
                    AND pmf.special != :outbox',
215
                PrivateMessage::class,
216
                PrivateMessageFolder::class
217
            )
218
        )
219
            ->setParameters([
220
                'threshold' => $timestamp,
221
                'outbox' => PrivateMessageFolderTypeEnum::SPECIAL_PMOUT
222
            ])
223
            ->getSingleScalarResult();
224
    }
225
226
    #[Override]
227
    public function unsetAllInboxReferences(): void
228
    {
229
        $this->getEntityManager()->createQuery(
230
            sprintf(
231
                'UPDATE %s pm
232
                SET pm.inbox_pm_id = null',
233
                PrivateMessage::class
234
            )
235
        )->execute();
236
    }
237
238
    #[Override]
239
    public function truncateAllPrivateMessages(): void
240
    {
241
        $this->getEntityManager()->createQuery(
242
            sprintf(
243
                'DELETE FROM %s pm',
244
                PrivateMessage::class
245
            )
246
        )->execute();
247
    }
248
}
249