Completed
Pull Request — develop (#167)
by Daniel
25:11
created

CachedSearchService   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 12
dl 0
loc 37
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A search() 0 7 3
A __construct() 0 3 1
A getHash() 0 6 2
A processResult() 0 3 1
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, SearchServiceInterface
33
{
34
    /**
35
     * @var array
36
     */
37
    protected $results = [];
38
39
    /**
40
     * @var SearchService
41
     */
42
    protected $searchService;
43
44
    public function __construct(SearchService $searchService)
45
    {
46
        $this->searchService = $searchService;
47
    }
48
49
    public function search(SearchRequestInterface $searchRequest): SearchResultInterface
50
    {
51
        $hash = $this->getHash($searchRequest);
52
        if (isset($this->results[$hash]) && $this->results[$hash] instanceof SearchResultInterface) {
53
            return $this->results[$hash];
54
        }
55
        return $this->results[$hash] = $this->searchService->search($searchRequest);
56
    }
57
58
    public function processResult(SearchResultInterface $searchResult): SearchResultInterface
59
    {
60
        return $this->searchService->processResult($searchResult);
61
    }
62
63
    protected function getHash(SearchRequestInterface $searchRequest): string
64
    {
65
        if (is_callable([$searchRequest, 'getRequestHash'])) {
66
            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

66
            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...
67
        }
68
        return sha1(serialize($searchRequest));
69
    }
70
}
71