Passed
Push — develop ( 743c4e...d8f86b )
by BENARD
08:44
created

MarkAsReadService   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 36
dl 0
loc 67
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A readForum() 0 25 2
A readAlL() 0 17 1
1
<?php
2
3
namespace ProjetNormandie\ForumBundle\Service;
4
5
use Doctrine\DBAL\Exception;
6
use Doctrine\ORM\EntityManagerInterface;
7
use ProjetNormandie\ForumBundle\Entity\Forum;
8
use Symfony\Component\Security\Core\Security;
9
10
class MarkAsReadService
11
{
12
    private Security $security;
13
    private EntityManagerInterface $em;
14
15
    /**
16
     * @param Security      $security
17
     * @param EntityManagerInterface $em
18
     */
19
    public function __construct(Security $security, EntityManagerInterface $em)
20
    {
21
        $this->security = $security;
22
        $this->em = $em;
23
    }
24
25
26
    /**
27
     * @return void
28
     */
29
    public function readAlL()
30
    {
31
        $user = $this->security->getUser();
32
33
        $query = $this->em->createQueryBuilder()
34
            ->update('ProjetNormandie\ForumBundle\Entity\ForumUser', 'fu')
35
            ->set('fu.boolRead', true)
36
            ->where('fu.user = :user')
37
            ->setParameter('user', $user);
38
        $query->getQuery()->getResult();
39
40
        $query = $this->em->createQueryBuilder()
41
            ->update('ProjetNormandie\ForumBundle\Entity\TopicUser', 'tu')
42
            ->set('tu.boolRead', true)
43
            ->where('tu.user = :user')
44
            ->setParameter('user', $user);
45
        $query->getQuery()->getResult();
46
    }
47
48
    /**
49
     * @param Forum $forum
50
     * @return void
51
     */
52
    public function readForum(Forum $forum)
53
    {
54
        $user = $this->security->getUser();
55
56
        $query = $this->em->createQueryBuilder()
57
            ->update('ProjetNormandie\ForumBundle\Entity\TopicUser', 'tu')
58
            ->set('tu.boolRead', true)
59
            ->where('tu.user = :user')
60
            ->setParameter('user', $user)
61
            ->andWhere('tu.topic IN (SELECT t FROM ProjetNormandie\ForumBundle\Entity\Topic t WHERE t.forum = :forum)')
62
            ->setParameter('forum', $forum);
63
        $query->getQuery()->getResult();
64
65
66
        $query = $this->em->createQueryBuilder()
67
            ->update('ProjetNormandie\ForumBundle\Entity\ForumUser', 'fu')
68
            ->set('fu.boolRead', true)
69
            ->where('fu.user = :user')
70
            ->setParameter('user', $user)
71
            ->andWhere('fu.forum = :forum')
72
            ->setParameter('forum', $forum);
73
        $query->getQuery()->getResult();
74
75
        //@todo forum parent
76
        if (null !== $forum->getParent()) {
77
78
        }
79
    }
80
}
81