Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
17 / 17 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
DefaultParametersListener | |
100.00% |
17 / 17 |
|
100.00% |
3 / 3 |
8 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
attach | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
onSearchPre | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
6 |
1 | <?php |
2 | |
3 | /** |
4 | * Solr default parameters listener. |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) The National Library of Finland 2021. |
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 Search |
25 | * @author Ere Maijala <ere.maijala@helsinki.fi> |
26 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
27 | * @link https://vufind.org Main Site |
28 | */ |
29 | |
30 | namespace VuFind\Search\Solr; |
31 | |
32 | use Laminas\EventManager\EventInterface; |
33 | use Laminas\EventManager\SharedEventManagerInterface; |
34 | use VuFindSearch\Backend\Solr\Backend; |
35 | |
36 | /** |
37 | * Solr default parameters listener. |
38 | * |
39 | * Allows injecting of default parameters depending on request type. |
40 | * |
41 | * @category VuFind |
42 | * @package Search |
43 | * @author Ere Maijala <ere.maijala@helsinki.fi> |
44 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
45 | * @link https://vufind.org Main Site |
46 | */ |
47 | class DefaultParametersListener |
48 | { |
49 | /** |
50 | * Backend. |
51 | * |
52 | * @var Backend |
53 | */ |
54 | protected $backend; |
55 | |
56 | /** |
57 | * Default parameters |
58 | * |
59 | * @var array |
60 | */ |
61 | protected $defaultParams; |
62 | |
63 | /** |
64 | * Mapping from search methods to contexts |
65 | * |
66 | * @var array |
67 | */ |
68 | protected $contextMap = [ |
69 | 'getIds' => 'search', |
70 | 'random' => 'retrieve', |
71 | 'retrieveBatch' => 'retrieve', |
72 | ]; |
73 | |
74 | /** |
75 | * Constructor. |
76 | * |
77 | * @param Backend $backend Search backend |
78 | * @param array $params Default parameters |
79 | * |
80 | * @return void |
81 | */ |
82 | public function __construct(Backend $backend, array $params) |
83 | { |
84 | $this->backend = $backend; |
85 | $this->defaultParams = $params; |
86 | } |
87 | |
88 | /** |
89 | * Attach listener to shared event manager. |
90 | * |
91 | * @param SharedEventManagerInterface $manager Shared event manager |
92 | * |
93 | * @return void |
94 | */ |
95 | public function attach( |
96 | SharedEventManagerInterface $manager |
97 | ) { |
98 | $manager->attach(\VuFindSearch\Service::class, 'pre', [$this, 'onSearchPre']); |
99 | } |
100 | |
101 | /** |
102 | * Add default parameters |
103 | * |
104 | * @param EventInterface $event Event |
105 | * |
106 | * @return EventInterface |
107 | */ |
108 | public function onSearchPre(EventInterface $event) |
109 | { |
110 | $backend = $event->getTarget(); |
111 | if ($backend === $this->backend) { |
112 | $context = $event->getParam('context'); |
113 | $context = $this->contextMap[$context] ?? $context; |
114 | $defaultParams = $this->defaultParams[$context] |
115 | ?? $this->defaultParams['*'] |
116 | ?? ''; |
117 | if ($defaultParams && $params = $event->getParam('params')) { |
118 | foreach (explode('&', $defaultParams) as $keyVal) { |
119 | $parts = explode('=', $keyVal, 2); |
120 | if (!isset($parts[1])) { |
121 | continue; |
122 | } |
123 | $params->add(urldecode($parts[0]), urldecode($parts[1])); |
124 | } |
125 | } |
126 | } |
127 | return $event; |
128 | } |
129 | } |