Passed
Push — master ( 702ea2...4e40a2 )
by Timo
21:41
created

DateRangeUrlDecoder::decode()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3.072

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 18
ccs 12
cts 15
cp 0.8
rs 9.9
c 0
b 0
f 0
cc 3
nc 4
nop 2
crap 3.072
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\RangeBased\DateRange;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2010-2011 Markus Goldbach <[email protected]>
8
 *  (c) 2012-2015 Ingo Renner <[email protected]>
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\FacetUrlDecoderInterface;
29
use ApacheSolrForTypo3\Solr\System\DateTime\FormatService;
30
use TYPO3\CMS\Core\Utility\GeneralUtility;
31
32
/**
33
 * Parser to build solr range queries from tx_solr[filter]
34
 *
35
 * @author Markus Goldbach <[email protected]>
36
 */
37
class DateRangeUrlDecoder implements FacetUrlDecoderInterface
38
{
39
40
    /**
41
     * Delimiter for date parts in the URL.
42
     *
43
     * @var string
44
     */
45
    const DELIMITER = '-';
46
47
    /**
48
     * Parses the given date range from a GET parameter and returns a Solr
49
     * date range filter.
50
     *
51
     * @param string $dateRange The range filter query string from the query URL
52
     * @param array $configuration Facet configuration
53
     * @return string Lucene query language filter to be used for querying Solr
54
     */
55 3
    public function decode($dateRange, array $configuration = [])
56
    {
57 3
        list($dateRangeStart, $dateRangeEnd) = explode(self::DELIMITER, $dateRange);
58
59 3
        $formatService = GeneralUtility::makeInstance(FormatService::class);
60 3
        $fromPart = '*';
61 3
        if($dateRangeStart !== ''){
62 2
            $fromPart = $formatService->timestampToIso(strtotime($dateRangeStart));
63
        }
64
65 3
        $toPart = '*';
66 3
        if($dateRangeEnd !== ''){
67 2
            $dateRangeEnd .= '59'; // adding 59 seconds
68 2
            $toPart = $formatService->timestampToIso(strtotime($dateRangeEnd));
69
        }
70
71 3
        $dateRangeFilter = '[' . $fromPart . ' TO ' . $toPart . ']';
72 3
        return $dateRangeFilter;
73
    }
74
}
75