Completed
Push — master ( 5919d7...94bd1b )
by Rafael
05:28
created

SolrReadService::handleErrorResponses()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 3
eloc 14
nc 3
nop 1
1
<?php
2
namespace ApacheSolrForTypo3\Solr\System\Solr\Service;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2009-2017 Timo Hund <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 2 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use ApacheSolrForTypo3\Solr\System\Solr\SolrCommunicationException;
28
use ApacheSolrForTypo3\Solr\System\Solr\SolrInternalServerErrorException;
29
use ApacheSolrForTypo3\Solr\System\Solr\SolrUnavailableException;
30
31
/**
32
 * Class SolrReadService
33
 * @package ApacheSolrForTypo3\System\Solr\Service
34
 */
35
class SolrReadService extends AbstractSolrService
36
{
37
38
    /**
39
     * @var bool
40
     */
41
    protected $hasSearched = false;
42
43
    /**
44
     * @var \Apache_Solr_Response
45
     */
46
    protected $responseCache = null;
47
48
49
    /**
50
     * @var string
51
     */
52
    protected $_extractUrl;
53
54
    /**
55
     * Performs a search.
56
     *
57
     * @param string $query query string / search term
58
     * @param int $offset result offset for pagination
59
     * @param int $limit number of results to retrieve
60
     * @param array $params additional HTTP GET parameters
61
     * @param string $method The HTTP method (Apache_Solr_Service::METHOD_GET or Apache_Solr_Service::METHOD::POST)
62
     * @return \Apache_Solr_Response Solr response
63
     * @throws \RuntimeException if Solr returns a HTTP status code other than 200
64
     */
65
    public function search($query, $offset = 0, $limit = 10, $params = [], $method = self::METHOD_GET)
66
    {
67
        $response = parent::search($query, $offset, $limit, $params, $method);
68
        $this->hasSearched = true;
69
70
        $this->responseCache = $response;
71
72
        $status = $response->getHttpStatus();
73
        $isValidResponse = $status === 200;
74
        if ($isValidResponse) {
75
            return $response;
76
        }
77
78
        $this->handleErrorResponses($response);
79
    }
80
81
    /**
82
     * Returns whether a search has been executed or not.
83
     *
84
     * @return bool TRUE if a search has been executed, FALSE otherwise
85
     */
86
    public function hasSearched()
87
    {
88
        return $this->hasSearched;
89
    }
90
91
    /**
92
     * Gets the most recent response (if any)
93
     *
94
     * @return \Apache_Solr_Response Most recent response, or NULL if a search has not been executed yet.
95
     */
96
    public function getResponse()
97
    {
98
        return $this->responseCache;
99
    }
100
101
    /**
102
     * This method maps the failed solr requests to a meaningful exception.
103
     *
104
     * @param \Apache_Solr_Response $response
105
     * @throws SolrCommunicationException
106
     * @return \Apache_Solr_Response
107
     */
108
    protected function handleErrorResponses(\Apache_Solr_Response $response)
109
    {
110
        $status = $response->getHttpStatus();
111
        $message = $response->getHttpStatusMessage();
112
113
        if ($status === 0) {
114
            $e = new SolrUnavailableException('Solr Server not available: ' . $message, 1505989391);
115
            $e->setSolrResponse($response);
116
            throw $e;
117
        }
118
119
        if ($status === 500) {
120
            $e = new SolrInternalServerErrorException('Internal Server error during search: ' . $message, 1505989897);
121
            $e->setSolrResponse($response);
122
            throw $e;
123
        }
124
125
        $e = new SolrCommunicationException('Invalid query. Solr returned an error: ' . $status . ' ' . $message, 1293109870);
126
        $e->setSolrResponse($response);
127
128
        throw $e;
129
    }
130
131
}