Passed
Push — dev ( 4c76dc...4abd48 )
by Janko
09:59
created

PrivateMessageRepository::hasRecentMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 14
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 19
ccs 14
cts 14
cp 1
crap 1
rs 9.7998
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 $senderUserId,
43
        int $recipientUserId,
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 = :sendUserId AND pm.recip_user = :recipUserId) OR
54
                        (pm.send_user = :recipUserId AND pm.recip_user = :sendUserId))
55
                    AND pmf.special in (:specialIds)
56
                    AND pm.deleted IS NULL
57
                    ORDER BY pm.date DESC',
58
                    PrivateMessage::class,
59
                    PrivateMessageFolder::class
60
                )
61
            )
62
            ->setParameters([
63
                'sendUserId' => $senderUserId,
64
                'recipUserId' => $recipientUserId,
65
                'specialIds' => $specialIds
66
            ])
67
            ->setMaxResults($limit)
68
            ->getResult();
69
    }
70
71
    #[Override]
72
    public function getBySender(UserInterface $user): array
73
    {
74
        return $this->findBy(
75
            ['send_user' => $user->getId()]
76
        );
77
    }
78
79
    #[Override]
80
    public function getByReceiver(UserInterface $user): array
81
    {
82
        return $this->findBy(
83
            ['recip_user' => $user->getId()]
84
        );
85
    }
86
87 1
    #[Override]
88
    public function getByUserAndFolder(
89
        int $userId,
90
        int $folderId,
91
        int $offset,
92
        int $limit
93
    ): array {
94 1
        return $this->findBy(
95 1
            ['recip_user' => $userId, 'cat_id' => $folderId, 'deleted' => null],
96 1
            ['date' => 'desc', 'id' => 'desc'],
97 1
            $limit,
98 1
            $offset
99 1
        );
100
    }
101
102 3
    #[Override]
103
    public function getAmountByFolder(PrivateMessageFolderInterface $privateMessageFolder): int
104
    {
105 3
        return $this->count([
106 3
            'category' => $privateMessageFolder,
107 3
            'deleted' => null
108 3
        ]);
109
    }
110
111 6
    #[Override]
112
    public function getNewAmountByFolder(PrivateMessageFolderInterface $privateMessageFolder): int
113
    {
114 6
        return $this->count([
115 6
            'category' => $privateMessageFolder,
116 6
            'new' => 1,
117 6
            'deleted' => null
118 6
        ]);
119
    }
120
121
    #[Override]
122
    public function setDeleteTimestampByFolder(int $folderId, int $timestamp): void
123
    {
124
        $this->getEntityManager()->createQuery(
125
            sprintf(
126
                'UPDATE %s pm SET pm.deleted = :timestamp WHERE pm.cat_id = :folderId',
127
                PrivateMessage::class
128
            )
129
        )->setParameters([
130
            'folderId' => $folderId,
131
            'timestamp' => $timestamp
132
        ])->execute();
133
    }
134
135 184
    #[Override]
136
    public function hasRecentMessage(UserInterface $user): bool
137
    {
138 184
        return $this->getEntityManager()->createQuery(
139 184
            sprintf(
140 184
                'SELECT count(pm.id)
141
                    FROM %s pm
142
                    WHERE pm.receivingUser = :user
143
                    AND pm.new = :true
144 184
                    AND pm.date > :threshold',
145 184
                PrivateMessage::class
146 184
            )
147 184
        )
148 184
            ->setParameters([
149 184
                'user' => $user,
150 184
                'threshold' => time() - GameComponentEnum::PM->getRefreshIntervalInSeconds(),
151 184
                'true' => true
152 184
            ])
153 184
            ->getSingleScalarResult() > 0;
154
    }
155
156
    #[Override]
157
    public function getConversations(UserInterface $user): array
158
    {
159
        return $this->getEntityManager()->createQuery(
160
            sprintf(
161
                'SELECT pm FROM %1$s pm
162
                JOIN %2$s pmf
163
                WITH pm.cat_id = pmf.id
164
                LEFT JOIN %1$s inbox
165
                WITH pm.inbox_pm_id = inbox.id
166
                LEFT JOIN %2$s pmfinbox
167
                WITH inbox.cat_id = pmfinbox.id
168
                WHERE pmf.special in (:main, :out)
169
                AND (pmfinbox.special is null or pmfinbox.special = :main)
170
                AND pm.receivingUser = :user
171
                AND pm.deleted IS NULL
172
                AND  NOT EXISTS (SELECT pm2.id FROM %1$s pm2
173
                                    WHERE pm2.send_user = pm.send_user
174
                                    AND pm2.cat_id = pm.cat_id 
175
                                    AND pm2.recip_user = pm.recip_user 
176
                                    AND pm2.date > pm.date)
177
                ORDER BY pm.id DESC',
178
                PrivateMessage::class,
179
                PrivateMessageFolder::class
180
            )
181
        )
182
            ->setParameters([
183
                'user' => $user,
184
                'main' => PrivateMessageFolderTypeEnum::SPECIAL_MAIN,
185
                'out' => PrivateMessageFolderTypeEnum::SPECIAL_PMOUT
186
            ])
187
            ->getResult();
188
    }
189
190
    #[Override]
191
    public function unsetAllInboxReferences(): void
192
    {
193
        $this->getEntityManager()->createQuery(
194
            sprintf(
195
                'UPDATE %s pm
196
                SET pm.inbox_pm_id = null',
197
                PrivateMessage::class
198
            )
199
        )->execute();
200
    }
201
202
    #[Override]
203
    public function truncateAllPrivateMessages(): void
204
    {
205
        $this->getEntityManager()->createQuery(
206
            sprintf(
207
                'DELETE FROM %s pm',
208
                PrivateMessage::class
209
            )
210
        )->execute();
211
    }
212
}
213