Passed
Push — master ( 8387eb...5a11dc )
by Janko
07:59
created

PrivateMessageRepository::truncateAllPrivateMessages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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