Completed
Push — symfony5 ( 074008...b11eb2 )
by
unknown
267:19 queued 255:17
created

SearchHitAdapterFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 24.24 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 8
loc 33
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createAdapter() 8 8 2
A createFixedAdapter() 0 13 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\Pagination\Pagerfanta\AdapterFactory;
10
11
use eZ\Publish\API\Repository\SearchService;
12
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
13
use eZ\Publish\API\Repository\Values\Content\Query;
14
use eZ\Publish\Core\Pagination\Pagerfanta\ContentSearchHitAdapter;
15
use eZ\Publish\Core\Pagination\Pagerfanta\LocationSearchHitAdapter;
16
use Pagerfanta\Adapter\AdapterInterface;
17
use Pagerfanta\Adapter\FixedAdapter;
18
19
/**
20
 * @internal
21
 */
22
final class SearchHitAdapterFactory implements SearchHitAdapterFactoryInterface
23
{
24
    /** @var \eZ\Publish\API\Repository\SearchService */
25
    private $searchService;
26
27
    public function __construct(SearchService $searchService)
28
    {
29
        $this->searchService = $searchService;
30
    }
31
32 View Code Duplication
    public function createAdapter(Query $query, array $languageFilter = []): AdapterInterface
33
    {
34
        if ($query instanceof LocationQuery) {
35
            return new LocationSearchHitAdapter($query, $this->searchService, $languageFilter);
36
        }
37
38
        return new ContentSearchHitAdapter($query, $this->searchService, $languageFilter);
39
    }
40
41
    public function createFixedAdapter(Query $query, array $languageFilter = []): AdapterInterface
42
    {
43
        if ($query instanceof LocationQuery) {
44
            $searchResults = $this->searchService->findLocations($query, $languageFilter);
45
        } else {
46
            $searchResults = $this->searchService->findContent($query, $languageFilter);
47
        }
48
49
        return new FixedAdapter(
50
            $searchResults->totalCount,
51
            $searchResults->searchHits
52
        );
53
    }
54
}
55