Failed Conditions
Push — release-11.5.x ( 71e6eb...3bfdb1 )
by Markus
27:37
created

SiteHighlighterUrlModifier   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
eloc 10
dl 0
loc 31
ccs 0
cts 10
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A modify() 0 22 3
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace ApacheSolrForTypo3\Solr\Domain\Search\Highlight;
17
18
use ApacheSolrForTypo3\Solr\System\Url\UrlHelper;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21
/**
22
 * Class SiteHighlighterUrlModifier provides highlighting of the search words on the document's actual page by
23
 * adding parameters to a document's URL property.
24
 *
25
 * Initial code from ApacheSolrForTypo3\Solr\ResultDocumentModifier\SiteHighlighter
26
 *
27
 * @author Stefan Sprenger <[email protected]>
28
 * @author Timo Hund <[email protected]>
29
 */
30
class SiteHighlighterUrlModifier
31
{
32
    /**
33
     * @param string $url
34
     * @param string $searchWords
35
     * @param bool $addNoCache
36
     * @param bool $keepCHash
37
     * @return string
38
     */
39
    public function modify(
40
        string $url,
41
        string $searchWords,
42
        bool $addNoCache = true,
43
        bool $keepCHash = false
44
    ): string {
45
        $searchWords = str_replace('&quot;', '', $searchWords);
46
        $searchWords = GeneralUtility::trimExplode(' ', $searchWords, true);
47
48
        /** @var UrlHelper $urlHelper */
49
        $urlHelper = GeneralUtility::makeInstance(UrlHelper::class, /** @scrutinizer ignore-type */ $url)
50
            ->withQueryParameter('sword_list', $searchWords);
51
52
        if ($addNoCache) {
53
            $urlHelper = $urlHelper->withQueryParameter('no_cache', '1');
54
        }
55
56
        if (!$keepCHash) {
57
            $urlHelper = $urlHelper->withoutQueryParameter('cHash');
58
        }
59
60
        return $urlHelper->__toString();
61
    }
62
}
63