Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.91% covered (success)
90.91%
10 / 11
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Eds
90.91% covered (success)
90.91%
10 / 11
66.67% covered (warning)
66.67%
2 / 3
6.03
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSuggestions
88.89% covered (warning)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
3.01
 setConfig
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * EDS Autocomplete Module
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2018.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  Autocomplete
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @author   Chris Hallberg <challber@villanova.edu>
27 * @author   Jochen Lienhard <jochen.lienhard@ub.uni-freiburg.de>
28 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
29 * @link     https://vufind.org/wiki/development:plugins:autosuggesters Wiki
30 */
31
32namespace VuFind\Autocomplete;
33
34use VuFindSearch\Service;
35
36use function is_array;
37
38/**
39 * EDS Autocomplete Module
40 *
41 * This class provides popular terms provided by EDS.
42 *
43 * @category VuFind
44 * @package  Autocomplete
45 * @author   Demian Katz <demian.katz@villanova.edu>
46 * @author   Jochen Lienhard <jochen.lienhard@ub.uni-freiburg.de>
47 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
48 * @link     https://vufind.org/wiki/development:plugins:autosuggesters Wiki
49 */
50class Eds implements AutocompleteInterface
51{
52    /**
53     * Eds domain
54     *
55     * @var string
56     */
57    protected $domain = 'rawqueries';
58
59    /**
60     * Search object family to use
61     *
62     * @var string
63     */
64    protected $searchClassId = 'EDS';
65
66    /**
67     * Search service
68     *
69     * @var Service
70     */
71    protected $searchService;
72
73    /**
74     * Constructor
75     *
76     * @param Service $ss Search service
77     */
78    public function __construct(Service $ss)
79    {
80        $this->searchService = $ss;
81    }
82
83    /**
84     * This method returns an array of strings matching the user's query for
85     * display in the autocomplete box.
86     *
87     * @param string $query The user query
88     *
89     * @return array        The suggestions for the provided query
90     */
91    public function getSuggestions($query)
92    {
93        $results = null;
94        try {
95            // Perform the autocomplete search:
96            $command = new \VuFindSearch\Backend\EDS\Command\AutocompleteCommand(
97                $this->searchClassId,
98                $query,
99                $this->domain
100            );
101            $results = $this->searchService->invoke($command)->getResult();
102        } catch (\Exception $e) {
103            // Ignore errors -- just return empty results if we must.
104        }
105        return is_array($results) ? array_unique($results) : [];
106    }
107
108    /**
109     * Set parameters that affect the behavior of the autocomplete handler.
110     * These values normally come from the EDS configuration file.
111     *
112     * @param string $params Parameters to set
113     *
114     * @return void
115     */
116    public function setConfig($params)
117    {
118        // Only change the value if it is not empty:
119        $this->domain = !empty($params) ? $params : $this->domain;
120    }
121}