Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
mark_read
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 2
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 handle
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2/**
3 *
4 * This file is part of the phpBB Forum Software package.
5 *
6 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7 * @license GNU General Public License, version 2 (GPL-2.0)
8 *
9 * For full copyright and license information, please see
10 * the docs/CREDITS.txt file.
11 *
12 */
13
14namespace phpbb\notification\controller;
15
16use phpbb\event\dispatcher;
17use phpbb\exception\http_exception;
18use phpbb\language\language;
19use phpbb\notification\manager;
20use phpbb\notification\type\type_interface;
21use phpbb\request\request;
22use phpbb\user;
23use Symfony\Component\HttpFoundation\JsonResponse;
24use Symfony\Component\HttpFoundation\RedirectResponse;
25use Symfony\Component\HttpFoundation\Response;
26
27class mark_read
28{
29    /** @var dispatcher  */
30    protected $dispatcher;
31
32    /** @var language */
33    protected $language;
34
35    /** @var manager */
36    protected $manager;
37
38    /** @var request */
39    protected $request;
40
41    /** @var user */
42    protected $user;
43
44    /** @var string */
45    protected $phpbb_root_path;
46
47    /**
48     * Constructor
49     *
50     * @param dispatcher    $dispatcher        Event dispatcher
51     * @param language        $language        Language object
52     * @param manager        $manager        Notification manager
53     * @param request        $request        Request object
54     * @param user            $user            User object
55     * @param string        $phpbb_root_path phpBB root path
56     */
57    public function __construct(dispatcher $dispatcher, language $language, manager $manager, request $request, user $user, string $phpbb_root_path)
58    {
59        $this->dispatcher = $dispatcher;
60        $this->language = $language;
61        $this->manager = $manager;
62        $this->request = $request;
63        $this->user = $user;
64        $this->phpbb_root_path = $phpbb_root_path;
65    }
66
67    /**
68     * Mark notification as read
69     *
70     * @param int $id Notification ID
71     *
72     * @return Response
73     *
74     * @throws http_exception
75     *
76     * @psalm-suppress InvalidArgument
77     */
78    public function handle(int $id): Response
79    {
80        $mark_notification = $id;
81
82        if ($this->user->data['user_id'] == ANONYMOUS)
83        {
84            if ($this->request->is_ajax())
85            {
86                throw new http_exception(403, 'LOGIN_REQUIRED');
87            }
88            login_box('', $this->language->lang('LOGIN_REQUIRED'));
89        }
90
91        if (!check_link_hash($this->request->variable('hash', ''), 'mark_notification_read'))
92        {
93            // Link hash error
94            throw new http_exception(403, 'NOT_AUTHORISED');
95        }
96
97        $notification = $this->manager->load_notifications('notification.method.board', [
98            'notification_id'    => $mark_notification,
99        ]);
100
101        if (!isset($notification['notifications'][$mark_notification]))
102        {
103            // Notification id not found
104            throw new http_exception(404, 'PAGE_NOT_FOUND');
105        }
106
107        $notification = $notification['notifications'][$mark_notification];
108
109        $notification->mark_read();
110
111        /**
112         * You can use this event to perform additional tasks or redirect user elsewhere.
113         *
114         * @event core.index_mark_notification_after
115         * @var        int                mark_notification    Notification ID
116         * @var        type_interface    notification        Notification instance
117         * @since 3.2.6-RC1
118         */
119        $vars = ['mark_notification', 'notification'];
120        extract($this->dispatcher->trigger_event('core.index_mark_notification_after', compact($vars)));
121
122        if ($this->request->is_ajax())
123        {
124            $data = [
125                'success'    => true,
126            ];
127            return new JsonResponse($data);
128        }
129
130        if (($redirect = $this->request->variable('redirect', '')))
131        {
132            return new RedirectResponse(append_sid($this->phpbb_root_path . $redirect));
133        }
134
135        return new RedirectResponse($notification->get_redirect_url());
136    }
137}