Passed
Push — release-11.5.x ( eab588...506b54 )
by Rafael
49:07 queued 25:53
created

FrontendEnvironment::getConfigurationFromPageId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 4
crap 1
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;
19
20
use ApacheSolrForTypo3\Solr\FrontendEnvironment\Tsfe;
21
use ApacheSolrForTypo3\Solr\FrontendEnvironment\TypoScript;
22
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
23
use Doctrine\DBAL\Driver\Exception as DBALDriverException;
24
use TYPO3\CMS\Backend\Utility\BackendUtility;
25
use TYPO3\CMS\Core\SingletonInterface;
26
use TYPO3\CMS\Core\Utility\GeneralUtility;
27
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
28
29
/**
30
 * Class FrontendEnvironment is responsible for initializing/simulating the frontend in backend context
31
 * For example on:
32
 * * indexing
33
 * * Status-report and other actions via TYPO3 backend
34
 * * etc.
35
 */
36
class FrontendEnvironment implements SingletonInterface
37
{
38
    /**
39
     * Loads the TypoScript configuration for a given page id and language.
40
     * Language usage may be disabled to get the default TypoScript
41
     * configuration.
42
     *
43
     * @param int $pageId
44
     * @param ?string $path
45
     * @param ?int $language
46
     * @param int|null $rootPageId
47
     * @return TypoScriptConfiguration
48
     * @throws DBALDriverException
49
     */
50 210
    public function getConfigurationFromPageId(int $pageId, ?string $path = '', ?int $language = 0, ?int $rootPageId = null): TypoScriptConfiguration
51
    {
52 210
        return GeneralUtility::makeInstance(TypoScript::class)->getConfigurationFromPageId($pageId, $path, $language, $rootPageId);
53
    }
54
55
    /**
56
     * Check whether the page record is within the configured allowed pages types(doktype) for indexing.
57
     * Uses TypoScript: plugin.tx_solr.index.queue.<queue name>.allowedPageTypes
58
     *
59
     * @param array $pageRecord
60
     * @param ?string $configurationName
61
     * @return bool
62
     * @throws DBALDriverException
63
     */
64 55
    public function isAllowedPageType(array $pageRecord, ?string $configurationName = 'pages'): bool
65
    {
66
        // $pageRecord could come from DataHandler and with all columns. So we want to fetch it again.
67 55
        $pageRecord = BackendUtility::getRecord('pages', $pageRecord['uid']);
68 55
        $rootPageRecordUid = $pageRecord['uid'];
69 55
        if (isset($pageRecord['sys_language_uid'])
70 55
            && (int)$pageRecord['sys_language_uid'] > 0
71 55
            && isset($pageRecord['l10n_parent'])
72 55
            && (int)$pageRecord['l10n_parent'] > 0
73
        ) {
74 2
            $rootPageRecordUid = $pageRecord['l10n_parent'];
75
        }
76
77 55
        $tsfe = GeneralUtility::makeInstance(Tsfe::class)->getTsfeByPageIdIgnoringLanguage($rootPageRecordUid);
78 55
        if (!$tsfe instanceof TypoScriptFrontendController) {
79 1
            return false;
80
        }
81 54
        $configuration = $this->getConfigurationFromPageId($rootPageRecordUid, '', $tsfe->getLanguage()->getLanguageId());
82 54
        $allowedPageTypes = $configuration->getIndexQueueAllowedPageTypesArrayByConfigurationName($configurationName);
0 ignored issues
show
Bug introduced by
It seems like $configurationName can also be of type null; however, parameter $configurationName of ApacheSolrForTypo3\Solr\...ayByConfigurationName() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

82
        $allowedPageTypes = $configuration->getIndexQueueAllowedPageTypesArrayByConfigurationName(/** @scrutinizer ignore-type */ $configurationName);
Loading history...
83 54
        return in_array($pageRecord['doktype'], $allowedPageTypes);
84
    }
85
86
    /**
87
     * Returns TypoScriptConfiguration for desired page ID and language id.
88
     *
89
     * @param int $pageId
90
     * @param ?int $language
91
     * @param int|null $rootPageId
92
     * @return TypoScriptConfiguration
93
     * @throws DBALDriverException
94
     */
95 210
    public function getSolrConfigurationFromPageId(int $pageId, ?int $language = 0, ?int $rootPageId = null): TypoScriptConfiguration
96
    {
97 210
        return $this->getConfigurationFromPageId($pageId, '', $language, $rootPageId);
98
    }
99
}
100