Passed
Push — master ( 4a930e...4b35d1 )
by Timo
22:47
created

SolrReadService::hasSearched()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 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 3 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\Domain\Search\Query\Query;
28
use ApacheSolrForTypo3\Solr\System\Solr\ResponseAdapter;
29
use ApacheSolrForTypo3\Solr\System\Solr\SolrCommunicationException;
30
use ApacheSolrForTypo3\Solr\System\Solr\SolrInternalServerErrorException;
31
use ApacheSolrForTypo3\Solr\System\Solr\SolrUnavailableException;
32
use Solarium\Exception\HttpException;
33
34
/**
35
 * Class SolrReadService
36
 * @package ApacheSolrForTypo3\System\Solr\Service
37
 */
38
class SolrReadService extends AbstractSolrService
39
{
40
41
    /**
42
     * @var bool
43
     */
44
    protected $hasSearched = false;
45
46
    /**
47
     * @var ResponseAdapter
48
     */
49
    protected $responseCache = null;
50
51
    /**
52
     * Performs a search.
53
     *
54
     * @param Query $query
55
     * @return ResponseAdapter Solr response
56
     * @throws \RuntimeException if Solr returns a HTTP status code other than 200
57
     */
58 53
    public function search($query)
59
    {
60
        try {
61 53
            $request = $this->client->createRequest($query);
62 53
            $response = $this->executeRequest($request);
63 48
            $this->hasSearched = true;
64 48
            $this->responseCache = $response;
65 5
        } catch (HttpException $e) {
66 5
            $this->handleErrorResponses($e);
67
        }
68 48
        return $response;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $response does not seem to be defined for all execution paths leading up to this point.
Loading history...
69
    }
70
71
    /**
72
     * Returns whether a search has been executed or not.
73
     *
74
     * @return bool TRUE if a search has been executed, FALSE otherwise
75
     */
76 1
    public function hasSearched()
77
    {
78 1
        return $this->hasSearched;
79
    }
80
81
    /**
82
     * Gets the most recent response (if any)
83
     *
84
     * @return ResponseAdapter Most recent response, or NULL if a search has not been executed yet.
85
     */
86
    public function getResponse()
87
    {
88
        return $this->responseCache;
89
    }
90
91
    /**
92
     * This method maps the failed solr requests to a meaningful exception.
93
     *
94
     * @param HttpException $exception
95
     * @throws SolrCommunicationException
96
     * @return HttpException
97
     */
98 5
    protected function handleErrorResponses(HttpException $exception)
99
    {
100 5
        $status = $exception->getCode();
101 5
        $message = $exception->getStatusMessage();
102 5
        $solrRespone = new ResponseAdapter($exception->getBody());
103
104 5
        if ($status === 0) {
105 3
            $e = new SolrUnavailableException('Solr Server not available: ' . $message, 1505989391);
106 3
            $e->setSolrResponse($solrRespone);
107 3
            throw $e;
108
        }
109
110 2
        if ($status === 500) {
111 1
            $e = new SolrInternalServerErrorException('Internal Server error during search: ' . $message, 1505989897);
112 1
            $e->setSolrResponse($solrRespone);
113 1
            throw $e;
114
        }
115
116 1
        $e = new SolrCommunicationException('Invalid query. Solr returned an error: ' . $status . ' ' . $message, 1293109870);
117 1
        $e->setSolrResponse($solrRespone);
118
119 1
        throw $e;
120
    }
121
}