Passed
Push — develop ( 57a0ca...bea66a )
by BENARD
09:14
created

MessageRepository   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 31
c 1
b 0
f 0
dl 0
loc 59
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getRecipients() 0 12 1
A getSenders() 0 12 1
A purge() 0 18 1
1
<?php
2
3
namespace ProjetNormandie\MessageBundle\Repository;
4
5
use DateInterval;
6
use DateTime;
7
use Doctrine\ORM\EntityRepository;
8
9
class MessageRepository extends EntityRepository
10
{
11
    /**
12
     *
13
     */
14
    public function purge(): void
15
    {
16
        //----- delete 1
17
        $query = $this->_em->createQuery(
18
            'DELETE projetNormandie\MessageBundle\Entity\Message m 
19
            WHERE m.isDeletedSender = :isDeletedSender
20
            AND m.isDeletedRecipient = :isDeletedRecipient'
21
        );
22
        $query->setParameter('isDeletedSender', true);
23
        $query->setParameter('isDeletedRecipient', true);
24
        $query->execute();
25
26
        //----- delete 2
27
        $date = new DateTime();
28
        $date = $date->sub(DateInterval::createFromDateString('2 years'));
29
        $query = $this->_em->createQuery('DELETE projetNormandie\MessageBundle\Entity\Message m WHERE m.createdAt < :date');
30
        $query->setParameter('date', $date->format('Y-m-d'));
31
        $query->execute();
32
    }
33
34
    /**
35
     * @param $user
36
     * @return mixed
37
     */
38
    public function getRecipients($user)
39
    {
40
        $query = $this->createQueryBuilder('m')
41
            ->join('m.recipient', 'u')
42
            ->select('DISTINCT u.id,u.username')
43
            ->where('m.sender = :user')
44
            ->setParameter('user', $user)
45
            ->andWhere('m.isDeletedSender = :isDeletedSender')
46
            ->setParameter('isDeletedSender', false)
47
            ->orderBy("u.username", 'ASC');
48
49
        return $query->getQuery()->getResult();
50
    }
51
52
    /**
53
     * @param $user
54
     * @return mixed
55
     */
56
    public function getSenders($user)
57
    {
58
        $query = $this->createQueryBuilder('m')
59
            ->join('m.sender', 'u')
60
            ->select('DISTINCT u.id,u.username')
61
            ->where('m.recipient = :user')
62
            ->setParameter('user', $user)
63
            ->andWhere('m.isDeletedRecipient = :isDeletedRecipient')
64
            ->setParameter('isDeletedRecipient', false)
65
            ->orderBy("u.username", 'ASC');
66
67
        return $query->getQuery()->getResult();
68
    }
69
}
70