Passed
Pull Request — main (#3444)
by Markus
29:09
created

SolrReadService   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 92.59%

Importance

Changes 0
Metric Value
wmc 9
eloc 31
c 0
b 0
f 0
dl 0
loc 98
ccs 25
cts 27
cp 0.9259
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A search() 0 12 2
A hasSearched() 0 3 1
A handleErrorResponse() 0 21 4
A getResponse() 0 3 1
A handleErrorResponses() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace ApacheSolrForTypo3\Solr\System\Solr\Service;
19
20
use ApacheSolrForTypo3\Solr\Domain\Search\Query\Query;
21
use ApacheSolrForTypo3\Solr\System\Solr\ResponseAdapter;
22
use ApacheSolrForTypo3\Solr\System\Solr\SolrCommunicationException;
23
use ApacheSolrForTypo3\Solr\System\Solr\SolrInternalServerErrorException;
24
use ApacheSolrForTypo3\Solr\System\Solr\SolrUnavailableException;
25
use RuntimeException;
26
use Solarium\Exception\HttpException;
27
28
/**
29
 * Class SolrReadService
30
 */
31
class SolrReadService extends AbstractSolrService
32
{
33
    /**
34
     * @var bool
35
     */
36
    protected bool $hasSearched = false;
37
38
    /**
39
     * @var ResponseAdapter|null
40
     */
41
    protected ?ResponseAdapter $responseCache = null;
42
43
    /**
44
     * Performs a search.
45
     *
46
     * @param Query $query
47
     * @return ResponseAdapter Solr response
48
     * @throws RuntimeException if Solr returns a HTTP status code other than 200
49
     */
50 49
    public function search(Query $query): ResponseAdapter
51
    {
52
        $request = $this->client->createRequest($query);
53 49
        $response = $this->executeRequest($request);
54 49
55 46
        if ($response->getHttpStatus() === 200) {
56 46
            $this->hasSearched = true;
57 3
            $this->responseCache = $response;
58 3
        } else {
59
            $this->handleErrorResponse($response);
60 46
        }
61
        return $response;
62
    }
63
64
    /**
65
     * Returns whether a search has been executed or not.
66
     *
67
     * @return bool TRUE if a search has been executed, FALSE otherwise
68 1
     */
69
    public function hasSearched(): bool
70 1
    {
71
        return $this->hasSearched;
72
    }
73
74
    /**
75
     * Gets the most recent response (if any)
76
     *
77
     * @return ResponseAdapter|null Most recent response, or NULL if a search has not been executed yet.
78
     */
79
    public function getResponse(): ?ResponseAdapter
80
    {
81
        return $this->responseCache;
82
    }
83
84
    /**
85
     * This method maps the failed solr requests to a meaningful exception.
86
     *
87
     * @param HttpException $exception
88
     * @throws SolrCommunicationException
89 3
     * @deprecated handleErrorResponses is deprecated and will be removed in v12, use handleErrorResponse() instead
90
     */
91 3
    protected function handleErrorResponses(HttpException $exception)
92 3
    {
93 3
        trigger_error(
94
            'handleErrorResponses() is deprecated and will be removed in v12, use handleErrorResponse() instead',
95 3
            E_USER_DEPRECATED
96 1
        );
97 1
98 1
        $solrResponse = new ResponseAdapter($exception->getBody(), $exception->getCode(), $exception->getStatusMessage());
99
        $this->handleErrorResponse($solrResponse);
100
    }
101 2
102 1
    /**
103 1
     * This method handles a failed Solr request and maps it to a meaningful exception.
104 1
     *
105
     * @return ResponseAdapter $response
106
     * @throws SolrCommunicationException
107 1
     */
108 1
    protected function handleErrorResponse(ResponseAdapter $response): void
109
    {
110 1
        $status = $response->getHttpStatus();
111
        $message = $response->getHttpStatusMessage();
112
113
        if ($status === 0 || $status === 502) {
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