Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
InjectOnCampusListener
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
5 / 5
7
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
 setPermissionHandler
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 attach
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 getOnCampus
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 onSearchPre
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * OnCampus listener.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2013.
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   Oliver Goldschmidt <o.goldschmidt@tuhh.de>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org Main Site
28 */
29
30namespace VuFind\Search\Primo;
31
32use Laminas\EventManager\EventInterface;
33use Laminas\EventManager\SharedEventManagerInterface;
34use LmcRbacMvc\Service\AuthorizationServiceAwareTrait;
35use VuFindSearch\Service;
36
37/**
38 * OnCampus listener.
39 * This listener detects whether a user is on campus or not.
40 *
41 * @category VuFind
42 * @package  Search
43 * @author   Oliver Goldschmidt <o.goldschmidt@tuhh.de>
44 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
45 * @link     https://vufind.org Main Site
46 */
47class InjectOnCampusListener
48{
49    use AuthorizationServiceAwareTrait;
50
51    /**
52     * Primo Permission Handler.
53     *
54     * @var PrimoPermissionHandler
55     */
56    protected $permissionHandler;
57
58    /**
59     * Is user on campus or not?
60     *
61     * @var bool
62     */
63    protected $isOnCampus;
64
65    /**
66     * Constructor.
67     *
68     * @param PrimoPermissionHandler $pph Primo Permission Handler
69     *
70     * @return void
71     */
72    public function __construct($pph = null)
73    {
74        $this->setPermissionHandler($pph);
75    }
76
77    /**
78     * Constructor.
79     *
80     * @param PrimoPermissionHandler $pph Primo Permission Handler
81     *
82     * @return void
83     */
84    public function setPermissionHandler($pph)
85    {
86        $this->permissionHandler = $pph;
87        $this->isOnCampus = null; // clear cache
88    }
89
90    /**
91     * Attach listener to shared event manager.
92     *
93     * @param SharedEventManagerInterface $manager Shared event manager
94     *
95     * @return void
96     */
97    public function attach(SharedEventManagerInterface $manager)
98    {
99        $manager->attach(
100            Service::class,
101            Service::EVENT_PRE,
102            [$this, 'onSearchPre']
103        );
104    }
105
106    /**
107     * Determines, which value is needed for the onCampus parameter
108     *
109     * @return bool
110     */
111    protected function getOnCampus()
112    {
113        if (null === $this->isOnCampus) {
114            $this->isOnCampus = $this->permissionHandler
115                ? $this->permissionHandler->hasPermission() : false;
116        }
117        return $this->isOnCampus;
118    }
119
120    /**
121     * Set up onCampus Listener.
122     *
123     * @param EventInterface $event Event
124     *
125     * @return EventInterface
126     */
127    public function onSearchPre(EventInterface $event)
128    {
129        $params = $event->getParam('command')->getSearchParameters();
130        $params->set('onCampus', $this->getOnCampus());
131
132        return $event;
133    }
134}