Passed
Push — develop ( 8cbecb...4958fb )
by BENARD
02:34
created

NotifyManager   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 35
dl 0
loc 62
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A notify() 0 38 3
1
<?php
2
3
namespace ProjetNormandie\ForumBundle\Service;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use ProjetNormandie\ForumBundle\Entity\Message;
7
use ProjetNormandie\MessageBundle\Service\Messager;
0 ignored issues
show
Bug introduced by
The type ProjetNormandie\MessageBundle\Service\Messager 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...
8
use Symfony\Contracts\Translation\TranslatorInterface;
9
10
class NotifyManager
11
{
12
    private EntityManagerInterface $em;
13
    private TranslatorInterface $translator;
14
    private Messager $messager;
15
16
    /**
17
     * MessageService constructor.
18
     * @param EntityManagerInterface $em
19
     * @param TranslatorInterface    $translator
20
     * @param Messager               $messager
21
     */
22
    public function __construct(EntityManagerInterface $em, TranslatorInterface $translator, Messager $messager)
23
    {
24
        $this->em = $em;
25
        $this->translator = $translator;
26
        $this->messager = $messager;
27
    }
28
29
30
    /**
31
     * @param Message $message
32
     * @param string  $type
33
     */
34
    public function notify(Message $message, string $type = 'new')
35
    {
36
        // Notify users
37
        $topicUsers = $this->em->getRepository('ProjetNormandie\ForumBundle\Entity\TopicUser')->findBy(
38
            array(
39
                'topic' => $message->getTopic(),
40
                'boolNotif' => 1
41
            )
42
        );
43
44
        foreach ($topicUsers as $topicUser) {
45
            $recipient = $topicUser->getUser();
46
            $url = '/' . $recipient->getLocale() . '/' . $message->getUrl();
47
            if ($topicUser->getUser()->getid() != $message->getUser()->getId()) {
48
                $this->messager->send(
49
                    sprintf(
50
                        $this->translator->trans(
51
                            'topic.notif.object.' . $type,
52
                            array(),
53
                            null,
54
                            $topicUser->getUser()->getLocale()
55
                        ),
56
                        $message->getTopic()->getLibTopic()
57
                    ),
58
                    sprintf(
59
                        $this->translator->trans(
60
                            'topic.notif.message',
61
                            array(),
62
                            null,
63
                            $topicUser->getUser()->getLocale()
64
                        ),
65
                        $message->getMessage(),
66
                        $url,
67
                        $message->getTopic()->getLibTopic()
68
                    ),
69
                    $message->getUser(),
70
                    $topicUser->getUser(),
71
                    'FORUM_NOTIF'
72
                );
73
            }
74
        }
75
    }
76
}
77