Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
Followup
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
5 / 5
9
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
 clear
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 retrieve
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 retrieveAndClear
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 store
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * VuFind Action Helper - Followup
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010.
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  Controller_Plugins
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 Main Page
28 */
29
30namespace VuFind\Controller\Plugin;
31
32use Laminas\Mvc\Controller\Plugin\AbstractPlugin;
33use Laminas\Session\Container;
34use Laminas\Uri\Http;
35
36/**
37 * Action helper to deal with login followup; responsible for remembering URLs
38 * before login and then redirecting the user to the appropriate place afterwards.
39 *
40 * @category VuFind
41 * @package  Controller_Plugins
42 * @author   Demian Katz <demian.katz@villanova.edu>
43 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
44 * @link     https://vufind.org Main Page
45 */
46class Followup extends AbstractPlugin
47{
48    /**
49     * Session container
50     *
51     * @var Container
52     */
53    protected $session;
54
55    /**
56     * Constructor
57     *
58     * @param Container $session Session container
59     */
60    public function __construct(Container $session)
61    {
62        $this->session = $session;
63    }
64
65    /**
66     * Clear an element of the stored followup information.
67     *
68     * @param string $key Element to clear.
69     *
70     * @return bool       True if cleared, false if never set.
71     */
72    public function clear($key)
73    {
74        if (isset($this->session->$key)) {
75            unset($this->session->$key);
76            return true;
77        }
78        return false;
79    }
80
81    /**
82     * Retrieve the stored followup information.
83     *
84     * @param string $key     Element to retrieve and clear (null for entire
85     * \Laminas\Session\Container object)
86     * @param mixed  $default Default value to return if no stored value found
87     * (ignored when $key is null)
88     *
89     * @return mixed
90     */
91    public function retrieve($key = null, $default = null)
92    {
93        if (null === $key) {
94            return $this->session;
95        }
96        return $this->session->$key ?? $default;
97    }
98
99    /**
100     * Retrieve and then clear a particular followup element.
101     *
102     * @param string $key     Element to retrieve and clear.
103     * @param mixed  $default Default value to return if no stored value found
104     *
105     * @return mixed
106     */
107    public function retrieveAndClear($key, $default = null)
108    {
109        $value = $this->retrieve($key, $default);
110        $this->clear($key);
111        return $value;
112    }
113
114    /**
115     * Store the current URL (and optional additional information) in the session
116     * for use following a successful login.
117     *
118     * @param array  $extras      Associative array of extra fields to store.
119     * @param string $overrideUrl URL to store in place of current server URL (null
120     * for no override)
121     *
122     * @return void
123     */
124    public function store($extras = [], $overrideUrl = null)
125    {
126        // Store the current URL:
127        $url = new Http(
128            !empty($overrideUrl)
129            ? $overrideUrl : $this->getController()->getServerUrl()
130        );
131        $query = $url->getQueryAsArray();
132        unset($query['lightboxParent']);
133        $url->setQuery($query);
134        $this->session->url = $url->toString();
135
136        // Store the extra parameters:
137        foreach ($extras as $key => $value) {
138            $this->session->$key = $value;
139        }
140    }
141}