NotificationManager   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 36.36%

Importance

Changes 0
Metric Value
dl 0
loc 45
ccs 8
cts 22
cp 0.3636
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getUserUnreadNotifications() 0 7 1
A read() 0 3 1
A notifyAllMembers() 0 3 1
A __construct() 0 4 1
A notify() 0 12 2
1
<?php
2
3
namespace App\Manager\User;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Symfony\Component\Translation\TranslatorInterface;
7
8
use App\Entity\User\Notification;
9
10
use App\Entity\User\Member;
11
use App\Entity\User\User;
12
13
class NotificationManager
14
{
15
    /** @var EntityManagerInterface **/
16
    protected $em;
17
    /** @var TranslatorInterface **/
18
    protected $translator;
19
    
20 15
    public function __construct(EntityManagerInterface $em, TranslatorInterface $translator)
21
    {
22 15
        $this->em = $em;
23 15
        $this->translator = $translator;
24 15
    }
25
    
26 6
    public function getUserUnreadNotifications(User $user): array
27
    {
28 6
        return $this->em->getRepository(Notification::class)->findBy([
29 6
            'user' => $user,
30
            'readAt' => null
31
        ], [
32 6
            'createdAt' => 'DESC'
33
        ]);
34
    }
35
    
36
    public function read(array $ids = [])
37
    {
38
        $this->em->getRepository(Notification::class)->read($ids);
39
    }
40
41
    public function notifyAllMembers(string $content, array $parameters = [])
42
    {
43
        $this->notify($this->em->getRepository(Member::class)->findAll(), $content, $parameters);
44
    }
45
    
46
    protected function notify(array $users, string $content, array $parameters = [])
47
    {
48
        $notification =
49
            (new Notification())
50
            ->setContent($this->translator->trans($content, $parameters))
51
        ;
52
        foreach ($users as $user) {
53
            $n = clone $notification;
54
            $n->setUser($user);
55
            $this->em->persist($n);
56
        }
57
        $this->em->flush();
58
    }
59
}