NotificationManager::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
}