Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.75% covered (success)
93.75%
15 / 16
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
mention
93.75% covered (success)
93.75%
15 / 16
50.00% covered (danger)
50.00%
1 / 2
5.01
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 handle
91.67% covered (success)
91.67%
11 / 12
0.00% covered (danger)
0.00%
0 / 1
4.01
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\mention\controller;
15
16use phpbb\di\service_collection;
17use phpbb\request\request_interface;
18use Symfony\Component\HttpFoundation\JsonResponse;
19use Symfony\Component\HttpFoundation\RedirectResponse;
20
21class mention
22{
23    /** @var service_collection */
24    protected $mention_sources;
25
26    /** @var  request_interface */
27    protected $request;
28
29    /** @var string */
30    protected $phpbb_root_path;
31
32    /** @var string */
33    protected $php_ext;
34
35    /**
36     * Constructor
37     *
38     * @param service_collection|array $mention_sources
39     * @param request_interface $request
40     * @param string $phpbb_root_path
41     * @param string $phpEx
42     */
43    public function __construct($mention_sources, request_interface $request, string $phpbb_root_path, string $phpEx)
44    {
45        $this->mention_sources = $mention_sources;
46        $this->request = $request;
47        $this->phpbb_root_path = $phpbb_root_path;
48        $this->php_ext = $phpEx;
49    }
50
51    /**
52     * Handle requests to mention controller
53     *
54     * @return JsonResponse|RedirectResponse
55     */
56    public function handle()
57    {
58        if (!$this->request->is_ajax())
59        {
60            return new RedirectResponse(append_sid($this->phpbb_root_path . 'index.' . $this->php_ext));
61        }
62
63        $keyword = $this->request->variable('keyword', '', true);
64        $topic_id = $this->request->variable('topic_id', 0);
65        $names = [];
66        $has_names_remaining = false;
67
68        foreach ($this->mention_sources as $source)
69        {
70            $has_names_remaining = !$source->get($names, $keyword, $topic_id) || $has_names_remaining;
71        }
72
73        return new JsonResponse([
74            'names' => array_values($names),
75            'all' => !$has_names_remaining,
76        ]);
77    }
78}