Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
78.95% |
15 / 19 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
Recommend | |
78.95% |
15 / 19 |
|
50.00% |
1 / 2 |
3.08 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
handleRequest | |
73.33% |
11 / 15 |
|
0.00% |
0 / 1 |
2.08 |
1 | <?php |
2 | |
3 | /** |
4 | * Load a recommendation module via AJAX. |
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 AJAX |
25 | * @author Demian Katz <demian.katz@villanova.edu> |
26 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
27 | * @link https://vufind.org/wiki/development Wiki |
28 | */ |
29 | |
30 | namespace VuFind\AjaxHandler; |
31 | |
32 | use Laminas\Mvc\Controller\Plugin\Params; |
33 | use Laminas\Stdlib\Parameters; |
34 | use Laminas\View\Renderer\RendererInterface; |
35 | use VuFind\I18n\Translator\TranslatorAwareInterface; |
36 | use VuFind\Recommend\PluginManager as RecommendManager; |
37 | use VuFind\Search\Solr\Results; |
38 | use VuFind\Session\Settings as SessionSettings; |
39 | |
40 | /** |
41 | * Load a recommendation module via AJAX. |
42 | * |
43 | * @category VuFind |
44 | * @package AJAX |
45 | * @author Demian Katz <demian.katz@villanova.edu> |
46 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
47 | * @link https://vufind.org/wiki/development Wiki |
48 | */ |
49 | class Recommend extends AbstractBase implements TranslatorAwareInterface |
50 | { |
51 | use \VuFind\I18n\Translator\TranslatorAwareTrait; |
52 | |
53 | /** |
54 | * Recommendation plugin manager |
55 | * |
56 | * @var RecommendManager |
57 | */ |
58 | protected $pluginManager; |
59 | |
60 | /** |
61 | * Solr search results object |
62 | * |
63 | * @var Results |
64 | */ |
65 | protected $results; |
66 | |
67 | /** |
68 | * View renderer |
69 | * |
70 | * @var RendererInterface |
71 | */ |
72 | protected $renderer; |
73 | |
74 | /** |
75 | * Constructor |
76 | * |
77 | * @param SessionSettings $ss Session settings |
78 | * @param RecommendManager $pm Recommendation plugin manager |
79 | * @param Results $results Solr results object |
80 | * @param RendererInterface $renderer View renderer |
81 | */ |
82 | public function __construct( |
83 | SessionSettings $ss, |
84 | RecommendManager $pm, |
85 | Results $results, |
86 | RendererInterface $renderer |
87 | ) { |
88 | $this->sessionSettings = $ss; |
89 | $this->pluginManager = $pm; |
90 | $this->results = $results; |
91 | $this->renderer = $renderer; |
92 | } |
93 | |
94 | /** |
95 | * Handle a request. |
96 | * |
97 | * @param Params $params Parameter helper from controller |
98 | * |
99 | * @return array [response data, HTTP status code] |
100 | */ |
101 | public function handleRequest(Params $params) |
102 | { |
103 | $this->disableSessionWrites(); // avoid session write timing bug |
104 | // Process recommendations -- for now, we assume Solr-based search objects, |
105 | // since deferred recommendations work best for modules that don't care about |
106 | // the details of the search objects anyway: |
107 | if (!($moduleName = $params->fromQuery('mod'))) { |
108 | return $this->formatResponse( |
109 | $this->translate('bulk_error_missing'), |
110 | self::STATUS_HTTP_BAD_REQUEST |
111 | ); |
112 | } |
113 | $module = $this->pluginManager->get($moduleName); |
114 | $module->setConfig($params->fromQuery('params', '')); |
115 | $paramsObj = $this->results->getParams(); |
116 | $request = new Parameters($params->fromQuery()); |
117 | // Initialize search parameters from Ajax request parameters in case the |
118 | // original request parameters were passed to the Ajax request. |
119 | $paramsObj->initFromRequest($request); |
120 | $module->init($paramsObj, $request); |
121 | $module->process($this->results); |
122 | |
123 | // Render recommendations: |
124 | $recommend = $this->renderer->plugin('recommend'); |
125 | return $this->formatResponse($recommend($module)); |
126 | } |
127 | } |