Passed
Pull Request — develop (#167)
by Daniel
05:07
created

SearchController::resultsAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Codappix\SearchCore\Controller;
4
5
/*
6
 * Copyright (C) 2016  Daniel Siepmann <[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\Domain\Model\SearchRequest;
25
use Codappix\SearchCore\Domain\Search\CachedSearchService;
26
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
27
28
/**
29
 * Handling search logic in TYPO3 Frontend.
30
 */
31
class SearchController extends ActionController
32
{
33
    /**
34
     * @var CachedSearchService
35
     */
36
    protected $searchService;
37
38
    /**
39
     * @param CachedSearchService $searchService
40
     */
41
    public function __construct(CachedSearchService $searchService)
42
    {
43
        $this->searchService = $searchService;
44
45
        parent::__construct();
46
    }
47
48
    public function initializeSearchAction()
49
    {
50
        if (isset($this->settings['searching']['mode'])
51
            && $this->settings['searching']['mode'] === 'filter'
52
            && $this->request->hasArgument('searchRequest') === false
53
        ) {
54
            $this->request->setArguments(array_merge(
55
                $this->request->getArguments(),
56
                ['searchRequest' => $this->objectManager->get(SearchRequest::class)]
57
            ));
58
        }
59
60
        if ($this->arguments->hasArgument('searchRequest')) {
61
            $this->arguments->getArgument('searchRequest')->getPropertyMappingConfiguration()
62
                ->allowAllProperties();
63
        }
64
    }
65
66
    /**
67
     * Display results and deliver original request and result to view.
68
     */
69
    public function formAction(SearchRequest $searchRequest = null)
70
    {
71
        $this->action($searchRequest);
72
    }
73
74
    /**
75
     * Display results and deliver original request and result to view.
76
     */
77
    public function resultsAction(SearchRequest $searchRequest = null)
78
    {
79
        $this->action($searchRequest);
80
    }
81
82
    private function action(SearchRequest $searchRequest = null)
83
    {
84
        $searchResult = null;
85
        if ($searchRequest !== null) {
86
            $searchResult = $this->searchService->search($searchRequest);
87
        }
88
89
        $this->view->assignMultiple([
90
            'searchRequest' => $searchRequest,
91
            'searchResult' => $searchResult,
92
        ]);
93
    }
94
}
95