Failed Conditions
Pull Request — release-11.2.x (#3343)
by Rafael
15:25
created

DebugWriter::getIsPageIndexingRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 4
cp 0.75
rs 10
cc 2
nc 2
nop 0
crap 2.0625
1
<?php
2
3
/***************************************************************
4
 *  Copyright notice
5
 *
6
 *  (c) 2010-2016 Timo Hund <[email protected]
7
 *  All rights reserved
8
 *
9
 *  This script is part of the TYPO3 project. The TYPO3 project is
10
 *  free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 3 of the License, or
13
 *  (at your option) any later version.
14
 *
15
 *  The GNU General Public License can be found at
16
 *  http://www.gnu.org/copyleft/gpl.html.
17
 *
18
 *  This script is distributed in the hope that it will be useful,
19
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 *  GNU General Public License for more details.
22
 *
23
 *  This copyright notice MUST APPEAR in all copies of the script!
24
 ***************************************************************/
25
26
namespace ApacheSolrForTypo3\Solr\System\Logging;
27
28
use ApacheSolrForTypo3\Solr\IndexQueue\PageIndexerRequest;
29
use ApacheSolrForTypo3\Solr\Util;
30
use TYPO3\CMS\Core\Http\Request;
31
use TYPO3\CMS\Core\Utility\DebugUtility;
32
use TYPO3\CMS\Core\Utility\GeneralUtility;
33
use TYPO3\CMS\Extbase\Utility\DebuggerUtility;
34
35
/**
36
 * The DebugWriter is used to write the devLog messages to the output of the page, or to the TYPO3 console in the
37
 * backend to provide a simple and lightweight debugging possibility.
38
 *
39
 * @author Timo Hund <[email protected]>
40
 */
41
class DebugWriter
42
{
43
44
    /**
45
     * When the feature is enabled with: plugin.tx_solr.logging.debugOutput the log writer uses the extbase
46
     * debug functionality in the frontend, or the console in the backend to display the devlog messages.
47
     *
48
     * @param int|string $level Log level. Value according to \TYPO3\CMS\Core\Log\LogLevel. Alternatively accepts a string.
49
     * @param string $message Log message.
50
     * @param array $data Additional data to log
51
     */
52 51
    public function write($level, $message, $data = [])
53
    {
54 51
        $debugAllowedForIp = $this->getIsAllowedByDevIPMask();
55 51
        if (!$debugAllowedForIp) {
56 49
            return;
57
        }
58
59 2
        $isDebugOutputEnabled = $this->getIsDebugOutputEnabled();
60 2
        if (!$isDebugOutputEnabled) {
61 1
            return;
62
        }
63
64 1
        $isPageIndexingRequest = $this->getIsPageIndexingRequest();
65 1
        if ($isPageIndexingRequest) {
66
            return;
67
        }
68
69 1
        $this->writeDebugMessage($level, $message, $data);
70 1
    }
71
72
    /**
73
     * @return bool
74
     */
75 48
    protected function getIsAllowedByDevIPMask()
76
    {
77 48
        return GeneralUtility::cmpIP(GeneralUtility::getIndpEnv('REMOTE_ADDR'), $GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask']);
78
    }
79
80
    /**
81
     * Check if Logging via debugOutput has been configured
82
     *
83
     * @return bool
84
     */
85
    protected function getIsDebugOutputEnabled()
86
    {
87
        return Util::getSolrConfiguration()->getLoggingDebugOutput();
88
    }
89
90 1
    protected function getIsPageIndexingRequest(): bool
91
    {
92 1
        if (!($GLOBALS['TYPO3_REQUEST'] ?? null) instanceof Request) {
93 1
            return false;
94
        }
95
        return $GLOBALS['TYPO3_REQUEST']->hasHeader(PageIndexerRequest::SOLR_INDEX_HEADER);
96
    }
97
98
    /**
99
     * @param int|string $level Log level. Value according to \TYPO3\CMS\Core\Log\LogLevel. Alternatively accepts a string.
100
     * @param string $message Log message.
101
     * @param array $data Additional data to log
102
     */
103
    protected function writeDebugMessage($level, $message, $data)
104
    {
105
        $parameters = ['extKey' => 'solr', 'msg' => $message, 'level' => $level, 'data' => $data];
106
        $message = $parameters['msg'] ?? '';
107
        if (TYPO3_MODE === 'BE') {
108
            DebugUtility::debug($parameters, $parameters['extKey'], 'DevLog ext:solr: ' . $message);
0 ignored issues
show
Bug introduced by
$parameters of type array<string,array|integer|string> is incompatible with the type string expected by parameter $var of TYPO3\CMS\Core\Utility\DebugUtility::debug(). ( Ignorable by Annotation )

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

108
            DebugUtility::debug(/** @scrutinizer ignore-type */ $parameters, $parameters['extKey'], 'DevLog ext:solr: ' . $message);
Loading history...
109
        } else {
110
            echo $message . ':<br/>';
111
            DebuggerUtility::var_dump($parameters);
112
        }
113
    }
114
}
115