1 | <?php |
||||||
2 | |||||||
3 | namespace App\Controller; |
||||||
4 | |||||||
5 | use App\Model\NotificationInterface; |
||||||
6 | use App\Model\UserInterface; |
||||||
7 | use App\Repository\NotificationRepositoryInterface; |
||||||
8 | use Doctrine\ORM\EntityManagerInterface; |
||||||
9 | use SWP\Component\Common\Exception\NotFoundHttpException; |
||||||
10 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
||||||
11 | use Symfony\Component\HttpFoundation\Response; |
||||||
12 | use Symfony\Component\Serializer\SerializerInterface; |
||||||
13 | |||||||
14 | class ApiNotificationsController extends AbstractController |
||||||
15 | { |
||||||
16 | protected SerializerInterface $serializer; |
||||||
17 | |||||||
18 | private NotificationRepositoryInterface $notificationRepository; |
||||||
19 | |||||||
20 | public function __construct( |
||||||
21 | SerializerInterface $serializer, |
||||||
22 | NotificationRepositoryInterface $notificationRepository |
||||||
23 | ) |
||||||
24 | { |
||||||
25 | $this->serializer = $serializer; |
||||||
26 | $this->notificationRepository = $notificationRepository; |
||||||
27 | } |
||||||
28 | |||||||
29 | public function list(): Response |
||||||
30 | { |
||||||
31 | $notifications = $this->notificationRepository->findAll(); |
||||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
32 | /** @var UserInterface $user */ |
||||||
33 | $user = $this->getUser(); |
||||||
34 | |||||||
35 | return new Response($this->serializer->serialize( |
||||||
36 | $this->getProcessedNotifications($notifications, $user), |
||||||
37 | 'json', |
||||||
38 | ['groups' => ['list']]) |
||||||
39 | ); |
||||||
40 | } |
||||||
41 | |||||||
42 | public function markAsRead(string $uuid, EntityManagerInterface $entityManager): Response |
||||||
43 | { |
||||||
44 | $notification = $this->notificationRepository->findOneBy(['id' =>$uuid]); |
||||||
0 ignored issues
–
show
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
![]() |
|||||||
45 | if (null === $notification) { |
||||||
46 | throw new NotFoundHttpException('Notification was not found'); |
||||||
47 | } |
||||||
48 | |||||||
49 | /** @var UserInterface $user */ |
||||||
50 | $user = $this->getUser(); |
||||||
51 | $user->addNotification($notification); |
||||||
52 | $entityManager->flush(); |
||||||
53 | |||||||
54 | $notifications = $this->notificationRepository->findAll(); |
||||||
55 | |||||||
56 | return new Response($this->serializer->serialize( |
||||||
57 | $this->getProcessedNotifications($notifications, $user), |
||||||
58 | 'json', |
||||||
59 | ['groups' => ['list']] |
||||||
60 | ), 201); |
||||||
61 | } |
||||||
62 | |||||||
63 | |||||||
64 | private function getProcessedNotifications(array $notifications, UserInterface $user): array |
||||||
65 | { |
||||||
66 | $readByUserNotifications = \array_map(function ($notification) { |
||||||
67 | return $notification->getId(); |
||||||
68 | }, $user->getNotifications()->toArray()); |
||||||
69 | |||||||
70 | $unreadNotification = count($notifications); |
||||||
71 | if (0 === $unreadNotification) { |
||||||
72 | return [ |
||||||
73 | 'notifications' => $notifications, |
||||||
74 | 'unread' => $unreadNotification, |
||||||
75 | ]; |
||||||
76 | } |
||||||
77 | |||||||
78 | |||||||
79 | /** @var NotificationInterface $notification */ |
||||||
80 | foreach ($notifications as $notification) { |
||||||
81 | if (in_array($notification->getId(), $readByUserNotifications)) { |
||||||
82 | $notification->setRead(true); |
||||||
83 | $unreadNotification--; |
||||||
84 | } |
||||||
85 | } |
||||||
86 | |||||||
87 | return [ |
||||||
88 | 'notifications' => $notifications, |
||||||
89 | 'unread' => $unreadNotification, |
||||||
90 | ]; |
||||||
91 | } |
||||||
92 | } |
||||||
93 |