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