Passed
Pull Request — develop (#167)
by Daniel
04:48
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: Cached Search
30
 * @package Codappix\SearchCore\Domain\Search
31
 */
32
class CachedSearchService implements SingletonInterface
33
{
34
    /**
35
     * @var array
36
     */
37
    protected $results = [];
38
39
    /**
40
     * @var SearchService
41
     */
42
    protected $searchService;
43
44
    /**
45
     * @param SearchService $searchService
46
     */
47
    public function __construct(SearchService $searchService)
48
    {
49
        $this->searchService = $searchService;
50
    }
51
52
    /**
53
     * @param SearchRequestInterface $searchRequest
54
     * @return SearchResultInterface
55
     */
56
    public function search(SearchRequestInterface $searchRequest): SearchResultInterface
57
    {
58
        $hash = $this->getHash($searchRequest);
59
        if (isset($this->results[$hash]) && $this->results[$hash] instanceof SearchResultInterface) {
60
            return $this->results[$hash];
61
        }
62
        return $this->results[$hash] = $this->searchService->search($searchRequest);
63
    }
64
65
    /**
66
     * @param SearchRequestInterface $searchRequest
67
     * @return string
68
     */
69
    protected function getHash(SearchRequestInterface $searchRequest): string
70
    {
71
        if (is_callable([$searchRequest, 'getRequestHash'])) {
72
            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

72
            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...
73
        }
74
        return sha1(serialize($searchRequest));
75
    }
76
}
77