Passed
Push — master ( d5568c...ac874d )
by Timo
28:49 queued 03:56
created

AbstractWidgetController::initializeSearch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1.0019

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 10
ccs 7
cts 8
cp 0.875
rs 10
cc 1
nc 1
nop 1
crap 1.0019
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Widget;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use ApacheSolrForTypo3\Solr\ConnectionManager;
18
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSetService;
19
use ApacheSolrForTypo3\Solr\Search;
20
use ApacheSolrForTypo3\Solr\System\Configuration\ConfigurationManager;
21
use ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext;
22
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
23
use ApacheSolrForTypo3\Solr\Util;
24
use ApacheSolrForTypo3\Solr\Widget\WidgetRequest;
25
use TYPO3\CMS\Core\Utility\GeneralUtility;
26
use TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetController as CoreAbstractWidgetController;
27
28
/**
29
 * Class AbstractWidgetController
30
 *
31
 * @author Frans Saris <[email protected]>
32
 * @author Timo Hund <[email protected]>
33
 */
34
class AbstractWidgetController extends CoreAbstractWidgetController
35
{
36
37
    /**
38
     * @var array
39
     */
40
    protected $supportedRequestTypes = [WidgetRequest::class];
41
42
    /**
43
     * @var ConfigurationManager
44
     */
45
    private $solrConfigurationManager;
46
47
    /**
48
     * @var \ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext
49
     */
50
    protected $controllerContext;
51
52
    /**
53
     * @param \ApacheSolrForTypo3\Solr\System\Configuration\ConfigurationManager
54
     */
55 36
    public function injectSolrConfigurationManager(ConfigurationManager $configurationManager)
56
    {
57 36
        $this->solrConfigurationManager = $configurationManager;
58 36
    }
59
60
    /**
61
     * Initialize the controller context
62
     *
63
     * @return \ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext ControllerContext to be passed to the view
64
     * @api
65
     */
66 35
    protected function buildControllerContext()
67
    {
68
        /** @var $controllerContext \ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext */
69 35
        $controllerContext = $this->objectManager->get(SolrControllerContext::class);
70 35
        $controllerContext->setRequest($this->request);
71 35
        $controllerContext->setResponse($this->response);
72 35
        if ($this->arguments !== null) {
73 35
            $controllerContext->setArguments($this->arguments);
74
        }
75 35
        $controllerContext->setUriBuilder($this->uriBuilder);
76 35
        $typoScriptConfiguration = $this->solrConfigurationManager->getTypoScriptConfiguration();
77 35
        $controllerContext->setTypoScriptConfiguration($typoScriptConfiguration);
78
79 35
        $this->setActiveSearchResultSet($controllerContext);
80
81 35
        return $controllerContext;
82
    }
83
84
    /**
85
     * @param \ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext $controllerContext
86
     * @return \ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext
87
     */
88 34
    protected function setActiveSearchResultSet($controllerContext)
89
    {
90 34
        $resultSetService = $this->initializeSearch($controllerContext->getTypoScriptConfiguration());
91 34
        $lastResult = $resultSetService->getLastResultSet();
92 34
        if (!is_null($lastResult)) {
93
            $controllerContext->setSearchResultSet($lastResult);
94
        }
95
96 34
        return $controllerContext;
97
    }
98
99
    /**
100
     * @param TypoScriptConfiguration $typoScriptConfiguration
101
     * @return SearchResultSetService
102
     */
103 34
    protected function initializeSearch(TypoScriptConfiguration $typoScriptConfiguration)
104
    {
105
        /** @var \ApacheSolrForTypo3\Solr\ConnectionManager $solrConnection */
106 34
        $solrConnection = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionByPageId($GLOBALS['TSFE']->id, Util::getLanguageUid(), $GLOBALS['TSFE']->MP);
107 34
        $search = GeneralUtility::makeInstance(Search::class, /** @scrutinizer ignore-type */ $solrConnection);
108
109 34
        return GeneralUtility::makeInstance(
110 34
            SearchResultSetService::class,
111 34
            /** @scrutinizer ignore-type */ $typoScriptConfiguration,
112 34
            /** @scrutinizer ignore-type */ $search
113
        );
114
    }
115
}
116