Passed
Pull Request — develop (#167)
by
unknown
04:28
created

CachedSearchService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Codappix\SearchCore\Domain\Search;
4
5
use Codappix\SearchCore\Connection\SearchRequestInterface;
6
use Codappix\SearchCore\Connection\SearchResultInterface;
7
use TYPO3\CMS\Core\SingletonInterface;
8
9
/**
10
 * Service: Cached Search
11
 * @package Codappix\SearchCore\Domain\Search
12
 */
13
class CachedSearchService implements SingletonInterface
14
{
15
    /**
16
     * @var array
17
     */
18
    protected $results = [];
19
20
    /**
21
     * @var SearchService
22
     */
23
    protected $searchService;
24
25
    /**
26
     * @param SearchService $searchService
27
     */
28
    public function __construct(SearchService $searchService)
29
    {
30
        $this->searchService = $searchService;
31
    }
32
33
    /**
34
     * @param SearchRequestInterface $searchRequest
35
     * @return SearchResultInterface
36
     */
37
    public function search(SearchRequestInterface $searchRequest): SearchResultInterface
38
    {
39
        $hash = $this->getHash($searchRequest);
40
        if (isset($this->results[$hash]) && $this->results[$hash] instanceof SearchResultInterface) {
41
            return $this->results[$hash];
42
        }
43
        return $this->results[$hash] = $this->searchService->search($searchRequest);
44
    }
45
46
    /**
47
     * @param SearchRequestInterface $searchRequest
48
     * @return string
49
     */
50
    protected function getHash(SearchRequestInterface $searchRequest): string
51
    {
52
        if (is_callable([$searchRequest, 'getRequestHash'])) {
53
            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

53
            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...
54
        }
55
        return sha1(serialize($searchRequest));
56
    }
57
}
58