Completed
Push — ezp-31299-additional-search-la... ( 045827...ee5488 )
by
unknown
22:27
created

ContentSearchHitAdapter::getDisplayLanguage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the ContentSearchHitAdapter class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Pagination\Pagerfanta;
10
11
use eZ\Publish\API\Repository\Values\Content\Query;
12
use eZ\Publish\API\Repository\SearchService;
13
use Pagerfanta\Adapter\AdapterInterface;
14
15
/**
16
 * Pagerfanta adapter for eZ Publish content search.
17
 * Will return results as SearchHit objects.
18
 */
19 View Code Duplication
class ContentSearchHitAdapter implements AdapterInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20
{
21
    /** @var \eZ\Publish\API\Repository\Values\Content\Query */
22
    private $query;
23
24
    /** @var \eZ\Publish\API\Repository\SearchService */
25
    private $searchService;
26
27
    /** @var array */
28
    private $languageFilter;
29
30
    /** @var int */
31
    private $nbResults;
32
33
    public function __construct(
34
        Query $query,
35
        SearchService $searchService,
36
        array $languageFilter = []
37
    ) {
38
        $this->query = $query;
39
        $this->searchService = $searchService;
40
        $this->languageFilter = $languageFilter;
41
    }
42
43
    /**
44
     * Returns the number of results.
45
     *
46
     * @return int The number of results.
47
     */
48
    public function getNbResults()
49
    {
50
        if (isset($this->nbResults)) {
51
            return $this->nbResults;
52
        }
53
54
        $countQuery = clone $this->query;
55
        $countQuery->limit = 0;
56
57
        $searchResult = $this->searchService->findContent(
58
            $countQuery,
59
            $this->languageFilter
60
        );
61
62
        return $this->nbResults = $searchResult->totalCount;
63
    }
64
65
    /**
66
     * Returns a slice of the results, as SearchHit objects.
67
     *
68
     * @param int $offset The offset.
69
     * @param int $length The length.
70
     *
71
     * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchHit[]
72
     */
73
    public function getSlice($offset, $length)
74
    {
75
        $query = clone $this->query;
76
        $query->offset = $offset;
77
        $query->limit = $length;
78
        $query->performCount = false;
79
80
        $searchResult = $this->searchService->findContent($query, $this->languageFilter);
81
82
        // Set count for further use if returned by search engine despite !performCount (Solr, ES)
83
        if (!isset($this->nbResults) && isset($searchResult->totalCount)) {
84
            $this->nbResults = $searchResult->totalCount;
85
        }
86
87
        return $searchResult->searchHits;
88
    }
89
}
90