Passed
Pull Request — master (#668)
by Tomas Norre
04:59
created

UrlService   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 77.78%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 30
c 2
b 0
f 0
dl 0
loc 51
ccs 28
cts 36
cp 0.7778
rs 10
wmc 8

1 Method

Rating   Name   Duplication   Size   Complexity  
B getUrlFromPageAndQueryParameters() 0 41 8
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AOE\Crawler\Service;
6
7
/*
8
 * (c) 2020 AOE GmbH <[email protected]>
9
 *
10
 * This file is part of the TYPO3 Crawler Extension.
11
 *
12
 * It is free software; you can redistribute it and/or modify it under
13
 * the terms of the GNU General Public License, either version 2
14
 * of the License, or any later version.
15
 *
16
 * For the full copyright and license information, please read the
17
 * LICENSE.txt file that was distributed with this source code.
18
 *
19
 * The TYPO3 project - inspiring people to share!
20
 */
21
22
use Psr\Http\Message\UriInterface;
23
use TYPO3\CMS\Core\Http\Uri;
24
use TYPO3\CMS\Core\Routing\SiteMatcher;
25
use TYPO3\CMS\Core\Site\Entity\Site;
26
use TYPO3\CMS\Core\Utility\GeneralUtility;
27
28
class UrlService
29
{
30
    /**
31
     * Build a URL from a Page and the Query String. If the page has a Site configuration, it can be built by using
32
     * the Site instance.
33
     *
34
     * @param int $httpsOrHttp see tx_crawler_configuration.force_ssl
35
     * @throws \TYPO3\CMS\Core\Exception\SiteNotFoundException
36
     * @throws \TYPO3\CMS\Core\Routing\InvalidRouteArgumentsException
37
     */
38 12
    public function getUrlFromPageAndQueryParameters(int $pageId, string $queryString, ?string $alternativeBaseUrl, int $httpsOrHttp): UriInterface
39
    {
40 12
        $site = GeneralUtility::makeInstance(SiteMatcher::class)->matchByPageId((int) $pageId);
41 12
        if ($site instanceof Site) {
42 5
            $queryString = ltrim($queryString, '?&');
43 5
            $queryParts = [];
44 5
            parse_str($queryString, $queryParts);
45 5
            unset($queryParts['id']);
46
            // workaround as long as we don't have native language support in crawler configurations
47 5
            if (isset($queryParts['L'])) {
48 1
                $queryParts['_language'] = $queryParts['L'];
49 1
                unset($queryParts['L']);
50 1
                $siteLanguage = $site->getLanguageById((int) $queryParts['_language']);
0 ignored issues
show
Unused Code introduced by
The assignment to $siteLanguage is dead and can be removed.
Loading history...
51
            } else {
52 4
                $siteLanguage = $site->getDefaultLanguage();
53
            }
54 5
            $url = $site->getRouter()->generateUri($pageId, $queryParts);
55 5
            if (! empty($alternativeBaseUrl)) {
56 2
                $alternativeBaseUrl = new Uri($alternativeBaseUrl);
57 2
                $url = $url->withHost($alternativeBaseUrl->getHost());
58 2
                $url = $url->withScheme($alternativeBaseUrl->getScheme());
59 2
                $url = $url->withPort($alternativeBaseUrl->getPort());
60 2
                if ($userInfo = $alternativeBaseUrl->getUserInfo()) {
61 5
                    $url = $url->withUserInfo($userInfo);
62
                }
63
            }
64
        } else {
65
            // Technically this is not possible with site handling, but kept for backwards-compatibility reasons
66
            // Once EXT:crawler is v10-only compatible, this should be removed completely
67 7
            $baseUrl = ($alternativeBaseUrl ?: GeneralUtility::getIndpEnv('TYPO3_SITE_URL'));
68 7
            $url = rtrim($baseUrl, '/') . '/index.php' . $queryString;
69 7
            $url = new Uri($url);
70
        }
71
72 12
        if ($httpsOrHttp === -1) {
73 1
            $url = $url->withScheme('http');
74 11
        } elseif ($httpsOrHttp === 1) {
75 7
            $url = $url->withScheme('https');
76
        }
77
78 12
        return $url;
79
    }
80
}
81