Passed
Pull Request — master (#1801)
by Nico
23:50
created

PrivateMessageRepository   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 60
dl 0
loc 125
ccs 0
cts 75
cp 0
rs 10
c 0
b 0
f 0
wmc 12

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getNewAmountByFolder() 0 6 1
A getAmountByFolder() 0 5 1
A getByUserAndFolder() 0 11 1
A setDeleteTimestampByFolder() 0 11 1
A save() 0 8 2
A prototype() 0 3 1
A getOrderedCorrepondence() 0 28 1
A getByReceiver() 0 4 1
A getBySender() 0 4 1
A truncateAllPrivateMessages() 0 8 1
A unsetAllInboxReferences() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Orm\Repository;
6
7
use Doctrine\ORM\EntityRepository;
8
use Stu\Orm\Entity\PrivateMessage;
9
use Stu\Orm\Entity\PrivateMessageFolder;
10
use Stu\Orm\Entity\PrivateMessageFolderInterface;
11
use Stu\Orm\Entity\PrivateMessageInterface;
12
use Stu\Orm\Entity\UserInterface;
13
14
/**
15
 * @extends EntityRepository<PrivateMessage>
16
 */
17
final class PrivateMessageRepository extends EntityRepository implements PrivateMessageRepositoryInterface
18
{
19
    public function prototype(): PrivateMessageInterface
20
    {
21
        return new PrivateMessage();
22
    }
23
24
    public function save(PrivateMessageInterface $post, bool $doFlush = false): void
25
    {
26
        $em = $this->getEntityManager();
27
28
        $em->persist($post);
29
30
        if ($doFlush) {
31
            $em->flush();
32
        }
33
    }
34
35
    public function getOrderedCorrepondence(
36
        int $senderUserId,
37
        int $recipientUserId,
38
        array $specialIds,
39
        int $limit
40
    ): array {
41
        return $this->getEntityManager()
42
            ->createQuery(
43
                sprintf(
44
                    'SELECT pm FROM %s pm
45
                    JOIN %s pmf
46
                    WITH pm.cat_id = pmf.id
47
                    WHERE ((pm.send_user = :sendUserId AND pm.recip_user = :recipUserId) OR
48
                        (pm.send_user = :recipUserId AND pm.recip_user = :sendUserId))
49
                    AND pmf.special in (:specialIds)
50
                    AND pm.deleted IS NULL
51
                    ORDER BY pm.date DESC',
52
                    PrivateMessage::class,
53
                    PrivateMessageFolder::class
54
                )
55
            )
56
            ->setParameters([
57
                'sendUserId' => $senderUserId,
58
                'recipUserId' => $recipientUserId,
59
                'specialIds' => $specialIds
60
            ])
61
            ->setMaxResults($limit)
62
            ->getResult();
63
    }
64
65
    public function getBySender(UserInterface $user): array
66
    {
67
        return $this->findBy(
68
            ['send_user' => $user->getId()]
69
        );
70
    }
71
72
    public function getByReceiver(UserInterface $user): array
73
    {
74
        return $this->findBy(
75
            ['recip_user' => $user->getId()]
76
        );
77
    }
78
79
    public function getByUserAndFolder(
80
        int $userId,
81
        int $folderId,
82
        int $offset,
83
        int $limit
84
    ): array {
85
        return $this->findBy(
86
            ['recip_user' => $userId, 'cat_id' => $folderId, 'deleted' => null],
87
            ['date' => 'desc', 'id' => 'desc'],
88
            $limit,
89
            $offset
90
        );
91
    }
92
93
    public function getAmountByFolder(PrivateMessageFolderInterface $privateMessageFolder): int
94
    {
95
        return $this->count([
96
            'category' => $privateMessageFolder,
97
            'deleted' => null
98
        ]);
99
    }
100
101
    public function getNewAmountByFolder(PrivateMessageFolderInterface $privateMessageFolder): int
102
    {
103
        return $this->count([
104
            'category' => $privateMessageFolder,
105
            'new' => 1,
106
            'deleted' => null
107
        ]);
108
    }
109
110
    public function setDeleteTimestampByFolder(int $folderId, int $timestamp): void
111
    {
112
        $this->getEntityManager()->createQuery(
113
            sprintf(
114
                'UPDATE %s pm SET pm.deleted = :timestamp WHERE pm.cat_id = :folderId',
115
                PrivateMessage::class
116
            )
117
        )->setParameters([
118
            'folderId' => $folderId,
119
            'timestamp' => $timestamp
120
        ])->execute();
121
    }
122
123
    public function unsetAllInboxReferences(): void
124
    {
125
        $this->getEntityManager()->createQuery(
126
            sprintf(
127
                'UPDATE %s pm
128
                SET pm.inbox_pm_id = null',
129
                PrivateMessage::class
130
            )
131
        )->execute();
132
    }
133
134
    public function truncateAllPrivateMessages(): void
135
    {
136
        $this->getEntityManager()->createQuery(
137
            sprintf(
138
                'DELETE FROM %s pm',
139
                PrivateMessage::class
140
            )
141
        )->execute();
142
    }
143
}
144