Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.71% covered (warning)
85.71%
6 / 7
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbstractBase
85.71% covered (warning)
85.71%
6 / 7
50.00% covered (danger)
50.00%
1 / 2
4.05
0.00% covered (danger)
0.00%
0 / 1
 disableSessionWrites
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 formatResponse
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * Abstract base AJAX handler
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
30namespace VuFind\AjaxHandler;
31
32use VuFind\Session\Settings as SessionSettings;
33
34/**
35 * Abstract base AJAX handler
36 *
37 * @category VuFind
38 * @package  AJAX
39 * @author   Demian Katz <demian.katz@villanova.edu>
40 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
41 * @link     https://vufind.org/wiki/development Wiki
42 */
43abstract class AbstractBase implements AjaxHandlerInterface
44{
45    /**
46     * Session settings
47     *
48     * @var SessionSettings
49     */
50    protected $sessionSettings = null;
51
52    /**
53     * Prevent session writes -- this is designed to be called prior to time-
54     * consuming AJAX operations to help reduce the odds of a timing-related bug
55     * that causes the wrong version of session data to be written to disk (see
56     * VUFIND-716 for more details).
57     *
58     * @return void
59     */
60    protected function disableSessionWrites()
61    {
62        if (null === $this->sessionSettings) {
63            throw new \Exception('Session settings object missing.');
64        }
65        $this->sessionSettings->disableWrite();
66    }
67
68    /**
69     * Format a response array.
70     *
71     * @param mixed $response Response data
72     * @param int   $httpCode HTTP status code (omit for default)
73     *
74     * @return array
75     */
76    protected function formatResponse($response, $httpCode = null)
77    {
78        $arr = [$response];
79        if ($httpCode !== null) {
80            $arr[] = $httpCode;
81        }
82        return $arr;
83    }
84}