Passed
Push — master ( ee0235...88a6dd )
by Timo
23:30
created

SortingComponent::initializeSearchComponent()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 4
nop 0
crap 4
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Search;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2012-2015 Ingo Renner <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 3 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use ApacheSolrForTypo3\Solr\Domain\Search\Query\Query;
28
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Sorting\SortingHelper;
29
use ApacheSolrForTypo3\Solr\Domain\Search\SearchRequest;
30
use ApacheSolrForTypo3\Solr\Domain\Search\SearchRequestAware;
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
33
/**
34
 * Sorting search component
35
 *
36
 * TODO maybe merge ApacheSolrForTypo3\Solr\Sorting into ApacheSolrForTypo3\Solr\Search\SortingComponent
37
 *
38
 * @author Ingo Renner <[email protected]>
39
 */
40
class SortingComponent extends AbstractComponent implements QueryAware, SearchRequestAware
41
{
42
43
    /**
44
     * Solr query
45
     *
46
     * @var Query
47
     */
48
    protected $query;
49
50
    /**
51
     * @var SearchRequest
52
     */
53
    protected $searchRequest;
54
55
    /**
56
     * Initializes the search component.
57
     *
58
     * Sets the sorting query parameters
59
     */
60 40
    public function initializeSearchComponent()
61
    {
62 40
        if (!empty($this->searchConfiguration['query.']['sortBy'])) {
63 1
            $this->query->addQueryParameter('sort', $this->searchConfiguration['query.']['sortBy']);
64
        }
65
66 40
        $arguments = $this->searchRequest->getArguments();
67
68 40
        if (!empty($this->searchConfiguration['sorting']) && $this->hasValidSorting($arguments)) {
69 1
            $sortHelper = GeneralUtility::makeInstance(SortingHelper::class, $this->searchConfiguration['sorting.']['options.']);
70 1
            $sortField = $sortHelper->getSortFieldFromUrlParameter($arguments['sort']);
71 1
            $this->query->setSorting($sortField);
72
        }
73 40
    }
74
75
    /**
76
     * Checks if the arguments array has a valid sorting.
77
     *
78
     * @param array $arguments
79
     * @return bool
80
     */
81 33
    protected function hasValidSorting(array $arguments)
82
    {
83 33
        return !empty($arguments['sort']) && preg_match('/^([a-z0-9_]+ (asc|desc)[, ]*)*([a-z0-9_]+ (asc|desc))+$/i', $arguments['sort']);
84
    }
85
86
    /**
87
     * Provides the extension component with an instance of the current query.
88
     *
89
     * @param Query $query Current query
90
     */
91 40
    public function setQuery(Query $query)
92
    {
93 40
        $this->query = $query;
94 40
    }
95
96
    /**
97
     * @param SearchRequest $searchRequest
98
     */
99 40
    public function setSearchRequest(SearchRequest $searchRequest)
100
    {
101 40
        $this->searchRequest = $searchRequest;
102 40
    }
103
}
104