Passed
Branch main (5e381f)
by Rafael
06:02 queued 02:52
created

DebugWriter::getIsDebugOutputEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace ApacheSolrForTypo3\Solr\System\Logging;
17
18
use ApacheSolrForTypo3\Solr\IndexQueue\PageIndexerRequest;
19
use ApacheSolrForTypo3\Solr\Util;
20
use TYPO3\CMS\Core\Http\ApplicationType;
21
use TYPO3\CMS\Core\Http\Request;
22
use TYPO3\CMS\Core\Utility\DebugUtility;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
use TYPO3\CMS\Extbase\Utility\DebuggerUtility;
25
26
/**
27
 * The DebugWriter is used to write the devLog messages to the output of the page, or to the TYPO3 console in the
28
 * backend to provide a simple and lightweight debugging possibility.
29
 *
30
 * @author Timo Hund <[email protected]>
31
 */
32
class DebugWriter
33
{
34
35
    /**
36
     * When the feature is enabled with: plugin.tx_solr.logging.debugOutput the log writer uses the extbase
37
     * debug functionality in the frontend, or the console in the backend to display the devlog messages.
38
     *
39
     * @param int|string $level Log level. Value according to \TYPO3\CMS\Core\Log\LogLevel. Alternatively accepts a string.
40
     * @param string $message Log message.
41
     * @param array $data Additional data to log
42
     */
43 83
    public function write($level, $message, $data = [])
44
    {
45 83
        $debugAllowedForIp = $this->getIsAllowedByDevIPMask();
46 83
        if (!$debugAllowedForIp) {
47 81
            return;
48
        }
49
50 2
        $isDebugOutputEnabled = $this->getIsDebugOutputEnabled();
51 2
        if (!$isDebugOutputEnabled) {
52 1
            return;
53
        }
54
55 1
        $isPageIndexingRequest = $this->getIsPageIndexingRequest();
56 1
        if ($isPageIndexingRequest) {
57
            return;
58
        }
59
60 1
        $this->writeDebugMessage($level, $message, $data);
61
    }
62
63
    /**
64
     * @return bool
65
     */
66 80
    protected function getIsAllowedByDevIPMask()
67
    {
68 80
        return GeneralUtility::cmpIP(GeneralUtility::getIndpEnv('REMOTE_ADDR'), $GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask']);
0 ignored issues
show
Bug introduced by
It seems like TYPO3\CMS\Core\Utility\G...tIndpEnv('REMOTE_ADDR') can also be of type array<string,boolean|null|string>; however, parameter $baseIP of TYPO3\CMS\Core\Utility\GeneralUtility::cmpIP() 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

68
        return GeneralUtility::cmpIP(/** @scrutinizer ignore-type */ GeneralUtility::getIndpEnv('REMOTE_ADDR'), $GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask']);
Loading history...
69
    }
70
71
    /**
72
     * Check if Logging via debugOutput has been configured
73
     *
74
     * @return bool
75
     */
76
    protected function getIsDebugOutputEnabled()
77
    {
78
        return Util::getSolrConfiguration()->getLoggingDebugOutput();
79
    }
80
81 1
    protected function getIsPageIndexingRequest(): bool
82
    {
83 1
        if (!($GLOBALS['TYPO3_REQUEST'] ?? null) instanceof Request) {
84 1
            return false;
85
        }
86
        return $GLOBALS['TYPO3_REQUEST']->hasHeader(PageIndexerRequest::SOLR_INDEX_HEADER);
87
    }
88
89
    /**
90
     * @param int|string $level Log level. Value according to \TYPO3\CMS\Core\Log\LogLevel. Alternatively accepts a string.
91
     * @param string $message Log message.
92
     * @param array $data Additional data to log
93
     */
94
    protected function writeDebugMessage($level, $message, $data)
95
    {
96
        $parameters = ['extKey' => 'solr', 'msg' => $message, 'level' => $level, 'data' => $data];
97
        $message = $parameters['msg'] ?? '';
98
        if (ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isBackend()) {
99
            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

99
            DebugUtility::debug(/** @scrutinizer ignore-type */ $parameters, $parameters['extKey'], 'DevLog ext:solr: ' . $message);
Loading history...
100
        } else {
101
            echo $message . ':<br/>';
102
            DebuggerUtility::var_dump($parameters);
103
        }
104
    }
105
}
106