Passed
Pull Request — develop (#167)
by Daniel
04:48
created

SearchController::formAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
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
    /**
49
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException
50
     * @return void
51
     */
52
    public function initializeSearchAction()
53
    {
54
        if (isset($this->settings['searching']['mode'])
55
            && $this->settings['searching']['mode'] === 'filter'
56
            && $this->request->hasArgument('searchRequest') === false
57
        ) {
58
            $this->request->setArguments(array_merge(
59
                $this->request->getArguments(),
60
                ['searchRequest' => $this->objectManager->get(SearchRequest::class)]
61
            ));
62
        }
63
64
        if ($this->arguments->hasArgument('searchRequest')) {
65
            $this->arguments->getArgument('searchRequest')->getPropertyMappingConfiguration()
66
                ->allowAllProperties();
67
        }
68
    }
69
70
    /**
71
     * Action: Search form
72
     *
73
     * @param SearchRequest $searchRequest
74
     * @return void
75
     */
76
    public function formAction(SearchRequest $searchRequest = null)
77
    {
78
        $searchResult = null;
79
        if ($searchRequest !== null) {
80
            $searchResult = $this->searchService->search($searchRequest);
81
        }
82
83
        $this->view->assignMultiple([
84
            'searchRequest' => $searchRequest,
85
            'searchResult' => $searchResult,
86
        ]);
87
    }
88
89
    /**
90
     * Action: Display list results and deliver original request and result to view.
91
     *
92
     * @param SearchRequest $searchRequest
93
     * @return void
94
     */
95
    public function resultsAction(SearchRequest $searchRequest = null)
96
    {
97
        $searchResult = null;
98
        if ($searchRequest !== null) {
99
            $searchResult = $this->searchService->search($searchRequest);
100
        }
101
102
        $this->view->assignMultiple([
103
            'searchRequest' => $searchRequest,
104
            'searchResult' => $searchResult,
105
        ]);
106
    }
107
}
108