Passed
Pull Request — develop (#167)
by Daniel
04:23
created

CachedSearchService::search()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 12
1
<?php
2
3
namespace Codappix\SearchCore\Domain\Search;
4
5
/*
6
 * Copyright (C) 2018 Benjamin Serfhos <[email protected]>
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21
 * 02110-1301, USA.
22
 */
23
24
use Codappix\SearchCore\Connection\SearchRequestInterface;
25
use Codappix\SearchCore\Connection\SearchResultInterface;
26
use TYPO3\CMS\Core\SingletonInterface;
27
28
/**
29
 * Service to process a search request, only once per request.
30
 */
31
class CachedSearchService implements SingletonInterface, SearchServiceInterface
32
{
33
    /**
34
     * @var array
35
     */
36
    protected $results = [];
37
38
    /**
39
     * @var SearchService
40
     */
41
    protected $searchService;
42
43
    public function __construct(SearchService $searchService)
44
    {
45
        $this->searchService = $searchService;
46
    }
47
48
    public function search(SearchRequestInterface $searchRequest): SearchResultInterface
49
    {
50
        $hash = $this->getHash($searchRequest);
51
        if (isset($this->results[$hash]) && $this->results[$hash] instanceof SearchResultInterface) {
52
            return $this->results[$hash];
53
        }
54
        return $this->results[$hash] = $this->searchService->search($searchRequest);
55
    }
56
57
    public function processResult(SearchResultInterface $searchResult): SearchResultInterface
58
    {
59
        return $this->searchService->processResult($searchResult);
60
    }
61
62
    protected function getHash(SearchRequestInterface $searchRequest): string
63
    {
64
        if (is_callable([$searchRequest, 'getRequestHash'])) {
65
            return (string)$searchRequest->getRequestHash();
0 ignored issues
show
Bug introduced by
The method getRequestHash() does not exist on Codappix\SearchCore\Conn...\SearchRequestInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
            return (string)$searchRequest->/** @scrutinizer ignore-call */ getRequestHash();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66
        }
67
        return sha1(serialize($searchRequest));
68
    }
69
}
70