Passed
Push — master ( ed38f6...30aafa )
by Timo
11:15
created

AbstractUriStrategy::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php declare(strict_types = 1);
2
3
namespace ApacheSolrForTypo3\Solr\Domain\Index\PageIndexer\Helper\UriBuilder;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2019 Timo Hund <[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
 *  A copy is found in the textfile GPL.txt and important notices to the license
20
 *  from the author is found in LICENSE.txt distributed with these scripts.
21
 *
22
 *
23
 *  This script is distributed in the hope that it will be useful,
24
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
 *  GNU General Public License for more details.
27
 *
28
 *  This copyright notice MUST APPEAR in all copies of the script!
29
 ***************************************************************/
30
31
use ApacheSolrForTypo3\Solr\IndexQueue\Item;
32
use ApacheSolrForTypo3\Solr\IndexQueue\PageIndexerDataUrlModifier;
33
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager;
34
use ApacheSolrForTypo3\Solr\System\Url\UrlHelper;
35
use TYPO3\CMS\Core\Utility\GeneralUtility;
36
37
/**
38
 * Implementations of this class are able to build an indexing url for solr page indexing.
39
 *
40
 * @package ApacheSolrForTypo3\Solr\Domain\Index\PageIndexer\Helper\UriBuilder
41
 */
42
abstract class AbstractUriStrategy
43
{
44
    /**
45
     * @var SolrLogManager|null|object
46
     */
47
    protected $logger;
48
49
    /**
50
     * AbstractUriStrategy constructor.
51
     * @param SolrLogManager|null $logger
52
     */
53
    public function __construct(SolrLogManager $logger = null)
54
    {
55
        $this->logger = $logger ?? GeneralUtility::makeInstance(SolrLogManager::class, /** @scrutinizer ignore-type */ __CLASS__);
56
    }
57
58
    /**
59
     * @param UrlHelper $urlHelper
60
     * @param array $overrideConfiguration
61
     * @return UrlHelper
62
     */
63
    protected function applyTypoScriptOverridesOnIndexingUrl(UrlHelper $urlHelper, array $overrideConfiguration = []): UrlHelper
64
    {
65
        // check whether we should use ssl / https
66
        if (!empty($overrideConfiguration['scheme'])) {
67
            $urlHelper->setScheme($overrideConfiguration['scheme']);
68
        }
69
70
        // overwriting the host
71
        if (!empty($overrideConfiguration['host'])) {
72
            $urlHelper->setHost($overrideConfiguration['host']);
73
        }
74
75
        // setting a path if TYPO3 is installed in a sub directory
76
        if (!empty($overrideConfiguration['path'])) {
77
            $urlHelper->setPath($overrideConfiguration['path']);
78
        }
79
80
        return $urlHelper;
81
    }
82
83
    /**
84
     * @param Item $item
85
     * @param int $language
86
     * @param string $mountPointParameter
87
     * @param array $options
88
     * @return string
89
     */
90
    public function getPageIndexingUriFromPageItemAndLanguageId(Item $item, int $language = 0, string $mountPointParameter = '', $options = []): string
91
    {
92
        $pageIndexUri = $this->buildPageIndexingUriFromPageItemAndLanguageId($item, $language, $mountPointParameter);
93
        $urlHelper = GeneralUtility::makeInstance(UrlHelper::class, $pageIndexUri);
94
        $overrideConfiguration = is_array($options['frontendDataHelper.']) ? $options['frontendDataHelper.'] : [];
95
        $urlHelper = $this->applyTypoScriptOverridesOnIndexingUrl($urlHelper, $overrideConfiguration);
96
        $dataUrl = $urlHelper->getUrl();
97
98
        if (!GeneralUtility::isValidUrl($dataUrl)) {
99
            $this->logger->log(
0 ignored issues
show
Bug introduced by
The method log() does not exist on null. ( Ignorable by Annotation )

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

99
            $this->logger->/** @scrutinizer ignore-call */ 
100
                           log(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

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