Completed
Push — ezp26175-exception_on_non_defa... ( 77d2f3...ca5fc8 )
by
unknown
39:33
created

QueryController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

5 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
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\Core\MVC\Symfony\View\ContentView;
9
use eZ\Publish\Core\QueryType\ContentViewQueryTypeMapper;
10
11
/**
12
 * A content view controller that runs queries based on the matched view configuration.
13
 *
14
 * The action used depends on which type of search is needed: location, content or contentInfo.
15
 */
16
class QueryController
17
{
18
    /** @var \eZ\Publish\API\Repository\SearchService */
19
    private $searchService;
20
21
    /** @var \eZ\Publish\Core\QueryType\ContentViewQueryTypeMapper */
22
    private $contentViewQueryTypeMapper;
23
24
    public function __construct(
25
        ContentViewQueryTypeMapper $contentViewQueryTypeMapper,
26
        SearchService $searchService
27
    ) {
28
        $this->contentViewQueryTypeMapper = $contentViewQueryTypeMapper;
29
        $this->searchService = $searchService;
30
    }
31
32
    /**
33
     * Runs a content search.
34
     *
35
     * @param ContentView $view
36
     * @return ContentView
37
     */
38
    public function contentQueryAction(ContentView $view)
39
    {
40
        $this->runQuery($view, 'findContent');
41
42
        return $view;
43
    }
44
45
    /**
46
     * Runs a location search.
47
     *
48
     * @param ContentView $view
49
     * @return ContentView
50
     */
51
    public function locationQueryAction(ContentView $view)
52
    {
53
        $this->runQuery($view, 'findLocations');
54
55
        return $view;
56
    }
57
58
    /**
59
     * Runs a contentInfo search.
60
     *
61
     * @param ContentView $view
62
     * @return ContentView
63
     */
64
    public function contentInfoQueryAction(ContentView $view)
65
    {
66
        $this->runQuery($view, 'findContentInfo');
67
68
        return $view;
69
    }
70
71
    /**
72
     * Runs the Query defined in $view using $method on SearchService.
73
     *
74
     * @param ContentView $view
75
     * @param string $method Name of the SearchService method to run.
76
     */
77
    private function runQuery(ContentView $view, $method)
78
    {
79
        $searchResults = $this->searchService->$method(
80
            $this->contentViewQueryTypeMapper->map($view)
81
        );
82
        $view->addParameters([$view->getParameter('query')['assign_results_to'] => $searchResults]);
83
    }
84
}
85