Passed
Push — dev ( 8e4f3d...84d979 )
by Janko
29:51
created

getOrderedCorrepondence()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 20
nc 1
nop 4
dl 0
loc 29
ccs 0
cts 17
cp 0
crap 2
rs 9.6
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\Orm\Entity\PrivateMessage;
11
use Stu\Orm\Entity\PrivateMessageFolder;
12
use Stu\Orm\Entity\PrivateMessageFolderInterface;
13
use Stu\Orm\Entity\PrivateMessageInterface;
14
use Stu\Orm\Entity\UserInterface;
15
16
/**
17
 * @extends EntityRepository<PrivateMessage>
18
 */
19
final class PrivateMessageRepository extends EntityRepository implements PrivateMessageRepositoryInterface
20
{
21
    #[Override]
22
    public function prototype(): PrivateMessageInterface
23
    {
24
        return new PrivateMessage();
25
    }
26
27
    #[Override]
28
    public function save(PrivateMessageInterface $post, bool $doFlush = false): void
29
    {
30
        $em = $this->getEntityManager();
31
32
        $em->persist($post);
33
34
        if ($doFlush) {
35
            $em->flush();
36
        }
37
    }
38
39
    #[Override]
40
    public function getOrderedCorrepondence(
41
        int $senderUserId,
42
        int $recipientUserId,
43
        array $specialIds,
44
        int $limit
45
    ): array {
46
        return $this->getEntityManager()
47
            ->createQuery(
48
                sprintf(
49
                    'SELECT pm FROM %s pm
50
                    JOIN %s pmf
51
                    WITH pm.cat_id = pmf.id
52
                    WHERE ((pm.send_user = :sendUserId AND pm.recip_user = :recipUserId) OR
53
                        (pm.send_user = :recipUserId AND pm.recip_user = :sendUserId))
54
                    AND pmf.special in (:specialIds)
55
                    AND pm.deleted IS NULL
56
                    ORDER BY pm.date DESC',
57
                    PrivateMessage::class,
58
                    PrivateMessageFolder::class
59
                )
60
            )
61
            ->setParameters([
62
                'sendUserId' => $senderUserId,
63
                'recipUserId' => $recipientUserId,
64
                'specialIds' => $specialIds
65
            ])
66
            ->setMaxResults($limit)
67
            ->getResult();
68
    }
69
70
    #[Override]
71
    public function getBySender(UserInterface $user): array
72
    {
73
        return $this->findBy(
74
            ['send_user' => $user->getId()]
75
        );
76
    }
77
78
    #[Override]
79
    public function getByReceiver(UserInterface $user): array
80
    {
81
        return $this->findBy(
82
            ['recip_user' => $user->getId()]
83
        );
84
    }
85
86 1
    #[Override]
87
    public function getByUserAndFolder(
88
        int $userId,
89
        int $folderId,
90
        int $offset,
91
        int $limit
92
    ): array {
93 1
        return $this->findBy(
94 1
            ['recip_user' => $userId, 'cat_id' => $folderId, 'deleted' => null],
95 1
            ['date' => 'desc', 'id' => 'desc'],
96 1
            $limit,
97 1
            $offset
98 1
        );
99
    }
100
101 3
    #[Override]
102
    public function getAmountByFolder(PrivateMessageFolderInterface $privateMessageFolder): int
103
    {
104 3
        return $this->count([
105 3
            'category' => $privateMessageFolder,
106 3
            'deleted' => null
107 3
        ]);
108
    }
109
110 6
    #[Override]
111
    public function getNewAmountByFolder(PrivateMessageFolderInterface $privateMessageFolder): int
112
    {
113 6
        return $this->count([
114 6
            'category' => $privateMessageFolder,
115 6
            'new' => 1,
116 6
            'deleted' => null
117 6
        ]);
118
    }
119
120
    #[Override]
121
    public function setDeleteTimestampByFolder(int $folderId, int $timestamp): void
122
    {
123
        $this->getEntityManager()->createQuery(
124
            sprintf(
125
                'UPDATE %s pm SET pm.deleted = :timestamp WHERE pm.cat_id = :folderId',
126
                PrivateMessage::class
127
            )
128
        )->setParameters([
129
            'folderId' => $folderId,
130
            'timestamp' => $timestamp
131
        ])->execute();
132
    }
133
134 184
    #[Override]
135
    public function hasRecentMessage(UserInterface $user): bool
136
    {
137 184
        return $this->getEntityManager()->createQuery(
138 184
            sprintf(
139 184
                'SELECT count(pm.id)
140
                    FROM %s pm
141
                    WHERE pm.receivingUser = :user
142
                    AND pm.new = :true
143 184
                    AND pm.date > :threshold',
144 184
                PrivateMessage::class
145 184
            )
146 184
        )
147 184
            ->setParameters([
148 184
                'user' => $user,
149 184
                'threshold' => time() - GameComponentEnum::PM->getRefreshIntervalInSeconds(),
150 184
                'true' => true
151 184
            ])
152 184
            ->getSingleScalarResult() > 0;
153
    }
154
155
    #[Override]
156
    public function unsetAllInboxReferences(): void
157
    {
158
        $this->getEntityManager()->createQuery(
159
            sprintf(
160
                'UPDATE %s pm
161
                SET pm.inbox_pm_id = null',
162
                PrivateMessage::class
163
            )
164
        )->execute();
165
    }
166
167
    #[Override]
168
    public function truncateAllPrivateMessages(): void
169
    {
170
        $this->getEntityManager()->createQuery(
171
            sprintf(
172
                'DELETE FROM %s pm',
173
                PrivateMessage::class
174
            )
175
        )->execute();
176
    }
177
}
178