Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractErrorListener
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
4 / 4
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 addBackend
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 listenForBackend
100.00% covered (success)
100.00%
1 / 1
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
 onSearchError
n/a
0 / 0
n/a
0 / 0
0
1<?php
2
3/**
4 * Abstract base class of SOLR error listeners.
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   David Maus <maus@hab.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\Solr;
31
32use Laminas\EventManager\EventInterface;
33use Laminas\EventManager\SharedEventManagerInterface;
34use VuFindSearch\Service;
35
36use function in_array;
37
38/**
39 * Abstract base class of SOLR error listeners.
40 *
41 * @category VuFind
42 * @package  Search
43 * @author   David Maus <maus@hab.de>
44 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
45 * @link     https://vufind.org Main Site
46 */
47abstract class AbstractErrorListener
48{
49    /**
50     * Tag indicating a parser error.
51     *
52     * @var string
53     */
54    public const TAG_PARSER_ERROR = 'VuFind\Search\ParserError';
55
56    /**
57     * Backends to listen for.
58     *
59     * @var array
60     */
61    protected $backends;
62
63    /**
64     * Constructor.
65     *
66     * @param string $backend Identifier of backend to listen for
67     *
68     * @return void
69     */
70    public function __construct(string $backend)
71    {
72        $this->backends = [];
73        $this->addBackend($backend);
74    }
75
76    /**
77     * Add backend to listen for.
78     *
79     * @param string $backend Identifier of backend to listen for
80     *
81     * @return void
82     */
83    public function addBackend(string $backend)
84    {
85        if (!$this->listenForBackend($backend)) {
86            $this->backends[] = $backend;
87        }
88    }
89
90    /**
91     * Return true if listeners listens for backend errors.
92     *
93     * @param string $backend Backend identifier
94     *
95     * @return bool
96     */
97    public function listenForBackend(string $backend)
98    {
99        return in_array($backend, $this->backends);
100    }
101
102    /**
103     * Attach listener to shared event manager.
104     *
105     * @param SharedEventManagerInterface $manager Shared event manager
106     *
107     * @return void
108     */
109    public function attach(SharedEventManagerInterface $manager)
110    {
111        $manager->attach(
112            Service::class,
113            Service::EVENT_ERROR,
114            [$this, 'onSearchError']
115        );
116    }
117
118    /**
119     * VuFindSearch.error event.
120     *
121     * @param EventInterface $event The event
122     *
123     * @return EventInterface
124     */
125    abstract public function onSearchError(EventInterface $event);
126}