Completed
Push — ezp_30973 ( 1b82fd...a5777c )
by
unknown
12:50
created

QueryController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 117
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 6

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A contentQueryAction() 0 6 1
A locationQueryAction() 0 6 1
A contentInfoQueryAction() 0 6 1
A runQuery() 0 7 1
A pagingQueryAction() 0 6 1
A runPagingQuery() 0 18 1
A getAdapter() 0 8 2
1
<?php
2
/**
3
 * @license For full copyright and license information view LICENSE file distributed with this source code.
4
 */
5
namespace eZ\Publish\Core\MVC\Symfony\Controller\Content;
6
7
use eZ\Publish\API\Repository\SearchService;
8
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
9
use eZ\Publish\API\Repository\Values\Content\Query;
10
use eZ\Publish\Core\MVC\Symfony\View\ContentView;
11
use eZ\Publish\Core\Pagination\Pagerfanta\ContentSearchHitAdapter;
12
use eZ\Publish\Core\Pagination\Pagerfanta\LocationSearchHitAdapter;
13
use eZ\Publish\Core\QueryType\ContentViewQueryTypeMapper;
14
use Pagerfanta\Adapter\AdapterInterface;
15
use Pagerfanta\Pagerfanta;
16
use Symfony\Component\HttpFoundation\Request;
17
18
/**
19
 * A content view controller that runs queries based on the matched view configuration.
20
 *
21
 * The action used depends on which type of search is needed: location, content or contentInfo.
22
 */
23
class QueryController
24
{
25
    /** @var \eZ\Publish\API\Repository\SearchService */
26
    private $searchService;
27
28
    /** @var \eZ\Publish\Core\QueryType\ContentViewQueryTypeMapper */
29
    private $contentViewQueryTypeMapper;
30
31
    public function __construct(
32
        ContentViewQueryTypeMapper $contentViewQueryTypeMapper,
33
        SearchService $searchService
34
    ) {
35
        $this->contentViewQueryTypeMapper = $contentViewQueryTypeMapper;
36
        $this->searchService = $searchService;
37
    }
38
39
    /**
40
     * Runs a content search.
41
     *
42
     * @param ContentView $view
43
     * @return ContentView
44
     */
45
    public function contentQueryAction(ContentView $view)
46
    {
47
        $this->runQuery($view, 'findContent');
48
49
        return $view;
50
    }
51
52
    /**
53
     * Runs a location search.
54
     *
55
     * @param ContentView $view
56
     * @return ContentView
57
     */
58
    public function locationQueryAction(ContentView $view)
59
    {
60
        $this->runQuery($view, 'findLocations');
61
62
        return $view;
63
    }
64
65
    /**
66
     * Runs a contentInfo search.
67
     *
68
     * @param ContentView $view
69
     * @return ContentView
70
     */
71
    public function contentInfoQueryAction(ContentView $view)
72
    {
73
        $this->runQuery($view, 'findContentInfo');
74
75
        return $view;
76
    }
77
78
    /**
79
     * Runs the Query defined in $view using $method on SearchService.
80
     *
81
     * @param ContentView $view
82
     * @param string $method Name of the SearchService method to run.
83
     */
84
    private function runQuery(ContentView $view, $method)
85
    {
86
        $searchResults = $this->searchService->$method(
87
            $this->contentViewQueryTypeMapper->map($view)
88
        );
89
        $view->addParameters([$view->getParameter('query')['assign_results_to'] => $searchResults]);
90
    }
91
92
    /**
93
     * @param \eZ\Publish\Core\MVC\Symfony\View\ContentView $view
94
     * @param \Symfony\Component\HttpFoundation\Request $request
95
     * @return \eZ\Publish\Core\MVC\Symfony\View\ContentView
96
     */
97
    public function pagingQueryAction(ContentView $view, Request $request)
98
    {
99
        $this->runPagingQuery($view, $request);
100
101
        return $view;
102
    }
103
104
    /**
105
     * @param \eZ\Publish\Core\MVC\Symfony\View\ContentView $view
106
     * @param \Symfony\Component\HttpFoundation\Request $request
107
     */
108
    private function runPagingQuery(ContentView $view, Request $request)
109
    {
110
        $queryParameters = $view->getParameter('query');
111
112
        $limit = $queryParameters['limit'] ?? 10;
113
        $pageParam = $queryParameters['page_param'] ?? 'page';
114
115
        $page = $request->get($pageParam, 1);
116
117
        $pager = new Pagerfanta(
118
            $this->getAdapter($this->contentViewQueryTypeMapper->map($view))
119
        );
120
121
        $pager->setMaxPerPage($limit);
122
        $pager->setCurrentPage($page);
123
124
        $view->addParameters([$queryParameters['assign_results_to'] => $pager]);
125
    }
126
127
    /**
128
     * @param \eZ\Publish\API\Repository\Values\Content\Query $query
129
     * @return \Pagerfanta\Adapter\AdapterInterface
130
     */
131
    private function getAdapter(Query $query): AdapterInterface
132
    {
133
        if ($query instanceof LocationQuery) {
134
            return new LocationSearchHitAdapter($query, $this->searchService);
135
        }
136
137
        return new ContentSearchHitAdapter($query, $this->searchService);
138
    }
139
}
140