Passed
Pull Request — master (#45)
by Paweł
10:26
created

ApiNotificationsController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
eloc 32
c 1
b 0
f 1
dl 0
loc 66
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A markAsRead() 0 19 2
A list() 0 11 1
A __construct() 0 7 1
A getNumberOfUnreadNotifications() 0 17 4
1
<?php
2
3
namespace App\Controller;
4
5
use App\Model\UserInterface;
6
use App\Repository\NotificationRepositoryInterface;
7
use Doctrine\ORM\EntityManagerInterface;
8
use SWP\Component\Common\Exception\NotFoundHttpException;
9
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\Serializer\SerializerInterface;
12
13
class ApiNotificationsController extends AbstractController
14
{
15
    protected SerializerInterface $serializer;
16
17
    private NotificationRepositoryInterface $notificationRepository;
18
19
    public function __construct(
20
        SerializerInterface $serializer,
21
        NotificationRepositoryInterface $notificationRepository
22
    )
23
    {
24
        $this->serializer = $serializer;
25
        $this->notificationRepository = $notificationRepository;
26
    }
27
28
    public function list(): Response
29
    {
30
        $notifications = $this->notificationRepository->findAll();
0 ignored issues
show
Bug introduced by
The method findAll() does not exist on App\Repository\NotificationRepositoryInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to App\Repository\NotificationRepositoryInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
        /** @scrutinizer ignore-call */ 
31
        $notifications = $this->notificationRepository->findAll();
Loading history...
31
        /** @var UserInterface $user */
32
        $user = $this->getUser();
33
34
        return new Response($this->serializer->serialize(
35
            [
36
                'notifications' => $notifications,
37
                'unread' => $this->getNumberOfUnreadNotifications($notifications, $user)
38
            ], 'json', ['groups' => ['list']]));
39
    }
40
41
    public function markAsRead(string $uuid, EntityManagerInterface $entityManager): Response
42
    {
43
        $notification = $this->notificationRepository->findOneBy(['id' =>$uuid]);
0 ignored issues
show
Bug introduced by
The method findOneBy() does not exist on App\Repository\NotificationRepositoryInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to App\Repository\NotificationRepositoryInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
        /** @scrutinizer ignore-call */ 
44
        $notification = $this->notificationRepository->findOneBy(['id' =>$uuid]);
Loading history...
44
        if (null === $notification) {
45
            throw new NotFoundHttpException('Notification was not found');
46
        }
47
48
        /** @var UserInterface $user */
49
        $user = $this->getUser();
50
        $user->addNotification($notification);
51
        $entityManager->flush();
52
53
        $notifications = $this->notificationRepository->findAll();
54
55
        return new Response($this->serializer->serialize(
56
            [
57
                'notifications' => $notifications,
58
                'unread' => $this->getNumberOfUnreadNotifications($notifications, $user)
59
            ], 'json', ['groups' => ['list']]), 201);
60
    }
61
62
    private function getNumberOfUnreadNotifications(array $notifications, UserInterface $user): int
63
    {
64
        $readByUserNotifications = \array_map(function ($notification) {
65
            return $notification->getId();
66
        }, $user->getNotifications()->toArray());
67
68
        if (0 === count($readByUserNotifications)) {
69
            return count($notifications);
70
        }
71
72
        foreach ($notifications as $key => $notification) {
73
            if (in_array($notification->getId(), $readByUserNotifications)) {
74
                unset($notifications[$key]);
75
            }
76
        }
77
78
        return count($notifications);
79
    }
80
}
81