Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
SystemStatus
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 handleRequest
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3/**
4 * "Keep Alive" AJAX handler
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2023.
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
30namespace VuFind\AjaxHandler;
31
32use Laminas\Config\Config;
33use Laminas\Mvc\Controller\Plugin\Params;
34use Laminas\Session\SessionManager;
35use VuFind\Db\Service\SessionServiceInterface;
36use VuFind\Search\Results\PluginManager as ResultsManager;
37
38/**
39 * "Keep Alive" AJAX handler
40 *
41 * This is responsible for keeping the session alive whenever called
42 * (via JavaScript)
43 *
44 * @category VuFind
45 * @package  AJAX
46 * @author   Demian Katz <demian.katz@villanova.edu>
47 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
48 * @link     https://vufind.org/wiki/development Wiki
49 */
50class SystemStatus extends AbstractBase implements \Laminas\Log\LoggerAwareInterface
51{
52    use \VuFind\Log\LoggerAwareTrait;
53
54    /**
55     * Constructor
56     *
57     * @param SessionManager          $sessionManager Session manager
58     * @param ResultsManager          $resultsManager Results manager
59     * @param Config                  $config         Top-level VuFind configuration (config.ini)
60     * @param SessionServiceInterface $sessionService Session database service
61     */
62    public function __construct(
63        protected SessionManager $sessionManager,
64        protected ResultsManager $resultsManager,
65        protected Config $config,
66        protected SessionServiceInterface $sessionService
67    ) {
68    }
69
70    /**
71     * Handle a request.
72     *
73     * @param Params $params Parameter helper from controller
74     *
75     * @return array [response data, HTTP status code]
76     *
77     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
78     */
79    public function handleRequest(Params $params)
80    {
81        // Check system status
82        if (
83            !empty($this->config->System->healthCheckFile)
84            && file_exists($this->config->System->healthCheckFile)
85        ) {
86            return $this->formatResponse(
87                'Health check file exists',
88                self::STATUS_HTTP_UNAVAILABLE
89            );
90        }
91
92        // Test logging (note that the message doesn't need to get written for the log writers to initialize):
93        $this->log('info', 'SystemStatus log check', [], true);
94
95        // Test search index
96        try {
97            $results = $this->resultsManager->get('Solr');
98            $paramsObj = $results->getParams();
99            $paramsObj->setQueryIDs(['healthcheck']);
100            $results->performAndProcessSearch();
101        } catch (\Exception $e) {
102            return $this->formatResponse(
103                'Search index error: ' . $e->getMessage(),
104                self::STATUS_HTTP_ERROR
105            );
106        }
107
108        // Test database connection
109        try {
110            $this->sessionService->getSessionById('healthcheck', false);
111        } catch (\Exception $e) {
112            return $this->formatResponse(
113                'Database error: ' . $e->getMessage(),
114                self::STATUS_HTTP_ERROR
115            );
116        }
117
118        // This may be called frequently, don't leave sessions dangling
119        $this->sessionManager->destroy();
120
121        return $this->formatResponse('');
122    }
123}