diff --git a/module/VuFind/src/VuFind/Search/Solr/AbstractErrorListener.php b/module/VuFind/src/VuFind/Search/Solr/AbstractErrorListener.php new file mode 100644 index 0000000000000000000000000000000000000000..5ee81b7691ce9cf6a274cb7084a49039dea166ec --- /dev/null +++ b/module/VuFind/src/VuFind/Search/Solr/AbstractErrorListener.php @@ -0,0 +1,115 @@ +<?php + +/** + * Abstract base class of SOLR error listeners. + * + * PHP version 5 + * + * Copyright (C) Villanova University 2013. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * @category VuFind2 + * @package Search + * @author David Maus <maus@hab.de> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org Main Site + */ + +namespace VuFind\Search\Solr; + +use VuFindSearch\Backend\BackendInterface; + +use Zend\EventManager\EventInterface; + +/** + * Abstract base class of SOLR error listeners. + * + * @category VuFind2 + * @package Search + * @author David Maus <maus@hab.de> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org Main Site + */ + +abstract class AbstractErrorListener +{ + + /** + * Tag indicating a parser error. + * + * @var string + */ + const TAG_PARSER_ERROR = 'VuFind\Search\ParserError'; + + /** + * Backends to listen for. + * + * @var array + */ + protected $backends; + + /** + * Constructor. + * + * @param string $backend Name of backend to listen for + * + * @return void + */ + public function __construct($backend) + { + $this->backends = array(); + $this->addBackend($backend); + } + + /** + * Add backend to listen for. + * + * @param BackendInterface|string $backend Backend instance or name of backend + * + * @return void + */ + public function addBackend($backend) + { + if ($backend instanceOf BackendInterface) { + $this->backends[] = $backend->getName(); + } else { + $this->backends[] = $backend; + } + } + + /** + * Return true if listeners listens for backend errors. + * + * @param BackendInterface|string $backend Backend instance or name of backend + * + * @return boolean + */ + public function listenForBackend($backend) + { + if ($backend instanceOf BackendInterface) { + $backend = $backend->getName(); + } + return in_array($backend, $this->backends); + } + + /** + * VuFindSearch.error event. + * + * @param EventInterface $event The event + * + * @return EventInterface + */ + abstract public function onSearchError(EventInterface $event); +} \ No newline at end of file diff --git a/module/VuFind/src/VuFind/Search/Solr/V3/ErrorListener.php b/module/VuFind/src/VuFind/Search/Solr/V3/ErrorListener.php index 620c72293321c4ea413503592fc19e62f6489d30..f42bb6cb2c94f1d1f3623412ed4e7b083c3bcfe5 100644 --- a/module/VuFind/src/VuFind/Search/Solr/V3/ErrorListener.php +++ b/module/VuFind/src/VuFind/Search/Solr/V3/ErrorListener.php @@ -29,6 +29,8 @@ namespace VuFind\Search\Solr\V3; +use VuFind\Search\Solr\AbstractErrorListener; + use VuFindSearch\Backend\Exception\HttpErrorException; use Zend\EventManager\EventInterface; @@ -42,27 +44,8 @@ use Zend\EventManager\EventInterface; * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @link http://vufind.org Main Site */ -class ErrorListener +class ErrorListener extends AbstractErrorListener { - /** - * Backends to listen on. - * - * @var array - */ - protected $backends; - - /** - * Constructor. - * - * @param array $backends Name of backends to listen on - * - * @return void - */ - public function __construct(array $backends) - { - $this->backends = $backends; - } - /** * VuFindSearch.error * @@ -73,7 +56,7 @@ class ErrorListener public function onSearchError(EventInterface $event) { $backend = $event->getParam('backend'); - if (in_array($backend, $this->backends)) { + if ($this->listenForBackend($backend)) { $error = $event->getTarget(); if ($error instanceOf HttpErrorException) { $reason = $error->getResponse()->getReasonPhrase(); diff --git a/module/VuFind/src/VuFind/Search/Solr/V4/ErrorListener.php b/module/VuFind/src/VuFind/Search/Solr/V4/ErrorListener.php index 74c018d0926164b912ae7f5fbaa34f708d8493ad..8458a439b177341f3a7abbc1db3ebfa71d2fab96 100644 --- a/module/VuFind/src/VuFind/Search/Solr/V4/ErrorListener.php +++ b/module/VuFind/src/VuFind/Search/Solr/V4/ErrorListener.php @@ -30,6 +30,7 @@ namespace VuFind\Search\Solr\V4; use VuFindSearch\Backend\Exception\HttpErrorException; +use VuFind\Search\Solr\AbstractErrorListener; use Zend\Http\Response; use Zend\EventManager\EventInterface; @@ -43,14 +44,8 @@ use Zend\EventManager\EventInterface; * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @link http://vufind.org Main Site */ -class ErrorListener +class ErrorListener extends AbstractErrorListener { - /** - * Parser error tag. - * - * @var string - */ - const TAG_PARSER_ERROR = 'VuFind\Search\ParserError'; /** * Normalized media types. @@ -61,25 +56,6 @@ class ErrorListener const TYPE_JSON = 'json'; const TYPE_XML = 'xml'; - /** - * Backends to listen on. - * - * @var array - */ - protected $backends; - - /** - * Constructor. - * - * @param array $backends Name of backends to listen on - * - * @return void - */ - public function __construct(array $backends) - { - $this->backends = $backends; - } - /** * VuFindSearch.error * @@ -90,7 +66,7 @@ class ErrorListener public function onSearchError(EventInterface $event) { $backend = $event->getParam('backend'); - if (in_array($backend, $this->backends)) { + if ($this->listenForBackend($backend)) { $error = $event->getTarget(); if ($error instanceOf HttpErrorException) { $response = $error->getResponse(); @@ -127,7 +103,8 @@ class ErrorListener if (isset($body->error->msg)) { $reason = $body->error->msg; if (stristr($reason, 'org.apache.solr.search.SyntaxError') - || stristr($reason, 'undefined field')) { + || stristr($reason, 'undefined field') + ) { $tags[] = self::TAG_PARSER_ERROR; } } diff --git a/module/VuFind/tests/unit-tests/src/Search/Solr/V3/ErrorListenerTest.php b/module/VuFind/tests/unit-tests/src/Search/Solr/V3/ErrorListenerTest.php index 4386972c8b1edba1827e96cff73b63c329713dcb..708fe31e593fcce624f1767f501df99b1045feef 100644 --- a/module/VuFind/tests/unit-tests/src/Search/Solr/V3/ErrorListenerTest.php +++ b/module/VuFind/tests/unit-tests/src/Search/Solr/V3/ErrorListenerTest.php @@ -63,7 +63,7 @@ class ErrorListenerTest extends TestCase $exception = HttpErrorException::createFromResponse($response); $params = array('backend' => 'test'); $event = new Event(null, $exception, $params); - $listener = new ErrorListener(array('test')); + $listener = new ErrorListener('test'); $listener->onSearchError($event); $this->assertTrue($exception->hasTag('VuFind\Search\ParserError')); } @@ -80,7 +80,7 @@ class ErrorListenerTest extends TestCase $exception = HttpErrorException::createFromResponse($response); $params = array('backend' => 'test'); $event = new Event(null, $exception, $params); - $listener = new ErrorListener(array('test')); + $listener = new ErrorListener('test'); $listener->onSearchError($event); $this->assertTrue($exception->hasTag('VuFind\Search\ParserError')); } diff --git a/module/VuFind/tests/unit-tests/src/Search/Solr/V4/ErrorListenerTest.php b/module/VuFind/tests/unit-tests/src/Search/Solr/V4/ErrorListenerTest.php index 33dad92f8264c9b0a35a6373f35c10715d80dc64..84d51543757b09ec24409a7bef69171e0d3421df 100644 --- a/module/VuFind/tests/unit-tests/src/Search/Solr/V4/ErrorListenerTest.php +++ b/module/VuFind/tests/unit-tests/src/Search/Solr/V4/ErrorListenerTest.php @@ -63,7 +63,7 @@ class ErrorListenerTest extends TestCase $exception = HttpErrorException::createFromResponse($response); $params = array('backend' => 'test'); $event = new Event(null, $exception, $params); - $listener = new ErrorListener(array('test')); + $listener = new ErrorListener('test'); $listener->onSearchError($event); $this->assertTrue($exception->hasTag('VuFind\Search\ParserError')); } @@ -80,7 +80,7 @@ class ErrorListenerTest extends TestCase $exception = HttpErrorException::createFromResponse($response); $params = array('backend' => 'test'); $event = new Event(null, $exception, $params); - $listener = new ErrorListener(array('test')); + $listener = new ErrorListener('test'); $listener->onSearchError($event); $this->assertTrue($exception->hasTag('VuFind\Search\ParserError')); }