Passed
Push — release-11.5.x ( 385fe8...cd49eb )
by Rafael
53:22 queued 14:05
created

applyTypoScriptOverridesOnIndexingUrl()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 23
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 23
ccs 8
cts 10
cp 0.8
rs 9.6111
cc 5
nc 16
nop 2
crap 5.2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace ApacheSolrForTypo3\Solr\Domain\Index\PageIndexer\Helper\UriBuilder;
19
20
use ApacheSolrForTypo3\Solr\IndexQueue\Item;
21
use ApacheSolrForTypo3\Solr\IndexQueue\PageIndexerDataUrlModifier;
22
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager;
23
use ApacheSolrForTypo3\Solr\System\Url\UrlHelper;
24
use RuntimeException;
25
use TYPO3\CMS\Core\Utility\GeneralUtility;
26
27
/**
28
 * Implementations of this class are able to build an indexing url for solr page indexing.
29
 */
30
abstract class AbstractUriStrategy
31
{
32
    /**
33
     * @var SolrLogManager
34
     */
35
    protected SolrLogManager $logger;
36
37
    /**
38
     * AbstractUriStrategy constructor.
39
     * @param SolrLogManager|null $logger
40
     */
41 3
    public function __construct(
42
        SolrLogManager $logger = null
43
    ) {
44 3
        $this->logger = $logger ?? GeneralUtility::makeInstance(SolrLogManager::class, /** @scrutinizer ignore-type */ __CLASS__);
45
    }
46
47
    /**
48
     * @param UrlHelper $urlHelper
49
     * @param array $overrideConfiguration
50
     * @return UrlHelper
51
     */
52 3
    protected function applyTypoScriptOverridesOnIndexingUrl(UrlHelper $urlHelper, array $overrideConfiguration = []): UrlHelper
53
    {
54
        // check whether we should use ssl / https
55 3
        if (!empty($overrideConfiguration['scheme'])) {
56 1
            $urlHelper->setScheme($overrideConfiguration['scheme']);
0 ignored issues
show
Deprecated Code introduced by
The function ApacheSolrForTypo3\Solr\...\UrlHelper::setScheme() has been deprecated: Will be removed with v12. Use Uri::withScheme instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

56
            /** @scrutinizer ignore-deprecated */ $urlHelper->setScheme($overrideConfiguration['scheme']);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
57
        }
58
59
        // overwriting the host
60 3
        if (!empty($overrideConfiguration['host'])) {
61 1
            $urlHelper->setHost($overrideConfiguration['host']);
0 ignored issues
show
Deprecated Code introduced by
The function ApacheSolrForTypo3\Solr\...rl\UrlHelper::setHost() has been deprecated: Will be removed with v12. Use withHost instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

61
            /** @scrutinizer ignore-deprecated */ $urlHelper->setHost($overrideConfiguration['host']);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
62
        }
63
64
        // overwriting the port
65 3
        if (!empty($overrideConfiguration['port'])) {
66
            $urlHelper->setPort((int)$overrideConfiguration['port']);
0 ignored issues
show
Deprecated Code introduced by
The function ApacheSolrForTypo3\Solr\...rl\UrlHelper::setPort() has been deprecated: Will be removed with v12. Use withPort instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

66
            /** @scrutinizer ignore-deprecated */ $urlHelper->setPort((int)$overrideConfiguration['port']);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
67
        }
68
69
        // setting a path if TYPO3 is installed in a subdirectory
70 3
        if (!empty($overrideConfiguration['path'])) {
71
            $urlHelper->setPath($overrideConfiguration['path']);
0 ignored issues
show
Deprecated Code introduced by
The function ApacheSolrForTypo3\Solr\...rl\UrlHelper::setPath() has been deprecated: Will be removed with v12. Use withPath instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

71
            /** @scrutinizer ignore-deprecated */ $urlHelper->setPath($overrideConfiguration['path']);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
72
        }
73
74 3
        return $urlHelper;
75
    }
76
77
    /**
78
     * @param Item $item
79
     * @param int $language
80
     * @param string $mountPointParameter
81
     * @param array $options
82
     * @return string
83
     */
84 3
    public function getPageIndexingUriFromPageItemAndLanguageId(
85
        Item $item,
86
        int $language = 0,
87
        string $mountPointParameter = '',
88
        array $options = []
89
    ): string {
90 3
        $pageIndexUri = $this->buildPageIndexingUriFromPageItemAndLanguageId($item, $language, $mountPointParameter);
91 3
        $urlHelper = GeneralUtility::makeInstance(UrlHelper::class, $pageIndexUri);
92 3
        $overrideConfiguration = $options['frontendDataHelper.'] ?? [];
93 3
        $urlHelper = $this->applyTypoScriptOverridesOnIndexingUrl($urlHelper, $overrideConfiguration);
94 3
        $dataUrl = $urlHelper->getUrl();
0 ignored issues
show
Deprecated Code introduced by
The function ApacheSolrForTypo3\Solr\...Url\UrlHelper::getUrl() has been deprecated: Will be removed with v12. Use {@link __toString()} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

94
        $dataUrl = /** @scrutinizer ignore-deprecated */ $urlHelper->getUrl();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
95
96 3
        if (!GeneralUtility::isValidUrl($dataUrl)) {
97
            $this->logger->log(
98
                SolrLogManager::ERROR,
99
                'Could not create a valid URL to get frontend data while trying to index a page.',
100
                [
101
                    'item' => (array)$item,
102
                    'constructed URL' => $dataUrl,
103
                    'scheme' => $urlHelper->getScheme(),
104
                    'host' => $urlHelper->getHost(),
105
                    'path' => $urlHelper->getPath(),
106
                    'page ID' => $item->getRecordUid(),
107
                    'indexer options' => $options,
108
                ]
109
            );
110
111
            throw new RuntimeException(
112
                'Could not create a valid URL to get frontend data while trying to index a page. Created URL: ' . $dataUrl,
113
                1311080805
114
            );
115
        }
116
117 3
        return $this->applyDataUrlModifier($item, $language, $dataUrl, $urlHelper);
118
    }
119
120
    /**
121
     * @param Item $item
122
     * @param int $language
123
     * @param string $mountPointParameter
124
     * @return mixed
125
     */
126
    abstract protected function buildPageIndexingUriFromPageItemAndLanguageId(
127
        Item $item,
128
        int $language = 0,
129
        string $mountPointParameter = ''
130
    );
131
132
    /**
133
     * @param Item $item
134
     * @param int $language
135
     * @param string $dataUrl
136
     * @param UrlHelper $urlHelper
137
     * @return string
138
     */
139 3
    protected function applyDataUrlModifier(
140
        Item $item,
141
        int $language,
142
        string $dataUrl,
143
        UrlHelper $urlHelper
144
    ): string {
145 3
        if (empty($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['IndexQueuePageIndexer']['dataUrlModifier'])) {
146 3
            return $dataUrl;
147
        }
148
149
        $dataUrlModifier = GeneralUtility::makeInstance($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['IndexQueuePageIndexer']['dataUrlModifier']);
150
        if (!$dataUrlModifier instanceof PageIndexerDataUrlModifier) {
151
            throw new RuntimeException($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['IndexQueuePageIndexer']['dataUrlModifier'] . ' is not an implementation of ApacheSolrForTypo3\Solr\IndexQueue\PageIndexerDataUrlModifier', 1290523345);
152
        }
153
154
        return $dataUrlModifier->modifyDataUrl(
155
            $dataUrl,
156
            [
157
                'item' => $item, 'scheme' => $urlHelper->getScheme(), 'host' => $urlHelper->getHost(),
158
                'path' => $urlHelper->getPath(), 'pageId' => $item->getRecordUid(), 'language' => $language,
159
            ]
160
        );
161
    }
162
}
163